Skip to content

Commit 33caaf4

Browse files
author
Stephane Landelle
committed
Use Netty Timer interface instead of HashedWheelTimer, close AsyncHttpClient#528
1 parent a94bd62 commit 33caaf4

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

providers/netty/src/main/java/org/asynchttpclient/providers/netty/NettyAsyncHttpProviderConfig.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import io.netty.channel.Channel;
2929
import io.netty.channel.ChannelOption;
3030
import io.netty.channel.EventLoopGroup;
31-
import io.netty.util.HashedWheelTimer;
31+
import io.netty.util.Timer;
3232

3333
import java.util.HashMap;
3434
import java.util.Map;
@@ -89,7 +89,7 @@ public class NettyAsyncHttpProviderConfig implements AsyncHttpProviderConfig<Str
8989

9090
private boolean disableZeroCopy;
9191

92-
private HashedWheelTimer hashedWheelTimer;
92+
private Timer nettyTimer;
9393

9494
private long handshakeTimeoutInMillis;
9595

@@ -235,12 +235,12 @@ public void setDisableZeroCopy(boolean disableZeroCopy) {
235235
this.disableZeroCopy = disableZeroCopy;
236236
}
237237

238-
public HashedWheelTimer getHashedWheelTimer() {
239-
return hashedWheelTimer;
238+
public Timer getNettyTimer() {
239+
return nettyTimer;
240240
}
241241

242-
public void setHashedWheelTimer(HashedWheelTimer hashedWheelTimer) {
243-
this.hashedWheelTimer = hashedWheelTimer;
242+
public void setNettyTimer(Timer nettyTimer) {
243+
this.nettyTimer = nettyTimer;
244244
}
245245

246246
public long getHandshakeTimeoutInMillis() {

providers/netty/src/main/java/org/asynchttpclient/providers/netty/channel/Channels.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import io.netty.util.AttributeKey;
5656
import io.netty.util.HashedWheelTimer;
5757
import io.netty.util.Timeout;
58+
import io.netty.util.Timer;
5859
import io.netty.util.TimerTask;
5960

6061
import javax.net.ssl.SSLEngine;
@@ -110,8 +111,8 @@ public boolean remove(Object o) {
110111
}
111112
};
112113

113-
private final boolean allowStopHashedWheelTimer;
114-
private final HashedWheelTimer hashedWheelTimer;
114+
private final boolean allowStopNettyTimer;
115+
private final Timer nettyTimer;
115116
private final long handshakeTimeoutInMillis;
116117

117118
private Processor wsProcessor;
@@ -131,9 +132,8 @@ public Channels(final AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
131132
eventLoopGroup = allowReleaseEventLoopGroup ? new NioEventLoopGroup() : nettyProviderConfig.getEventLoopGroup();
132133

133134
// check if external HashedWheelTimer is defined
134-
allowStopHashedWheelTimer = nettyProviderConfig.getHashedWheelTimer() == null;
135-
hashedWheelTimer = allowStopHashedWheelTimer ? new HashedWheelTimer() : nettyProviderConfig.getHashedWheelTimer();
136-
hashedWheelTimer.start();
135+
allowStopNettyTimer = nettyProviderConfig.getNettyTimer() == null;
136+
nettyTimer = allowStopNettyTimer ? newNettyTimer() : nettyProviderConfig.getNettyTimer();
137137
handshakeTimeoutInMillis = nettyProviderConfig.getHandshakeTimeoutInMillis();
138138

139139
if (!(eventLoopGroup instanceof NioEventLoopGroup))
@@ -147,7 +147,7 @@ public Channels(final AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
147147
ChannelPool cp = nettyProviderConfig.getChannelPool();
148148
if (cp == null) {
149149
if (config.getAllowPoolingConnection()) {
150-
cp = new DefaultChannelPool(config, hashedWheelTimer);
150+
cp = new DefaultChannelPool(config, nettyTimer);
151151
} else {
152152
cp = new NonChannelPool();
153153
}
@@ -195,6 +195,12 @@ public Channels(final AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
195195
secureWebSocketBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeOut);
196196
}
197197

198+
private Timer newNettyTimer() {
199+
HashedWheelTimer nettyTimer = new HashedWheelTimer();
200+
nettyTimer.start();
201+
return nettyTimer;
202+
}
203+
198204
private SSLEngine createSSLEngine() throws IOException, GeneralSecurityException {
199205
SSLEngine sslEngine = config.getSSLEngineFactory().newSSLEngine();
200206
if (sslEngine == null) {
@@ -299,8 +305,8 @@ public void close() {
299305
if (allowReleaseEventLoopGroup)
300306
eventLoopGroup.shutdownGracefully();
301307

302-
if (allowStopHashedWheelTimer)
303-
hashedWheelTimer.stop();
308+
if (allowStopNettyTimer)
309+
nettyTimer.stop();
304310
}
305311

306312
/**
@@ -475,7 +481,7 @@ public void abort(NettyResponseFuture<?> future, Throwable t) {
475481
}
476482

477483
public Timeout newTimeoutInMs(TimerTask task, long delayInMs) {
478-
return hashedWheelTimer.newTimeout(task, delayInMs, TimeUnit.MILLISECONDS);
484+
return nettyTimer.newTimeout(task, delayInMs, TimeUnit.MILLISECONDS);
479485
}
480486

481487
public static SslHandler getSslHandler(Channel channel) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import org.slf4j.LoggerFactory;
2222

2323
import io.netty.channel.Channel;
24-
import io.netty.util.HashedWheelTimer;
2524
import io.netty.util.Timeout;
25+
import io.netty.util.Timer;
2626
import io.netty.util.TimerTask;
2727

2828
import java.util.ArrayList;
@@ -43,20 +43,20 @@ public class DefaultChannelPool implements ChannelPool {
4343
private final ConcurrentHashMap<Channel, IdleChannel> channel2IdleChannel = new ConcurrentHashMap<Channel, IdleChannel>();
4444
private final ConcurrentHashMap<Channel, Long> channel2CreationDate = new ConcurrentHashMap<Channel, Long>();
4545
private final AtomicBoolean closed = new AtomicBoolean(false);
46-
private final HashedWheelTimer hashedWheelTimer;
46+
private final Timer nettyTimer;
4747
private final boolean sslConnectionPoolEnabled;
4848
private final int maxTotalConnections;
4949
private final int maxConnectionPerHost;
5050
private final int maxConnectionLifeTimeInMs;
5151
private final long maxIdleTime;
5252

53-
public DefaultChannelPool(AsyncHttpClientConfig config, HashedWheelTimer hashedWheelTimer) {
53+
public DefaultChannelPool(AsyncHttpClientConfig config, Timer nettyTimer) {
5454
this(config.getMaxTotalConnections(),//
5555
config.getMaxConnectionPerHost(),//
5656
config.getIdleConnectionInPoolTimeoutInMs(),//
5757
config.isSslConnectionPoolEnabled(),//
5858
config.getMaxConnectionLifeTimeInMs(),//
59-
hashedWheelTimer);
59+
nettyTimer);
6060
}
6161

6262
public DefaultChannelPool(//
@@ -65,18 +65,18 @@ public DefaultChannelPool(//
6565
long maxIdleTime,//
6666
boolean sslConnectionPoolEnabled,//
6767
int maxConnectionLifeTimeInMs,//
68-
HashedWheelTimer hashedWheelTimer) {
68+
Timer nettyTimer) {
6969
this.maxTotalConnections = maxTotalConnections;
7070
this.maxConnectionPerHost = maxConnectionPerHost;
7171
this.sslConnectionPoolEnabled = sslConnectionPoolEnabled;
7272
this.maxIdleTime = maxIdleTime;
7373
this.maxConnectionLifeTimeInMs = maxConnectionLifeTimeInMs;
74-
this.hashedWheelTimer = hashedWheelTimer;
74+
this.nettyTimer = nettyTimer;
7575
scheduleNewIdleChannelDetector(new IdleChannelDetector());
7676
}
7777

7878
private void scheduleNewIdleChannelDetector(TimerTask task) {
79-
this.hashedWheelTimer.newTimeout(task, maxIdleTime, TimeUnit.MILLISECONDS);
79+
nettyTimer.newTimeout(task, maxIdleTime, TimeUnit.MILLISECONDS);
8080
}
8181

8282
private static class IdleChannel {

0 commit comments

Comments
 (0)