Skip to content

Commit ef301cc

Browse files
author
Stephane Landelle
committed
Use Netty sync() for handling sync connect
1 parent 2a71598 commit ef301cc

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/Channels.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ public Channels(final AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
169169
}
170170
}
171171

172-
// FIXME clean up
173-
plainBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectionTimeoutInMs());
174-
webSocketBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectionTimeoutInMs());
175-
secureBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectionTimeoutInMs());
176-
secureWebSocketBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectionTimeoutInMs());
172+
int timeOut = config.getConnectionTimeoutInMs() > 0 ? config.getConnectionTimeoutInMs() : Integer.MAX_VALUE;
173+
plainBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeOut);
174+
webSocketBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeOut);
175+
secureBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeOut);
176+
secureWebSocketBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeOut);
177177
}
178178

179179
private SSLEngine createSSLEngine() throws IOException, GeneralSecurityException {

providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/NettyConnectListener.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import java.net.URI;
2828
import java.nio.channels.ClosedChannelException;
2929

30-
import javax.net.ssl.HostnameVerifier;
31-
3230
import org.asynchttpclient.AsyncHandler;
3331
import org.asynchttpclient.AsyncHttpClientConfig;
3432
import org.asynchttpclient.ProxyServer;
@@ -60,24 +58,20 @@ public NettyResponseFuture<T> future() {
6058
return future;
6159
}
6260

63-
private void onFutureSuccess(final Channel channel) throws Exception {
61+
public void onFutureSuccess(final Channel channel) throws ConnectException {
6462
Channels.setDefaultAttribute(channel, future);
6563
SslHandler sslHandler = Channels.getSslHandler(channel);
6664

67-
if (sslHandler != null) {
68-
// FIXME done on connect or on every request?
69-
HostnameVerifier v = config.getHostnameVerifier();
70-
if (!v.verify(future.getURI().getHost(), sslHandler.engine().getSession())) {
71-
ConnectException exception = new ConnectException("HostnameVerifier exception.");
72-
future.abort(exception);
73-
throw exception;
74-
}
65+
if (sslHandler != null && !config.getHostnameVerifier().verify(future.getURI().getHost(), sslHandler.engine().getSession())) {
66+
ConnectException exception = new ConnectException("HostnameVerifier exception");
67+
future.abort(exception);
68+
throw exception;
7569
}
7670

7771
requestSender.writeRequest(channel, config, future);
7872
}
7973

80-
private void onFutureFailure(Channel channel, Throwable cause) throws Exception {
74+
public void onFutureFailure(Channel channel, Throwable cause) {
8175

8276
logger.debug("Trying to recover a dead cached channel {} with a retry value of {} ", channel, future.canRetry());
8377
if (future.canRetry() && cause != null

providers/netty4/src/main/java/org/asynchttpclient/providers/netty4/NettyRequestSender.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.io.RandomAccessFile;
23-
import java.net.ConnectException;
2423
import java.net.InetSocketAddress;
2524
import java.net.URI;
2625
import java.util.Map;
@@ -179,28 +178,29 @@ private ChannelFuture connect(Request request, URI uri, ProxyServer proxy, Boots
179178
}
180179

181180
private void performSyncConnect(ChannelFuture channelFuture, URI uri, boolean acquiredConnection, NettyConnectListener<?> cl, AsyncHandler<?> asyncHandler) throws IOException {
182-
// FIXME why not use bootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)?
183-
int timeOut = config.getConnectionTimeoutInMs() > 0 ? config.getConnectionTimeoutInMs() : Integer.MAX_VALUE;
184-
if (!channelFuture.awaitUninterruptibly(timeOut, TimeUnit.MILLISECONDS)) {
185-
if (acquiredConnection) {
186-
channels.releaseFreeConnections();
187-
}
188-
channelFuture.cancel(false);
189-
channels.abort(cl.future(), new ConnectException(String.format("Connect operation to %s timed out %s", uri, timeOut)));
190-
}
191181

192182
try {
193-
cl.operationComplete(channelFuture);
194-
} catch (Exception e) {
195-
if (acquiredConnection) {
183+
channelFuture.syncUninterruptibly();
184+
cl.onFutureSuccess(channelFuture.channel());
185+
186+
} catch (Throwable t) {
187+
if (t.getCause() != null)
188+
t = t.getCause();
189+
190+
IOException ioe = null;
191+
if (t instanceof IOException)
192+
ioe = IOException.class.cast(t);
193+
else
194+
ioe = new IOException(t.getMessage(), t);
195+
196+
if (acquiredConnection)
196197
channels.releaseFreeConnections();
197-
}
198-
IOException ioe = new IOException(e.getMessage());
199-
ioe.initCause(e);
198+
199+
channels.abort(cl.future(), ioe);
200200
try {
201201
asyncHandler.onThrowable(ioe);
202-
} catch (Throwable t) {
203-
LOGGER.warn("c.operationComplete()", t);
202+
} catch (Throwable t2) {
203+
LOGGER.warn("asyncHandler.onThrowable()", t2);
204204
}
205205
throw ioe;
206206
}

0 commit comments

Comments
 (0)