Skip to content

Commit fbd6554

Browse files
author
Stephane Landelle
committed
useCache is always true, see NoConnectionsPool
1 parent 159e577 commit fbd6554

File tree

5 files changed

+190
-212
lines changed

5 files changed

+190
-212
lines changed

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

Lines changed: 44 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.asynchttpclient.providers.netty4;
22

3-
import static org.asynchttpclient.providers.netty4.util.HttpUtil.HTTP;
4-
import static org.asynchttpclient.providers.netty4.util.HttpUtil.WEBSOCKET;
5-
import static org.asynchttpclient.providers.netty4.util.HttpUtil.isSecure;
3+
import static org.asynchttpclient.providers.netty4.util.HttpUtil.*;
64
import io.netty.bootstrap.Bootstrap;
75
import io.netty.channel.Channel;
86
import io.netty.channel.ChannelHandlerContext;
@@ -45,6 +43,7 @@
4543
import org.asynchttpclient.ConnectionsPool;
4644
import org.asynchttpclient.providers.netty4.future.NettyResponseFuture;
4745
import org.asynchttpclient.providers.netty4.pool.NettyConnectionsPool;
46+
import org.asynchttpclient.providers.netty4.pool.NonConnectionsPool;
4847
import org.asynchttpclient.providers.netty4.util.CleanupChannelGroup;
4948
import org.asynchttpclient.util.SslUtils;
5049
import org.slf4j.Logger;
@@ -91,8 +90,6 @@ public boolean remove(Object o) {
9190
}
9291
};
9392

94-
private NettyChannelHandler httpProcessor;
95-
9693
public Channels(final AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig asyncHttpProviderConfig) {
9794

9895
this.config = config;
@@ -177,21 +174,23 @@ public Channels(final AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
177174
webSocketBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectionTimeoutInMs());
178175
secureBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectionTimeoutInMs());
179176
secureWebSocketBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectionTimeoutInMs());
177+
}
180178

181-
// FIXME What was the meaning of this and what is it still a matter with
182-
// Netty4
183-
// DefaultChannelFuture.setUseDeadLockChecker(false);
179+
private SSLEngine createSSLEngine() throws IOException, GeneralSecurityException {
180+
SSLEngine sslEngine = config.getSSLEngineFactory().newSSLEngine();
181+
if (sslEngine == null) {
182+
sslEngine = SslUtils.getSSLEngine();
183+
}
184+
return sslEngine;
184185
}
185186

186187
public void configure(final NettyChannelHandler httpProcessor) {
187-
this.httpProcessor = httpProcessor;
188-
189-
ChannelInitializer<Channel> httpChannelInitializer = new ChannelInitializer<Channel>() {
190188

189+
plainBootstrap.handler(new ChannelInitializer<Channel>() {
191190
@Override
192191
protected void initChannel(Channel ch) throws Exception {
193192
ChannelPipeline pipeline = ch.pipeline()//
194-
.addLast(HTTP_HANDLER, newHttpClientCodec());
193+
.addLast(HTTP_HANDLER, newHttpClientCodec());
195194

196195
if (config.getRequestCompressionLevel() > 0) {
197196
pipeline.addLast(DEFLATER_HANDLER, new HttpContentCompressor(config.getRequestCompressionLevel()));
@@ -201,75 +200,41 @@ protected void initChannel(Channel ch) throws Exception {
201200
pipeline.addLast(INFLATER_HANDLER, new HttpContentDecompressor());
202201
}
203202
pipeline.addLast(CHUNKED_WRITER_HANDLER, new ChunkedWriteHandler())//
204-
.addLast(AHC_HANDLER, httpProcessor);
203+
.addLast(AHC_HANDLER, httpProcessor);
205204

206205
if (asyncHttpProviderConfig.getHttpAdditionalChannelInitializer() != null) {
207206
asyncHttpProviderConfig.getHttpAdditionalChannelInitializer().initChannel(ch);
208207
}
209208
}
210-
};
209+
});
211210

212-
ChannelInitializer<Channel> webSocketChannelInitializer = new ChannelInitializer<Channel>() {
211+
webSocketBootstrap.handler(new ChannelInitializer<Channel>() {
213212
@Override
214213
protected void initChannel(Channel ch) throws Exception {
215214
ch.pipeline()//
216-
.addLast(HTTP_DECODER_HANDLER, new HttpResponseDecoder())//
217-
.addLast(HTTP_ENCODER_HANDLER, new HttpRequestEncoder())//
218-
.addLast(AHC_HANDLER, httpProcessor);
215+
.addLast(HTTP_DECODER_HANDLER, new HttpResponseDecoder())//
216+
.addLast(HTTP_ENCODER_HANDLER, new HttpRequestEncoder())//
217+
.addLast(AHC_HANDLER, httpProcessor);
219218

220219
if (asyncHttpProviderConfig.getWsAdditionalChannelInitializer() != null) {
221220
asyncHttpProviderConfig.getWsAdditionalChannelInitializer().initChannel(ch);
222221
}
223222
}
224-
};
225-
226-
plainBootstrap.handler(httpChannelInitializer);
227-
webSocketBootstrap.handler(webSocketChannelInitializer);
228-
}
229-
230-
public Bootstrap getBootstrap(String url, boolean useSSl) {
231-
Bootstrap bootstrap = url.startsWith(WEBSOCKET) ? (useSSl ? secureWebSocketBootstrap : webSocketBootstrap) : (useSSl ? secureBootstrap : plainBootstrap);
232-
233-
return bootstrap;
234-
}
235-
236-
public void close() {
237-
connectionsPool.destroy();
238-
for (Channel channel : openChannels) {
239-
Object attribute = getDefaultAttribute(channel);
240-
if (attribute instanceof NettyResponseFuture<?>) {
241-
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute;
242-
future.setReaperFuture(null);
243-
}
244-
}
245-
openChannels.close();
246-
if (allowReleaseEventLoopGroup) {
247-
eventLoopGroup.shutdownGracefully();
248-
}
249-
}
250-
251-
void constructSSLPipeline(final NettyResponseFuture<?> future) {
223+
});
252224

253225
secureBootstrap.handler(new ChannelInitializer<Channel>() {
254226

255227
@Override
256228
protected void initChannel(Channel ch) throws Exception {
257-
ChannelPipeline pipeline = ch.pipeline();
258-
259-
try {
260-
pipeline.addLast(SSL_HANDLER, new SslHandler(createSSLEngine()));
261-
} catch (Throwable ex) {
262-
LOGGER.error("Channel {} could not add SslHandler {}", ch, ex);
263-
abort(future, ex);
264-
}
265-
266-
pipeline.addLast(HTTP_HANDLER, newHttpClientCodec());
229+
ChannelPipeline pipeline = ch.pipeline()//
230+
.addLast(SSL_HANDLER, new SslHandler(createSSLEngine()))//
231+
.addLast(HTTP_HANDLER, newHttpClientCodec());
267232

268233
if (config.isCompressionEnabled()) {
269234
pipeline.addLast(INFLATER_HANDLER, new HttpContentDecompressor());
270235
}
271236
pipeline.addLast(CHUNKED_WRITER_HANDLER, new ChunkedWriteHandler())//
272-
.addLast(AHC_HANDLER, httpProcessor);
237+
.addLast(AHC_HANDLER, httpProcessor);
273238

274239
if (asyncHttpProviderConfig.getHttpsAdditionalChannelInitializer() != null) {
275240
asyncHttpProviderConfig.getHttpsAdditionalChannelInitializer().initChannel(ch);
@@ -281,18 +246,11 @@ protected void initChannel(Channel ch) throws Exception {
281246

282247
@Override
283248
protected void initChannel(Channel ch) throws Exception {
284-
ChannelPipeline pipeline = ch.pipeline();
285-
286-
try {
287-
pipeline.addLast(SSL_HANDLER, new SslHandler(createSSLEngine()));
288-
} catch (Throwable ex) {
289-
LOGGER.error("Channel {} could not add SslHandler {}", ch, ex);
290-
abort(future, ex);
291-
}
292-
293-
pipeline.addLast(HTTP_DECODER_HANDLER, new HttpResponseDecoder())//
294-
.addLast(HTTP_ENCODER_HANDLER, new HttpRequestEncoder())//
295-
.addLast(AHC_HANDLER, httpProcessor);
249+
ch.pipeline()//
250+
.addLast(SSL_HANDLER, new SslHandler(createSSLEngine()))//
251+
.addLast(HTTP_DECODER_HANDLER, new HttpResponseDecoder())//
252+
.addLast(HTTP_ENCODER_HANDLER, new HttpRequestEncoder())//
253+
.addLast(AHC_HANDLER, httpProcessor);
296254

297255
if (asyncHttpProviderConfig.getWssAdditionalChannelInitializer() != null) {
298256
asyncHttpProviderConfig.getWssAdditionalChannelInitializer().initChannel(ch);
@@ -301,15 +259,26 @@ protected void initChannel(Channel ch) throws Exception {
301259
});
302260
}
303261

304-
private SSLEngine createSSLEngine() throws IOException, GeneralSecurityException {
305-
SSLEngine sslEngine = config.getSSLEngineFactory().newSSLEngine();
306-
if (sslEngine == null) {
307-
sslEngine = SslUtils.getSSLEngine();
262+
public Bootstrap getBootstrap(String url, boolean useSSl) {
263+
return url.startsWith(WEBSOCKET) ? (useSSl ? secureWebSocketBootstrap : webSocketBootstrap) : (useSSl ? secureBootstrap : plainBootstrap);
264+
}
265+
266+
public void close() {
267+
connectionsPool.destroy();
268+
for (Channel channel : openChannels) {
269+
Object attribute = getDefaultAttribute(channel);
270+
if (attribute instanceof NettyResponseFuture<?>) {
271+
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute;
272+
future.setReaperFuture(null);
273+
}
274+
}
275+
openChannels.close();
276+
if (allowReleaseEventLoopGroup) {
277+
eventLoopGroup.shutdownGracefully();
308278
}
309-
return sslEngine;
310279
}
311280

312-
// FIXME what for?
281+
// some servers can use the same port for HTTP and HTTPS
313282
public Channel verifyChannelPipeline(Channel channel, String scheme) throws IOException, GeneralSecurityException {
314283

315284
if (channel.pipeline().get(SSL_HANDLER) != null && HTTP.equalsIgnoreCase(scheme)) {
@@ -509,26 +478,4 @@ public static void setDefaultAttribute(Channel channel, Object o) {
509478
public static void setDefaultAttribute(ChannelHandlerContext ctx, Object o) {
510479
ctx.attr(DEFAULT_ATTRIBUTE).set(o);
511480
}
512-
513-
private static class NonConnectionsPool implements ConnectionsPool<String, Channel> {
514-
515-
public boolean offer(String uri, Channel connection) {
516-
return false;
517-
}
518-
519-
public Channel poll(String uri) {
520-
return null;
521-
}
522-
523-
public boolean removeAll(Channel connection) {
524-
return false;
525-
}
526-
527-
public boolean canCacheConnection() {
528-
return true;
529-
}
530-
531-
public void destroy() {
532-
}
533-
}
534481
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class NettyAsyncHttpProvider implements AsyncHttpProvider {
4141
private final Channels channels;
4242
private final NettyRequestSender requestSender;
4343
private final NettyChannelHandler channelHandler;
44-
private final boolean executeConnectAsync;
4544

4645
public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
4746

@@ -54,8 +53,6 @@ public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
5453
requestSender = new NettyRequestSender(closed, config, channels);
5554
channelHandler = new NettyChannelHandler(config, requestSender, channels, closed);
5655
channels.configure(channelHandler);
57-
58-
executeConnectAsync = asyncHttpProviderConfig.isAsyncConnect();
5956
}
6057

6158
@Override
@@ -82,6 +79,6 @@ public Response prepareResponse(final HttpResponseStatus status, final HttpRespo
8279

8380
@Override
8481
public <T> ListenableFuture<T> execute(Request request, final AsyncHandler<T> asyncHandler) throws IOException {
85-
return requestSender.doConnect(request, asyncHandler, null, true, executeConnectAsync, false);
82+
return requestSender.sendRequest(request, asyncHandler, null, asyncHttpProviderConfig.isAsyncConnect(), false);
8683
}
8784
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void call() throws Exception {
312312

313313
Request target = nBuilder.setUrl(newUrl).build();
314314
future.setRequest(target);
315-
requestSender.execute(target, future);
315+
requestSender.sendRequest(target, future);
316316
return true;
317317
}
318318
} else {
@@ -544,7 +544,7 @@ private boolean handleResponseAndExit(final ChannelHandlerContext ctx, final Net
544544
Callback callback = new Callback(future) {
545545
public void call() throws Exception {
546546
channels.drainChannel(ctx, future);
547-
requestSender.execute(builder.setHeaders(headers).setRealm(nr).build(), future);
547+
requestSender.sendRequest(builder.setHeaders(headers).setRealm(nr).build(), future);
548548
}
549549
};
550550

@@ -589,7 +589,7 @@ public void call() throws Exception {
589589

590590
future.setReuseChannel(true);
591591
future.setConnectAllowed(true);
592-
requestSender.execute(builder.setHeaders(headers).setRealm(newRealm).build(), future);
592+
requestSender.sendRequest(builder.setHeaders(headers).setRealm(newRealm).build(), future);
593593
return true;
594594
}
595595

@@ -609,7 +609,7 @@ public void call() throws Exception {
609609
}
610610
future.setReuseChannel(true);
611611
future.setConnectAllowed(false);
612-
requestSender.execute(builder.build(), future);
612+
requestSender.sendRequest(builder.build(), future);
613613
return true;
614614

615615
}

0 commit comments

Comments
 (0)