Skip to content

Commit 7a69311

Browse files
authored
AsyncHttpClient#1686 configure accuracy in HashedTimerWheel (AsyncHttpClient#1705)
1 parent fe0fe2f commit 7a69311

File tree

8 files changed

+79
-7
lines changed

8 files changed

+79
-7
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,16 @@ public interface AsyncHttpClientConfig {
306306

307307
Timer getNettyTimer();
308308

309+
/**
310+
* @return the duration between tick of {@link io.netty.util.HashedWheelTimer}
311+
*/
312+
long getHashedWheelTimerTickDuration();
313+
314+
/**
315+
* @return the size of the hashed wheel {@link io.netty.util.HashedWheelTimer}
316+
*/
317+
int getHashedWheelTimerSize();
318+
309319
KeepAliveStrategy getKeepAliveStrategy();
310320

311321
boolean isValidateResponseHeaders();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import java.util.List;
3535
import java.util.concurrent.ThreadFactory;
36+
import java.util.concurrent.TimeUnit;
3637
import java.util.concurrent.atomic.AtomicBoolean;
3738
import java.util.function.Predicate;
3839

@@ -93,8 +94,7 @@ public DefaultAsyncHttpClient(AsyncHttpClientConfig config) {
9394

9495
private Timer newNettyTimer(AsyncHttpClientConfig config) {
9596
ThreadFactory threadFactory = config.getThreadFactory() != null ? config.getThreadFactory() : new DefaultThreadFactory(config.getThreadPoolName() + "-timer");
96-
97-
HashedWheelTimer timer = new HashedWheelTimer(threadFactory);
97+
HashedWheelTimer timer = new HashedWheelTimer(threadFactory, config.getHashedWheelTimerTickDuration(), TimeUnit.MILLISECONDS, config.getHashedWheelTimerSize());
9898
timer.start();
9999
return timer;
100100
}

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
133133
private final Consumer<Channel> wsAdditionalChannelInitializer;
134134
private final ResponseBodyPartFactory responseBodyPartFactory;
135135
private final int ioThreadsCount;
136+
private final long hashedWheelTimerTickDuration;
137+
private final int hashedWheelTimerSize;
136138

137139
private DefaultAsyncHttpClientConfig(// http
138140
boolean followRedirect,
@@ -217,7 +219,9 @@ private DefaultAsyncHttpClientConfig(// http
217219
Consumer<Channel> httpAdditionalChannelInitializer,
218220
Consumer<Channel> wsAdditionalChannelInitializer,
219221
ResponseBodyPartFactory responseBodyPartFactory,
220-
int ioThreadsCount) {
222+
int ioThreadsCount,
223+
long hashedWheelTimerTickDuration,
224+
int hashedWheelTimerSize) {
221225

222226
// http
223227
this.followRedirect = followRedirect;
@@ -305,6 +309,8 @@ private DefaultAsyncHttpClientConfig(// http
305309
this.wsAdditionalChannelInitializer = wsAdditionalChannelInitializer;
306310
this.responseBodyPartFactory = responseBodyPartFactory;
307311
this.ioThreadsCount = ioThreadsCount;
312+
this.hashedWheelTimerTickDuration = hashedWheelTimerTickDuration;
313+
this.hashedWheelTimerSize = hashedWheelTimerSize;
308314
}
309315

310316
@Override
@@ -639,6 +645,16 @@ public Timer getNettyTimer() {
639645
return nettyTimer;
640646
}
641647

648+
@Override
649+
public long getHashedWheelTimerTickDuration() {
650+
return hashedWheelTimerTickDuration;
651+
}
652+
653+
@Override
654+
public int getHashedWheelTimerSize() {
655+
return hashedWheelTimerSize;
656+
}
657+
642658
@Override
643659
public ThreadFactory getThreadFactory() {
644660
return threadFactory;
@@ -756,6 +772,8 @@ public static class Builder {
756772
private Consumer<Channel> wsAdditionalChannelInitializer;
757773
private ResponseBodyPartFactory responseBodyPartFactory = ResponseBodyPartFactory.EAGER;
758774
private int ioThreadsCount = defaultIoThreadsCount();
775+
private long hashedWheelTickDuration = defaultHashedWheelTimerTickDuration();
776+
private int hashedWheelSize = defaultHashedWheelTimerSize();
759777

760778
public Builder() {
761779
}
@@ -838,6 +856,8 @@ public Builder(AsyncHttpClientConfig config) {
838856
wsAdditionalChannelInitializer = config.getWsAdditionalChannelInitializer();
839857
responseBodyPartFactory = config.getResponseBodyPartFactory();
840858
ioThreadsCount = config.getIoThreadsCount();
859+
hashedWheelTickDuration = config.getHashedWheelTimerTickDuration();
860+
hashedWheelSize = config.getHashedWheelTimerSize();
841861
}
842862

843863
// http
@@ -1188,6 +1208,16 @@ public Builder setChunkedFileChunkSize(int chunkedFileChunkSize) {
11881208
return this;
11891209
}
11901210

1211+
public Builder setHashedWheelTickDuration(long hashedWheelTickDuration) {
1212+
this.hashedWheelTickDuration = hashedWheelTickDuration;
1213+
return this;
1214+
}
1215+
1216+
public Builder setHashedWheelSize(int hashedWheelSize) {
1217+
this.hashedWheelSize = hashedWheelSize;
1218+
return this;
1219+
}
1220+
11911221
@SuppressWarnings("unchecked")
11921222
public <T> Builder addChannelOption(ChannelOption<T> name, T value) {
11931223
channelOptions.put((ChannelOption<Object>) name, value);
@@ -1323,7 +1353,9 @@ public DefaultAsyncHttpClientConfig build() {
13231353
httpAdditionalChannelInitializer,
13241354
wsAdditionalChannelInitializer,
13251355
responseBodyPartFactory,
1326-
ioThreadsCount);
1356+
ioThreadsCount,
1357+
hashedWheelTickDuration,
1358+
hashedWheelSize);
13271359
}
13281360
}
13291361
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public final class AsyncHttpClientConfigDefaults {
7171
public static final String SHUTDOWN_TIMEOUT_CONFIG = "shutdownTimeout";
7272
public static final String USE_NATIVE_TRANSPORT_CONFIG = "useNativeTransport";
7373
public static final String IO_THREADS_COUNT_CONFIG = "ioThreadsCount";
74+
public static final String HASHED_WHEEL_TIMER_TICK_DURATION = "hashedWheelTimerTickDuration";
75+
public static final String HASHED_WHEEL_TIMER_SIZE = "hashedWheelTimerSize";
7476

7577
public static final String AHC_VERSION;
7678

@@ -294,4 +296,12 @@ public static boolean defaultUseNativeTransport() {
294296
public static int defaultIoThreadsCount() {
295297
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + IO_THREADS_COUNT_CONFIG);
296298
}
299+
300+
public static int defaultHashedWheelTimerTickDuration() {
301+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + HASHED_WHEEL_TIMER_TICK_DURATION);
302+
}
303+
304+
public static int defaultHashedWheelTimerSize() {
305+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + HASHED_WHEEL_TIMER_SIZE);
306+
}
297307
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ org.asynchttpclient.shutdownQuietPeriod=2000
5050
org.asynchttpclient.shutdownTimeout=15000
5151
org.asynchttpclient.useNativeTransport=false
5252
org.asynchttpclient.ioThreadsCount=0
53+
org.asynchttpclient.hashedWheelTimerTickDuration=100
54+
org.asynchttpclient.hashedWheelTimerSize=512

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ public void testDefaultUseInsecureTrustManager() {
115115
testBooleanSystemProperty("useInsecureTrustManager", "defaultUseInsecureTrustManager", "false");
116116
}
117117

118+
public void testDefaultHashedWheelTimerTickDuration() {
119+
Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultHashedWheelTimerTickDuration(), 100);
120+
testIntegerSystemProperty("hashedWheelTimerTickDuration", "defaultHashedWheelTimerTickDuration", "100");
121+
}
122+
123+
public void testDefaultHashedWheelTimerSize() {
124+
Assert.assertEquals(AsyncHttpClientConfigDefaults.defaultHashedWheelTimerSize(), 512);
125+
testIntegerSystemProperty("hashedWheelTimerSize", "defaultHashedWheelTimerSize", "512");
126+
}
127+
118128
private void testIntegerSystemProperty(String propertyName, String methodName, String value) {
119129
String previous = System.getProperty(ASYNC_CLIENT_CONFIG_ROOT + propertyName);
120130
System.setProperty(ASYNC_CLIENT_CONFIG_ROOT + propertyName, value);

client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ public void testGetCompleteServicePrincipalName() throws Exception {
135135
null,
136136
null,
137137
null);
138-
// FIXME see https://github.com/AsyncHttpClient/async-http-client/issues/1706
139-
// InetAddress.getByName("localhost").getCanonicalHostName() returns 127.0.0.1 with JDK8 and localhost with JDK11.
140-
// Assert.assertNotEquals("HTTP@localhost", spnegoEngine.getCompleteServicePrincipalName("localhost"));
138+
Assert.assertNotEquals("HTTP@localhost", spnegoEngine.getCompleteServicePrincipalName("localhost"));
141139
Assert.assertTrue(spnegoEngine.getCompleteServicePrincipalName("localhost").startsWith("HTTP@"));
142140
}
143141
{

extras/typesafeconfig/src/main/java/org/asynchttpclient/extras/typesafeconfig/AsyncHttpClientTypesafeConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ public Timer getNettyTimer() {
339339
return null;
340340
}
341341

342+
@Override
343+
public long getHashedWheelTimerTickDuration() {
344+
return getIntegerOpt(HASHED_WHEEL_TIMER_TICK_DURATION).orElse(defaultHashedWheelTimerTickDuration());
345+
}
346+
347+
@Override
348+
public int getHashedWheelTimerSize() {
349+
return getIntegerOpt(HASHED_WHEEL_TIMER_SIZE).orElse(defaultHashedWheelTimerSize());
350+
}
351+
342352
@Override
343353
public KeepAliveStrategy getKeepAliveStrategy() {
344354
return new DefaultKeepAliveStrategy();

0 commit comments

Comments
 (0)