Skip to content

Commit cc3a763

Browse files
committed
Bug fix: corrected sleep time calculation in IdleConnectionEvictor; use 1 minute sleep time by default
1 parent 775d80e commit cc3a763

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/impl/IdleConnectionEvictor.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
package org.apache.hc.client5.http.impl;
2929

3030
import java.util.concurrent.ThreadFactory;
31+
import java.util.concurrent.TimeUnit;
3132

3233
import org.apache.hc.core5.annotation.Contract;
3334
import org.apache.hc.core5.annotation.ThreadingBehavior;
@@ -46,14 +47,17 @@
4647
@Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
4748
public final class IdleConnectionEvictor {
4849

50+
private static final TimeValue ONE_SECOND = TimeValue.ofSeconds(1L);
51+
private static final TimeValue ONE_MINUTE = TimeValue.ofMinutes(1L);
52+
4953
private final ThreadFactory threadFactory;
5054
private final Thread thread;
5155

5256
public IdleConnectionEvictor(final ConnPoolControl<?> connectionManager, final ThreadFactory threadFactory,
5357
final TimeValue sleepTime, final TimeValue maxIdleTime) {
5458
Args.notNull(connectionManager, "Connection manager");
5559
this.threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory("idle-connection-evictor", true);
56-
final TimeValue localSleepTime = sleepTime != null ? sleepTime : TimeValue.ofSeconds(5);
60+
final TimeValue localSleepTime = sleepTime != null ? sleepTime : calculateSleepTime(maxIdleTime);
5761
this.thread = this.threadFactory.newThread(() -> {
5862
try {
5963
while (!Thread.currentThread().isInterrupted()) {
@@ -76,7 +80,7 @@ public IdleConnectionEvictor(final ConnPoolControl<?> connectionManager, final T
7680
}
7781

7882
public IdleConnectionEvictor(final ConnPoolControl<?> connectionManager, final TimeValue maxIdleTime) {
79-
this(connectionManager, null, maxIdleTime, maxIdleTime);
83+
this(connectionManager, null, null, maxIdleTime);
8084
}
8185

8286
public void start() {
@@ -95,4 +99,13 @@ public void awaitTermination(final Timeout timeout) throws InterruptedException
9599
thread.join(timeout != null ? timeout.toMilliseconds() : Long.MAX_VALUE);
96100
}
97101

102+
static TimeValue calculateSleepTime(final TimeValue maxIdleTime) {
103+
if (maxIdleTime == null) {
104+
return ONE_MINUTE;
105+
} else {
106+
final TimeValue sleepTime = maxIdleTime.divide(10, TimeUnit.NANOSECONDS);
107+
return sleepTime.compareTo(ONE_SECOND) < 0 ? ONE_SECOND : sleepTime;
108+
}
109+
}
110+
98111
}

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.LinkedList;
3636
import java.util.List;
3737
import java.util.concurrent.ThreadFactory;
38-
import java.util.concurrent.TimeUnit;
3938
import java.util.function.Function;
4039
import java.util.function.UnaryOperator;
4140

@@ -159,8 +158,6 @@
159158
*/
160159
public class HttpAsyncClientBuilder {
161160

162-
private static final TimeValue ONE_SECOND = TimeValue.ofSeconds(1L);
163-
164161
private static class RequestInterceptorEntry {
165162

166163
enum Position { FIRST, LAST }
@@ -1177,11 +1174,8 @@ public CloseableHttpAsyncClient build() {
11771174
}
11781175
if (evictExpiredConnections || evictIdleConnections) {
11791176
if (connManagerCopy instanceof ConnPoolControl) {
1180-
final TimeValue maxIdleTimeCopy = evictIdleConnections ? maxIdleTime : null;
1181-
TimeValue sleepTime = maxIdleTimeCopy != null ? maxIdleTimeCopy.divide(10, TimeUnit.NANOSECONDS) : ONE_SECOND;
1182-
sleepTime = sleepTime.compareTo(ONE_SECOND) < 0 ? ONE_SECOND : sleepTime;
11831177
final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor((ConnPoolControl<?>) connManagerCopy,
1184-
sleepTime, maxIdleTimeCopy);
1178+
null, evictIdleConnections ? maxIdleTime : null);
11851179
closeablesCopy.add(connectionEvictor::shutdown);
11861180
connectionEvictor.start();
11871181
}

httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.LinkedList;
3636
import java.util.List;
3737
import java.util.Map;
38-
import java.util.concurrent.TimeUnit;
3938
import java.util.function.Function;
4039
import java.util.function.UnaryOperator;
4140

@@ -143,8 +142,6 @@
143142
*/
144143
public class HttpClientBuilder {
145144

146-
private static final TimeValue ONE_SECOND = TimeValue.ofSeconds(1L);
147-
148145
private static class RequestInterceptorEntry {
149146

150147
enum Position { FIRST, LAST }
@@ -1117,11 +1114,8 @@ public CloseableHttpClient build() {
11171114
}
11181115
if (evictExpiredConnections || evictIdleConnections) {
11191116
if (connManagerCopy instanceof ConnPoolControl) {
1120-
final TimeValue maxIdleTimeCopy = evictIdleConnections ? maxIdleTime : null;
1121-
TimeValue sleepTime = maxIdleTimeCopy != null ? maxIdleTimeCopy.divide(10, TimeUnit.NANOSECONDS) : ONE_SECOND;
1122-
sleepTime = sleepTime.compareTo(ONE_SECOND) < 0 ? ONE_SECOND : sleepTime;
11231117
final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor((ConnPoolControl<?>) connManagerCopy,
1124-
sleepTime, maxIdleTimeCopy);
1118+
null, evictIdleConnections ? maxIdleTime : null);
11251119
closeablesCopy.add(() -> {
11261120
connectionEvictor.shutdown();
11271121
try {

httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestIdleConnectionEvictor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ void testEvictExpiredOnly() throws Exception {
7878
Assertions.assertFalse(connectionEvictor.isRunning());
7979
}
8080

81+
@Test
82+
void testCalculateSleepTime() throws Exception {
83+
Assertions.assertEquals(TimeValue.ofMinutes(1), IdleConnectionEvictor.calculateSleepTime(null));
84+
Assertions.assertEquals(TimeValue.ofSeconds(3), IdleConnectionEvictor.calculateSleepTime(TimeValue.ofSeconds(30)));
85+
Assertions.assertEquals(TimeValue.ofSeconds(1), IdleConnectionEvictor.calculateSleepTime(TimeValue.ofSeconds(10)));
86+
Assertions.assertEquals(TimeValue.ofSeconds(1), IdleConnectionEvictor.calculateSleepTime(TimeValue.ofMilliseconds(125)));
87+
}
88+
8189
}

0 commit comments

Comments
 (0)