1919import org .apache .http .message .BasicHeader ;
2020import org .apache .http .util .EntityUtils ;
2121
22+ import java .net .SocketException ;
2223import java .net .SocketTimeoutException ;
2324import java .net .URI ;
2425import java .nio .charset .StandardCharsets ;
2526import java .util .Arrays ;
2627import java .util .Collection ;
2728import java .util .Map ;
29+ import java .util .concurrent .TimeUnit ;
2830
2931public class HttpClient {
3032
31- private static final String DEFAULT_USER_AGENT = "tracking-sdk-java/10.0 .0 (https://www.aftership.com) apache-httpclient/4.5.14" ;
33+ private static final String DEFAULT_USER_AGENT = "tracking-sdk-java/10.1 .0 (https://www.aftership.com) apache-httpclient/4.5.14" ;
3234 protected final org .apache .http .client .HttpClient client ;
3335
3436 public HttpClient (final RequestConfig requestConfig , String userAgent ) {
@@ -46,6 +48,9 @@ public HttpClient(final RequestConfig requestConfig, String userAgent) {
4648 client = clientBuilder
4749 .setDefaultHeaders (headers )
4850 .setDefaultRequestConfig (requestConfig )
51+ .setMaxConnPerRoute (20 )
52+ .setMaxConnTotal (100 )
53+ .setConnectionTimeToLive (300 , TimeUnit .SECONDS ).evictExpiredConnections ()
4954 .build ();
5055 }
5156
@@ -66,6 +71,17 @@ public Response request(final Request request, int retries) throws Exception {
6671 "Request timed out."
6772 );
6873 }
74+ } catch (SocketException e ) {
75+ if (e .getMessage ().equals ("Connection reset" )) {
76+ if (i > retries ) {
77+ throw new ApiException (
78+ ErrorEnum .TIMED_OUT .name (),
79+ "Request timed out."
80+ );
81+ }
82+ } else {
83+ throw e ;
84+ }
6985 } catch (Exception e ) {
7086 throw e ;
7187 }
@@ -115,7 +131,7 @@ private boolean shouldRetry(Response response) {
115131
116132 private int delay (int retryAttempt ) {
117133 int delayBase = 3 ;
118- int delay = delayBase * (2 ^ (retryAttempt - 1 ));
134+ int delay = delayBase * (1 << (retryAttempt - 1 ));
119135 double jitter = delay * (Math .random () - 0.5 );
120136 return (int ) (Math .max (1 , delay + jitter ) * 1000 );
121137 }
0 commit comments