Skip to content

Commit 74396ba

Browse files
committed
Bug fix: corrected sleep time calculation in IdleConnectionEvictor; use 1 minute sleep time by default
1 parent 8fdd72e commit 74396ba

4 files changed

Lines changed: 25 additions & 16 deletions

File tree

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
@@ -37,7 +37,6 @@
3737
import java.util.LinkedList;
3838
import java.util.List;
3939
import java.util.concurrent.ThreadFactory;
40-
import java.util.concurrent.TimeUnit;
4140
import java.util.function.Function;
4241
import java.util.function.UnaryOperator;
4342

@@ -163,8 +162,6 @@
163162
*/
164163
public class HttpAsyncClientBuilder {
165164

166-
private static final TimeValue ONE_SECOND = TimeValue.ofSeconds(1L);
167-
168165
private static class RequestInterceptorEntry {
169166

170167
enum Position { FIRST, LAST }
@@ -1146,11 +1143,8 @@ public CloseableHttpAsyncClient build() {
11461143
}
11471144
if (evictExpiredConnections || evictIdleConnections) {
11481145
if (connManagerCopy instanceof ConnPoolControl) {
1149-
final TimeValue maxIdleTimeCopy = evictIdleConnections ? maxIdleTime : null;
1150-
TimeValue sleepTime = maxIdleTimeCopy != null ? maxIdleTimeCopy.divide(10, TimeUnit.NANOSECONDS) : ONE_SECOND;
1151-
sleepTime = sleepTime.compareTo(ONE_SECOND) < 0 ? ONE_SECOND : sleepTime;
11521146
final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor((ConnPoolControl<?>) connManagerCopy,
1153-
sleepTime, maxIdleTimeCopy);
1147+
null, evictIdleConnections ? maxIdleTime : null);
11541148
closeablesCopy.add(connectionEvictor::shutdown);
11551149
connectionEvictor.start();
11561150
}

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
@@ -37,7 +37,6 @@
3737
import java.util.LinkedList;
3838
import java.util.List;
3939
import java.util.Map;
40-
import java.util.concurrent.TimeUnit;
4140
import java.util.function.Function;
4241
import java.util.function.UnaryOperator;
4342

@@ -147,8 +146,6 @@
147146
*/
148147
public class HttpClientBuilder {
149148

150-
private static final TimeValue ONE_SECOND = TimeValue.ofSeconds(1L);
151-
152149
private static class RequestInterceptorEntry {
153150

154151
enum Position { FIRST, LAST }
@@ -1115,11 +1112,8 @@ public CloseableHttpClient build() {
11151112
}
11161113
if (evictExpiredConnections || evictIdleConnections) {
11171114
if (connManagerCopy instanceof ConnPoolControl) {
1118-
final TimeValue maxIdleTimeCopy = evictIdleConnections ? maxIdleTime : null;
1119-
TimeValue sleepTime = maxIdleTimeCopy != null ? maxIdleTimeCopy.divide(10, TimeUnit.NANOSECONDS) : ONE_SECOND;
1120-
sleepTime = sleepTime.compareTo(ONE_SECOND) < 0 ? ONE_SECOND : sleepTime;
11211115
final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor((ConnPoolControl<?>) connManagerCopy,
1122-
sleepTime, maxIdleTimeCopy);
1116+
null, evictIdleConnections ? maxIdleTime : null);
11231117
closeablesCopy.add(() -> {
11241118
connectionEvictor.shutdown();
11251119
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)