Skip to content

Commit 5b0cdc5

Browse files
committed
Add pinned channel entry
1 parent 02cbc5c commit 5b0cdc5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
public class ChannelManager {
7070

7171
private static final Logger LOGGER = LoggerFactory.getLogger(ChannelManager.class);
72+
public static final String PINNED_ENTRY = "entry";
7273
public static final String HTTP_CLIENT_CODEC = "http";
7374
public static final String SSL_HANDLER = "ssl";
7475
public static final String DEFLATER_HANDLER = "deflater";
@@ -232,7 +233,7 @@ private Class<? extends Channel> getEpollSocketChannelClass() {
232233
throw new IllegalArgumentException(e);
233234
}
234235
}
235-
236+
236237
public void configureBootstraps(NettyRequestSender requestSender) {
237238

238239
HttpProtocol httpProtocol = new HttpProtocol(this, config, requestSender);
@@ -241,10 +242,13 @@ public void configureBootstraps(NettyRequestSender requestSender) {
241242
WebSocketProtocol wsProtocol = new WebSocketProtocol(this, config, requestSender);
242243
wsHandler = new AsyncHttpClientHandler(config, this, requestSender, wsProtocol);
243244

245+
final NoopHandler pinnedEntry = new NoopHandler();
246+
244247
httpBootstrap.handler(new ChannelInitializer<Channel>() {
245248
@Override
246249
protected void initChannel(Channel ch) throws Exception {
247250
ch.pipeline()//
251+
.addLast(PINNED_ENTRY, pinnedEntry)//
248252
.addLast(HTTP_CLIENT_CODEC, newHttpClientCodec())//
249253
.addLast(INFLATER_HANDLER, newHttpContentDecompressor())//
250254
.addLast(CHUNKED_WRITER_HANDLER, new ChunkedWriteHandler())//
@@ -261,6 +265,7 @@ protected void initChannel(Channel ch) throws Exception {
261265
@Override
262266
protected void initChannel(Channel ch) throws Exception {
263267
ch.pipeline()//
268+
.addLast(PINNED_ENTRY, pinnedEntry)//
264269
.addLast(HTTP_CLIENT_CODEC, newHttpClientCodec())//
265270
.addLast(AHC_WS_HANDLER, wsHandler);
266271

@@ -411,12 +416,12 @@ public void upgradeProtocol(ChannelPipeline pipeline, Uri requestUri) throws SSL
411416
if (isSslHandlerConfigured(pipeline)) {
412417
pipeline.addAfter(SSL_HANDLER, HTTP_CLIENT_CODEC, newHttpClientCodec());
413418
} else {
414-
pipeline.addFirst(HTTP_CLIENT_CODEC, newHttpClientCodec());
415-
pipeline.addFirst(SSL_HANDLER, createSslHandler(requestUri.getHost(), requestUri.getExplicitPort()));
419+
pipeline.addAfter(PINNED_ENTRY, HTTP_CLIENT_CODEC, newHttpClientCodec());
420+
pipeline.addAfter(PINNED_ENTRY, SSL_HANDLER, createSslHandler(requestUri.getHost(), requestUri.getExplicitPort()));
416421
}
417422

418423
else
419-
pipeline.addFirst(HTTP_CLIENT_CODEC, newHttpClientCodec());
424+
pipeline.addAfter(PINNED_ENTRY, HTTP_CLIENT_CODEC, newHttpClientCodec());
420425

421426
if (requestUri.isWebSocket()) {
422427
pipeline.addAfter(AHC_HTTP_HANDLER, AHC_WS_HANDLER, wsHandler);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2015 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package org.asynchttpclient.netty.channel;
15+
16+
import io.netty.channel.ChannelHandlerAdapter;
17+
import io.netty.channel.ChannelHandler.Sharable;
18+
19+
/**
20+
* A noop handler that just serves as a pinned reference for adding and removing handlers in the pipeline
21+
*/
22+
@Sharable
23+
public class NoopHandler extends ChannelHandlerAdapter {
24+
}

0 commit comments

Comments
 (0)