Skip to content

Commit 586b265

Browse files
author
Watson Zuo
committed
fix connection reset error
1 parent bcf125b commit 586b265

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Before you begin to integrate:
4747
<dependency>
4848
<groupId>com.aftership</groupId>
4949
<artifactId>tracking-sdk</artifactId>
50-
<version>10.0.0</version>
50+
<version>10.1.0</version>
5151
</dependency>
5252
```
5353

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.aftership</groupId>
88
<artifactId>tracking-sdk</artifactId>
9-
<version>10.0.0</version>
9+
<version>10.1.0</version>
1010

1111
<name>AfterShip Tracking SDK</name>
1212
<description>The official AfterShip Tracking Java API library</description>

src/main/java/com/aftership/tracking/http/HttpClient.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
import org.apache.http.message.BasicHeader;
2020
import org.apache.http.util.EntityUtils;
2121

22+
import java.net.SocketException;
2223
import java.net.SocketTimeoutException;
2324
import java.net.URI;
2425
import java.nio.charset.StandardCharsets;
2526
import java.util.Arrays;
2627
import java.util.Collection;
2728
import java.util.Map;
29+
import java.util.concurrent.TimeUnit;
2830

2931
public 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

Comments
 (0)