Skip to content

Commit 8616a27

Browse files
fix: Keep alive issue with HttpURLConnection (#616)
* fix: Keep alive issue with HttpURLConnection * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 6a5de98 commit 8616a27

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
# [9.10.2]
6+
- HTTP: Fixed stale pooled connection reuse by adding connection TTL, idle/expired eviction and inactivity validation to reduce intermittent `Connection reset` errors when reusing long-lived `VonageClient` instances
7+
8+
# [9.10.1]
9+
- No changes, had to create to trigger a rebuild due to build system problem
10+
511
# [9.10.0]
612
- Exceptions: Added `getRawRequest()` and `getRawResponse()` methods to `VonageApiResponseException` for debugging API errors
713
- Messages: RCS TTL is now publicly settable on all RCS message types, with validation between 300 and 2592000 seconds

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Add the following to your `build.gradle` or `build.gradle.kts` file:
8383

8484
```groovy
8585
dependencies {
86-
implementation("com.vonage:server-sdk:9.10.1")
86+
implementation("com.vonage:server-sdk:9.10.2")
8787
}
8888
```
8989

@@ -94,7 +94,7 @@ Add the following to the `<dependencies>` section of your `pom.xml` file:
9494
<dependency>
9595
<groupId>com.vonage</groupId>
9696
<artifactId>server-sdk</artifactId>
97-
<version>9.10.1</version>
97+
<version>9.10.2</version>
9898
</dependency>
9999
```
100100

pom.xml

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

66
<groupId>com.vonage</groupId>
77
<artifactId>server-sdk</artifactId>
8-
<version>9.10.1</version>
8+
<version>9.10.2</version>
99

1010
<name>Vonage Java Server SDK</name>
1111
<description>Java client for Vonage APIs</description>

src/main/java/com/vonage/client/HttpWrapper.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,22 @@
2929
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
3030
import java.net.URI;
3131
import java.nio.charset.StandardCharsets;
32+
import java.util.concurrent.TimeUnit;
3233
import java.util.UUID;
3334

3435
/**
3536
* Internal class that holds available authentication methods and a shared HttpClient.
3637
*/
3738
public class HttpWrapper {
39+
private static final int
40+
CONNECTION_POOL_MAX = 200,
41+
CONNECTION_TTL_SECONDS = 60,
42+
VALIDATE_AFTER_INACTIVITY_MS = 2000,
43+
EVICT_IDLE_CONNECTIONS_SECONDS = 30;
44+
3845
private static final String
3946
CLIENT_NAME = "vonage-java-sdk",
40-
CLIENT_VERSION = "9.10.1",
47+
CLIENT_VERSION = "9.10.2",
4148
JAVA_VERSION = System.getProperty("java.version"),
4249
USER_AGENT = String.format("%s/%s java/%s", CLIENT_NAME, CLIENT_VERSION, JAVA_VERSION);
4350

@@ -168,16 +175,16 @@ public AuthCollection getAuthCollection() {
168175
}
169176

170177
protected CloseableHttpClient createHttpClient() {
171-
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
172-
connectionManager.setDefaultMaxPerRoute(200);
173-
connectionManager.setMaxTotal(200);
178+
PoolingHttpClientConnectionManager connectionManager =
179+
new PoolingHttpClientConnectionManager(CONNECTION_TTL_SECONDS, TimeUnit.SECONDS);
180+
connectionManager.setDefaultMaxPerRoute(CONNECTION_POOL_MAX);
181+
connectionManager.setMaxTotal(CONNECTION_POOL_MAX);
174182
connectionManager.setDefaultConnectionConfig(
175183
ConnectionConfig.custom().setCharset(StandardCharsets.UTF_8).build()
176184
);
177185
connectionManager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build());
186+
connectionManager.setValidateAfterInactivity(VALIDATE_AFTER_INACTIVITY_MS);
178187

179-
// Need to work out a good value for the following:
180-
// threadSafeClientConnManager.setValidateAfterInactivity();
181188
RequestConfig requestConfig = RequestConfig.custom()
182189
.setConnectTimeout(httpConfig.getTimeoutMillis())
183190
.setConnectionRequestTimeout(httpConfig.getTimeoutMillis())
@@ -188,6 +195,8 @@ protected CloseableHttpClient createHttpClient() {
188195
.setConnectionManager(connectionManager)
189196
.setUserAgent(getUserAgent())
190197
.setDefaultRequestConfig(requestConfig)
198+
.evictExpiredConnections()
199+
.evictIdleConnections(EVICT_IDLE_CONNECTIONS_SECONDS, TimeUnit.SECONDS)
191200
.useSystemProperties().disableRedirectHandling();
192201

193202
URI proxy = httpConfig.getProxy();

0 commit comments

Comments
 (0)