Skip to content

Commit 26d46d7

Browse files
authored
Changed some other fields to java.time.Duration (#1866)
1 parent e294c70 commit 26d46d7

File tree

12 files changed

+82
-70
lines changed

12 files changed

+82
-70
lines changed

client/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,16 @@ public interface AsyncHttpClientConfig {
9393
Duration getReadTimeout();
9494

9595
/**
96-
* Return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in pool.
96+
* Return the maximum time an {@link AsyncHttpClient} will keep connection in pool.
9797
*
98-
* @return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in pool.
98+
* @return the maximum time an {@link AsyncHttpClient} will keep connection in pool.
9999
*/
100-
int getPooledConnectionIdleTimeout();
100+
Duration getPooledConnectionIdleTimeout();
101101

102102
/**
103-
* @return the period in millis to clean the pool of dead and idle connections.
103+
* @return the period to clean the pool of dead and idle connections.
104104
*/
105-
int getConnectionPoolCleanerPeriod();
105+
Duration getConnectionPoolCleanerPeriod();
106106

107107
/**
108108
* Return the maximum time an {@link AsyncHttpClient} waits until the response is completed.
@@ -236,9 +236,9 @@ public interface AsyncHttpClientConfig {
236236
boolean isStrict302Handling();
237237

238238
/**
239-
* @return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in the pool, or -1 to keep connection while possible.
239+
* @return the maximum time an {@link AsyncHttpClient} will keep connection in the pool, or negative value to keep connection while possible.
240240
*/
241-
int getConnectionTtl();
241+
Duration getConnectionTtl();
242242

243243
boolean isUseOpenSsl();
244244

@@ -296,9 +296,9 @@ public interface AsyncHttpClientConfig {
296296

297297
boolean isKeepEncodingHeader();
298298

299-
int getShutdownQuietPeriod();
299+
Duration getShutdownQuietPeriod();
300300

301-
int getShutdownTimeout();
301+
Duration getShutdownTimeout();
302302

303303
Map<ChannelOption<Object>, Object> getChannelOptions();
304304

client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClientConfig.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
134134
private final Duration connectTimeout;
135135
private final Duration requestTimeout;
136136
private final Duration readTimeout;
137-
private final int shutdownQuietPeriod;
138-
private final int shutdownTimeout;
137+
private final Duration shutdownQuietPeriod;
138+
private final Duration shutdownTimeout;
139139

140140
// keep-alive
141141
private final boolean keepAlive;
142-
private final int pooledConnectionIdleTimeout;
143-
private final int connectionPoolCleanerPeriod;
144-
private final int connectionTtl;
142+
private final Duration pooledConnectionIdleTimeout;
143+
private final Duration connectionPoolCleanerPeriod;
144+
private final Duration connectionTtl;
145145
private final int maxConnections;
146146
private final int maxConnectionsPerHost;
147147
private final int acquireFreeChannelTimeout;
@@ -219,14 +219,14 @@ private DefaultAsyncHttpClientConfig(// http
219219
Duration connectTimeout,
220220
Duration requestTimeout,
221221
Duration readTimeout,
222-
int shutdownQuietPeriod,
223-
int shutdownTimeout,
222+
Duration shutdownQuietPeriod,
223+
Duration shutdownTimeout,
224224

225225
// keep-alive
226226
boolean keepAlive,
227-
int pooledConnectionIdleTimeout,
228-
int connectionPoolCleanerPeriod,
229-
int connectionTtl,
227+
Duration pooledConnectionIdleTimeout,
228+
Duration connectionPoolCleanerPeriod,
229+
Duration connectionTtl,
230230
int maxConnections,
231231
int maxConnectionsPerHost,
232232
int acquireFreeChannelTimeout,
@@ -488,12 +488,12 @@ public Duration getReadTimeout() {
488488
}
489489

490490
@Override
491-
public int getShutdownQuietPeriod() {
491+
public Duration getShutdownQuietPeriod() {
492492
return shutdownQuietPeriod;
493493
}
494494

495495
@Override
496-
public int getShutdownTimeout() {
496+
public Duration getShutdownTimeout() {
497497
return shutdownTimeout;
498498
}
499499

@@ -504,17 +504,17 @@ public boolean isKeepAlive() {
504504
}
505505

506506
@Override
507-
public int getPooledConnectionIdleTimeout() {
507+
public Duration getPooledConnectionIdleTimeout() {
508508
return pooledConnectionIdleTimeout;
509509
}
510510

511511
@Override
512-
public int getConnectionPoolCleanerPeriod() {
512+
public Duration getConnectionPoolCleanerPeriod() {
513513
return connectionPoolCleanerPeriod;
514514
}
515515

516516
@Override
517-
public int getConnectionTtl() {
517+
public Duration getConnectionTtl() {
518518
return connectionTtl;
519519
}
520520

@@ -799,14 +799,14 @@ public static class Builder {
799799
private Duration connectTimeout = defaultConnectTimeout();
800800
private Duration requestTimeout = defaultRequestTimeout();
801801
private Duration readTimeout = defaultReadTimeout();
802-
private int shutdownQuietPeriod = defaultShutdownQuietPeriod();
803-
private int shutdownTimeout = defaultShutdownTimeout();
802+
private Duration shutdownQuietPeriod = defaultShutdownQuietPeriod();
803+
private Duration shutdownTimeout = defaultShutdownTimeout();
804804

805805
// keep-alive
806806
private boolean keepAlive = defaultKeepAlive();
807-
private int pooledConnectionIdleTimeout = defaultPooledConnectionIdleTimeout();
808-
private int connectionPoolCleanerPeriod = defaultConnectionPoolCleanerPeriod();
809-
private int connectionTtl = defaultConnectionTtl();
807+
private Duration pooledConnectionIdleTimeout = defaultPooledConnectionIdleTimeout();
808+
private Duration connectionPoolCleanerPeriod = defaultConnectionPoolCleanerPeriod();
809+
private Duration connectionTtl = defaultConnectionTtl();
810810
private int maxConnections = defaultMaxConnections();
811811
private int maxConnectionsPerHost = defaultMaxConnectionsPerHost();
812812
private int acquireFreeChannelTimeout = defaultAcquireFreeChannelTimeout();
@@ -1074,12 +1074,12 @@ public Builder setReadTimeout(Duration readTimeout) {
10741074
return this;
10751075
}
10761076

1077-
public Builder setShutdownQuietPeriod(int shutdownQuietPeriod) {
1077+
public Builder setShutdownQuietPeriod(Duration shutdownQuietPeriod) {
10781078
this.shutdownQuietPeriod = shutdownQuietPeriod;
10791079
return this;
10801080
}
10811081

1082-
public Builder setShutdownTimeout(int shutdownTimeout) {
1082+
public Builder setShutdownTimeout(Duration shutdownTimeout) {
10831083
this.shutdownTimeout = shutdownTimeout;
10841084
return this;
10851085
}
@@ -1090,17 +1090,17 @@ public Builder setKeepAlive(boolean keepAlive) {
10901090
return this;
10911091
}
10921092

1093-
public Builder setPooledConnectionIdleTimeout(int pooledConnectionIdleTimeout) {
1093+
public Builder setPooledConnectionIdleTimeout(Duration pooledConnectionIdleTimeout) {
10941094
this.pooledConnectionIdleTimeout = pooledConnectionIdleTimeout;
10951095
return this;
10961096
}
10971097

1098-
public Builder setConnectionPoolCleanerPeriod(int connectionPoolCleanerPeriod) {
1098+
public Builder setConnectionPoolCleanerPeriod(Duration connectionPoolCleanerPeriod) {
10991099
this.connectionPoolCleanerPeriod = connectionPoolCleanerPeriod;
11001100
return this;
11011101
}
11021102

1103-
public Builder setConnectionTtl(int connectionTtl) {
1103+
public Builder setConnectionTtl(Duration connectionTtl) {
11041104
this.connectionTtl = connectionTtl;
11051105
return this;
11061106
}

client/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfigDefaults.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ public static Duration defaultConnectTimeout() {
115115
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getDuration(ASYNC_CLIENT_CONFIG_ROOT + CONNECTION_TIMEOUT_CONFIG);
116116
}
117117

118-
public static int defaultPooledConnectionIdleTimeout() {
119-
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + POOLED_CONNECTION_IDLE_TIMEOUT_CONFIG);
118+
public static Duration defaultPooledConnectionIdleTimeout() {
119+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getDuration(ASYNC_CLIENT_CONFIG_ROOT + POOLED_CONNECTION_IDLE_TIMEOUT_CONFIG);
120120
}
121121

122-
public static int defaultConnectionPoolCleanerPeriod() {
123-
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + CONNECTION_POOL_CLEANER_PERIOD_CONFIG);
122+
public static Duration defaultConnectionPoolCleanerPeriod() {
123+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getDuration(ASYNC_CLIENT_CONFIG_ROOT + CONNECTION_POOL_CLEANER_PERIOD_CONFIG);
124124
}
125125

126126
public static Duration defaultReadTimeout() {
@@ -131,8 +131,8 @@ public static Duration defaultRequestTimeout() {
131131
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getDuration(ASYNC_CLIENT_CONFIG_ROOT + REQUEST_TIMEOUT_CONFIG);
132132
}
133133

134-
public static int defaultConnectionTtl() {
135-
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + CONNECTION_TTL_CONFIG);
134+
public static Duration defaultConnectionTtl() {
135+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getDuration(ASYNC_CLIENT_CONFIG_ROOT + CONNECTION_TTL_CONFIG);
136136
}
137137

138138
public static boolean defaultFollowRedirect() {
@@ -287,12 +287,12 @@ public static boolean defaultKeepEncodingHeader() {
287287
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + KEEP_ENCODING_HEADER_CONFIG);
288288
}
289289

290-
public static int defaultShutdownQuietPeriod() {
291-
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + SHUTDOWN_QUIET_PERIOD_CONFIG);
290+
public static Duration defaultShutdownQuietPeriod() {
291+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getDuration(ASYNC_CLIENT_CONFIG_ROOT + SHUTDOWN_QUIET_PERIOD_CONFIG);
292292
}
293293

294-
public static int defaultShutdownTimeout() {
295-
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + SHUTDOWN_TIMEOUT_CONFIG);
294+
public static Duration defaultShutdownTimeout() {
295+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getDuration(ASYNC_CLIENT_CONFIG_ROOT + SHUTDOWN_TIMEOUT_CONFIG);
296296
}
297297

298298
public static boolean defaultUseNativeTransport() {

client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,10 @@ private void doClose() {
332332

333333
public void close() {
334334
if (allowReleaseEventLoopGroup) {
335+
final long shutdownQuietPeriod = config.getShutdownQuietPeriod().toMillis();
336+
final long shutdownTimeout = config.getShutdownTimeout().toMillis();
335337
eventLoopGroup
336-
.shutdownGracefully(config.getShutdownQuietPeriod(), config.getShutdownTimeout(), TimeUnit.MILLISECONDS)
338+
.shutdownGracefully(shutdownQuietPeriod, shutdownTimeout, TimeUnit.MILLISECONDS)
337339
.addListener(future -> doClose());
338340
} else {
339341
doClose();

client/src/main/java/org/asynchttpclient/netty/channel/DefaultChannelPool.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.slf4j.LoggerFactory;
2828

2929
import java.net.InetSocketAddress;
30+
import java.time.Duration;
3031
import java.util.ArrayList;
3132
import java.util.Collections;
3233
import java.util.Deque;
@@ -55,9 +56,9 @@ public final class DefaultChannelPool implements ChannelPool {
5556
private final ConcurrentHashMap<Object, ConcurrentLinkedDeque<IdleChannel>> partitions = new ConcurrentHashMap<>();
5657
private final AtomicBoolean isClosed = new AtomicBoolean(false);
5758
private final Timer nettyTimer;
58-
private final int connectionTtl;
59+
private final long connectionTtl;
5960
private final boolean connectionTtlEnabled;
60-
private final int maxIdleTime;
61+
private final long maxIdleTime;
6162
private final boolean maxIdleTimeEnabled;
6263
private final long cleanerPeriod;
6364
private final PoolLeaseStrategy poolLeaseStrategy;
@@ -69,20 +70,23 @@ public DefaultChannelPool(AsyncHttpClientConfig config, Timer hashedWheelTimer)
6970
config.getConnectionPoolCleanerPeriod());
7071
}
7172

72-
public DefaultChannelPool(int maxIdleTime, int connectionTtl, Timer nettyTimer, int cleanerPeriod) {
73+
public DefaultChannelPool(Duration maxIdleTime, Duration connectionTtl, Timer nettyTimer, Duration cleanerPeriod) {
7374
this(maxIdleTime, connectionTtl, PoolLeaseStrategy.LIFO, nettyTimer, cleanerPeriod);
7475
}
7576

76-
public DefaultChannelPool(int maxIdleTime, int connectionTtl, PoolLeaseStrategy poolLeaseStrategy, Timer nettyTimer, int cleanerPeriod) {
77-
this.maxIdleTime = maxIdleTime;
78-
this.connectionTtl = connectionTtl;
79-
connectionTtlEnabled = connectionTtl > 0;
77+
public DefaultChannelPool(Duration maxIdleTime, Duration connectionTtl, PoolLeaseStrategy poolLeaseStrategy, Timer nettyTimer, Duration cleanerPeriod) {
78+
final long maxIdleTimeInMs = maxIdleTime.toMillis();
79+
final long connectionTtlInMs = connectionTtl.toMillis();
80+
final long cleanerPeriodInMs = cleanerPeriod.toMillis();
81+
this.maxIdleTime = maxIdleTimeInMs;
82+
this.connectionTtl = connectionTtlInMs;
83+
connectionTtlEnabled = connectionTtlInMs > 0;
8084
this.nettyTimer = nettyTimer;
81-
maxIdleTimeEnabled = maxIdleTime > 0;
85+
maxIdleTimeEnabled = maxIdleTimeInMs > 0;
8286
this.poolLeaseStrategy = poolLeaseStrategy;
8387

84-
this.cleanerPeriod = Math.min(cleanerPeriod, Math.min(connectionTtlEnabled ? connectionTtl : Integer.MAX_VALUE,
85-
maxIdleTimeEnabled ? maxIdleTime : Integer.MAX_VALUE));
88+
this.cleanerPeriod = Math.min(cleanerPeriodInMs, Math.min(connectionTtlEnabled ? connectionTtlInMs : Integer.MAX_VALUE,
89+
maxIdleTimeEnabled ? maxIdleTimeInMs : Integer.MAX_VALUE));
8690

8791
if (connectionTtlEnabled || maxIdleTimeEnabled) {
8892
scheduleNewIdleChannelDetector(new IdleChannelDetector());

client/src/main/resources/org/asynchttpclient/config/ahc-default.properties

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ org.asynchttpclient.maxConnections=-1
33
org.asynchttpclient.maxConnectionsPerHost=-1
44
org.asynchttpclient.acquireFreeChannelTimeout=0
55
org.asynchttpclient.connectTimeout=PT5S
6-
org.asynchttpclient.pooledConnectionIdleTimeout=60000
7-
org.asynchttpclient.connectionPoolCleanerPeriod=100
6+
org.asynchttpclient.pooledConnectionIdleTimeout=PT1M
7+
org.asynchttpclient.connectionPoolCleanerPeriod=PT0.1S
88
org.asynchttpclient.readTimeout=PT1M
99
org.asynchttpclient.requestTimeout=PT1M
10-
org.asynchttpclient.connectionTtl=-1
10+
org.asynchttpclient.connectionTtl=-PT0.001S
1111
org.asynchttpclient.followRedirect=false
1212
org.asynchttpclient.maxRedirects=5
1313
org.asynchttpclient.compressionEnforced=false
@@ -46,8 +46,8 @@ org.asynchttpclient.chunkedFileChunkSize=8192
4646
org.asynchttpclient.webSocketMaxBufferSize=128000000
4747
org.asynchttpclient.webSocketMaxFrameSize=10240
4848
org.asynchttpclient.keepEncodingHeader=false
49-
org.asynchttpclient.shutdownQuietPeriod=2000
50-
org.asynchttpclient.shutdownTimeout=15000
49+
org.asynchttpclient.shutdownQuietPeriod=PT2S
50+
org.asynchttpclient.shutdownTimeout=PT15S
5151
org.asynchttpclient.useNativeTransport=false
5252
org.asynchttpclient.useOnlyEpollNativeTransport=false
5353
org.asynchttpclient.ioThreadsCount=-1

client/src/test/java/org/asynchttpclient/AsyncHttpClientDefaultsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public void testDefaultConnectTimeOut() {
5656

5757
@RepeatedIfExceptionsTest(repeats = 5)
5858
public void testDefaultPooledConnectionIdleTimeout() {
59-
assertEquals(AsyncHttpClientConfigDefaults.defaultPooledConnectionIdleTimeout(), 60 * 1000);
60-
testIntegerSystemProperty("pooledConnectionIdleTimeout", "defaultPooledConnectionIdleTimeout", "100");
59+
assertEquals(AsyncHttpClientConfigDefaults.defaultPooledConnectionIdleTimeout(), Duration.ofMinutes(1));
60+
testDurationSystemProperty("pooledConnectionIdleTimeout", "defaultPooledConnectionIdleTimeout", "PT0.1S");
6161
}
6262

6363
@RepeatedIfExceptionsTest(repeats = 5)
@@ -74,8 +74,8 @@ public void testDefaultRequestTimeout() {
7474

7575
@RepeatedIfExceptionsTest(repeats = 5)
7676
public void testDefaultConnectionTtl() {
77-
assertEquals(AsyncHttpClientConfigDefaults.defaultConnectionTtl(), -1);
78-
testIntegerSystemProperty("connectionTtl", "defaultConnectionTtl", "100");
77+
assertEquals(AsyncHttpClientConfigDefaults.defaultConnectionTtl(), Duration.ofMillis(-1));
78+
testDurationSystemProperty("connectionTtl", "defaultConnectionTtl", "PT0.1S");
7979
}
8080

8181
@RepeatedIfExceptionsTest(repeats = 5)

client/src/test/java/org/asynchttpclient/ClientStatsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.github.artsok.RepeatedIfExceptionsTest;
1919

20+
import java.time.Duration;
2021
import java.util.List;
2122
import java.util.stream.Collectors;
2223
import java.util.stream.Stream;
@@ -35,7 +36,7 @@ public class ClientStatsTest extends AbstractBasicTest {
3536

3637
@RepeatedIfExceptionsTest(repeats = 5)
3738
public void testClientStatus() throws Throwable {
38-
try (final AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setPooledConnectionIdleTimeout(5000))) {
39+
try (final AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setPooledConnectionIdleTimeout(Duration.ofSeconds(5)))) {
3940
final String url = getTargetUrl();
4041

4142
final ClientStats emptyStats = client.getClientStats();
@@ -114,7 +115,7 @@ public void testClientStatus() throws Throwable {
114115

115116
@RepeatedIfExceptionsTest(repeats = 5)
116117
public void testClientStatusNoKeepalive() throws Throwable {
117-
try (final AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(false).setPooledConnectionIdleTimeout(1000))) {
118+
try (final AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(false).setPooledConnectionIdleTimeout(Duration.ofSeconds(1)))) {
118119
final String url = getTargetUrl();
119120

120121
final ClientStats emptyStats = client.getClientStats();

client/src/test/java/org/asynchttpclient/IdleStateHandlerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.jupiter.api.BeforeEach;
2727

2828
import java.io.IOException;
29+
import java.time.Duration;
2930
import java.util.concurrent.ExecutionException;
3031

3132
import static org.asynchttpclient.Dsl.asyncHttpClient;
@@ -48,7 +49,7 @@ public void setUpGlobal() throws Exception {
4849

4950
@RepeatedIfExceptionsTest(repeats = 5)
5051
public void idleStateTest() throws Exception {
51-
try (AsyncHttpClient c = asyncHttpClient(config().setPooledConnectionIdleTimeout(10 * 1000))) {
52+
try (AsyncHttpClient c = asyncHttpClient(config().setPooledConnectionIdleTimeout(Duration.ofSeconds(10)))) {
5253
c.prepareGet(getTargetUrl()).execute().get();
5354
} catch (ExecutionException e) {
5455
fail("Should allow to finish processing request.", e);

client/src/test/java/org/asynchttpclient/NoNullResponseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void multipleSslRequestsWithDelayAndKeepAlive() throws Exception {
3333
.setFollowRedirect(true)
3434
.setKeepAlive(true)
3535
.setConnectTimeout(Duration.ofSeconds(10))
36-
.setPooledConnectionIdleTimeout(60000)
36+
.setPooledConnectionIdleTimeout(Duration.ofMinutes(1))
3737
.setRequestTimeout(Duration.ofSeconds(10))
3838
.setMaxConnectionsPerHost(-1)
3939
.setMaxConnections(-1)

0 commit comments

Comments
 (0)