-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathChannelManager.java
executable file
·520 lines (437 loc) · 20.4 KB
/
ChannelManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient.netty.channel;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.ChannelGroupFuture;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.kqueue.KQueueEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;
import io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder;
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.proxy.ProxyHandler;
import io.netty.handler.proxy.Socks4ProxyHandler;
import io.netty.handler.proxy.Socks5ProxyHandler;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.resolver.NameResolver;
import io.netty.util.Timer;
import io.netty.util.concurrent.*;
import io.netty.util.internal.PlatformDependent;
import org.asynchttpclient.*;
import org.asynchttpclient.channel.ChannelPool;
import org.asynchttpclient.channel.ChannelPoolPartitioning;
import org.asynchttpclient.channel.NoopChannelPool;
import org.asynchttpclient.netty.NettyResponseFuture;
import org.asynchttpclient.netty.OnLastHttpContentCallback;
import org.asynchttpclient.netty.handler.AsyncHttpClientHandler;
import org.asynchttpclient.netty.handler.HttpHandler;
import org.asynchttpclient.netty.handler.WebSocketHandler;
import org.asynchttpclient.netty.request.NettyRequestSender;
import org.asynchttpclient.netty.ssl.DefaultSslEngineFactory;
import org.asynchttpclient.proxy.ProxyServer;
import org.asynchttpclient.proxy.ProxyType;
import org.asynchttpclient.uri.Uri;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ChannelManager {
public static final String HTTP_CLIENT_CODEC = "http";
public static final String SSL_HANDLER = "ssl";
public static final String SSL_TUNNEL_HANDLER = "ssl-tunnel";
public static final String SOCKS_HANDLER = "socks";
public static final String INFLATER_HANDLER = "inflater";
public static final String CHUNKED_WRITER_HANDLER = "chunked-writer";
public static final String WS_DECODER_HANDLER = "ws-decoder";
public static final String WS_FRAME_AGGREGATOR = "ws-aggregator";
public static final String WS_COMPRESSOR_HANDLER = "ws-compressor";
public static final String WS_ENCODER_HANDLER = "ws-encoder";
public static final String AHC_HTTP_HANDLER = "ahc-http";
public static final String AHC_WS_HANDLER = "ahc-ws";
public static final String LOGGING_HANDLER = "logging";
private static final Logger LOGGER = LoggerFactory.getLogger(ChannelManager.class);
private final AsyncHttpClientConfig config;
private final SslEngineFactory sslEngineFactory;
private final EventLoopGroup eventLoopGroup;
private final boolean allowReleaseEventLoopGroup;
private final Bootstrap httpBootstrap;
private final Bootstrap wsBootstrap;
private final long handshakeTimeout;
private final ChannelPool channelPool;
private final ChannelGroup openChannels;
private AsyncHttpClientHandler wsHandler;
public ChannelManager(final AsyncHttpClientConfig config, Timer nettyTimer) {
this.config = config;
this.sslEngineFactory = config.getSslEngineFactory() != null ? config.getSslEngineFactory() : new DefaultSslEngineFactory();
try {
this.sslEngineFactory.init(config);
} catch (SSLException e) {
throw new RuntimeException("Could not initialize sslEngineFactory", e);
}
ChannelPool channelPool = config.getChannelPool();
if (channelPool == null) {
if (config.isKeepAlive()) {
channelPool = new DefaultChannelPool(config, nettyTimer);
} else {
channelPool = NoopChannelPool.INSTANCE;
}
}
this.channelPool = channelPool;
openChannels = new DefaultChannelGroup("asyncHttpClient", GlobalEventExecutor.INSTANCE);
handshakeTimeout = config.getHandshakeTimeout();
// check if external EventLoopGroup is defined
ThreadFactory threadFactory = config.getThreadFactory() != null ? config.getThreadFactory() : new DefaultThreadFactory(config.getThreadPoolName());
allowReleaseEventLoopGroup = config.getEventLoopGroup() == null;
TransportFactory<? extends Channel, ? extends EventLoopGroup> transportFactory;
if (allowReleaseEventLoopGroup) {
if (config.isUseNativeTransport()) {
transportFactory = getNativeTransportFactory();
} else {
transportFactory = NioTransportFactory.INSTANCE;
}
eventLoopGroup = transportFactory.newEventLoopGroup(config.getIoThreadsCount(), threadFactory);
} else {
eventLoopGroup = config.getEventLoopGroup();
if (eventLoopGroup instanceof NioEventLoopGroup) {
transportFactory = NioTransportFactory.INSTANCE;
} else if (eventLoopGroup instanceof EpollEventLoopGroup) {
transportFactory = new EpollTransportFactory();
} else if (eventLoopGroup instanceof KQueueEventLoopGroup) {
transportFactory = new KQueueTransportFactory();
} else {
throw new IllegalArgumentException("Unknown event loop group " + eventLoopGroup.getClass().getSimpleName());
}
}
httpBootstrap = newBootstrap(transportFactory, eventLoopGroup, config);
wsBootstrap = newBootstrap(transportFactory, eventLoopGroup, config);
// for reactive streams
httpBootstrap.option(ChannelOption.AUTO_READ, false);
}
public static boolean isSslHandlerConfigured(ChannelPipeline pipeline) {
return pipeline.get(SSL_HANDLER) != null;
}
public static boolean isSslTunnelHandlerConfigured(ChannelPipeline pipeline) {
return pipeline.get(SSL_TUNNEL_HANDLER) != null;
}
private Bootstrap newBootstrap(ChannelFactory<? extends Channel> channelFactory, EventLoopGroup eventLoopGroup, AsyncHttpClientConfig config) {
@SuppressWarnings("deprecation")
Bootstrap bootstrap = new Bootstrap().channelFactory(channelFactory).group(eventLoopGroup)
.option(ChannelOption.ALLOCATOR, config.getAllocator() != null ? config.getAllocator() : ByteBufAllocator.DEFAULT)
.option(ChannelOption.TCP_NODELAY, config.isTcpNoDelay())
.option(ChannelOption.SO_REUSEADDR, config.isSoReuseAddress())
.option(ChannelOption.SO_KEEPALIVE, config.isSoKeepAlive())
.option(ChannelOption.AUTO_CLOSE, false);
if (config.getConnectTimeout() > 0) {
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout());
}
if (config.getSoLinger() >= 0) {
bootstrap.option(ChannelOption.SO_LINGER, config.getSoLinger());
}
if (config.getSoSndBuf() >= 0) {
bootstrap.option(ChannelOption.SO_SNDBUF, config.getSoSndBuf());
}
if (config.getSoRcvBuf() >= 0) {
bootstrap.option(ChannelOption.SO_RCVBUF, config.getSoRcvBuf());
}
for (Entry<ChannelOption<Object>, Object> entry : config.getChannelOptions().entrySet()) {
bootstrap.option(entry.getKey(), entry.getValue());
}
return bootstrap;
}
@SuppressWarnings("unchecked")
private TransportFactory<? extends Channel, ? extends EventLoopGroup> getNativeTransportFactory() {
String nativeTransportFactoryClassName = null;
if (PlatformDependent.isOsx()) {
nativeTransportFactoryClassName = "org.asynchttpclient.netty.channel.KQueueTransportFactory";
} else if (!PlatformDependent.isWindows()) {
nativeTransportFactoryClassName = "org.asynchttpclient.netty.channel.EpollTransportFactory";
}
try {
if (nativeTransportFactoryClassName != null) {
return (TransportFactory<? extends Channel, ? extends EventLoopGroup>) Class.forName(nativeTransportFactoryClassName).newInstance();
}
} catch (Exception e) {
}
throw new IllegalArgumentException("No suitable native transport (epoll or kqueue) available");
}
public void configureBootstraps(NettyRequestSender requestSender) {
final AsyncHttpClientHandler httpHandler = new HttpHandler(config, this, requestSender);
wsHandler = new WebSocketHandler(config, this, requestSender);
final LoggingHandler loggingHandler = new LoggingHandler(LogLevel.TRACE);
httpBootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ChannelPipeline pipeline = ch.pipeline()
.addLast(HTTP_CLIENT_CODEC, newHttpClientCodec())
.addLast(INFLATER_HANDLER, newHttpContentDecompressor())
.addLast(CHUNKED_WRITER_HANDLER, new ChunkedWriteHandler())
.addLast(AHC_HTTP_HANDLER, httpHandler);
if (LOGGER.isTraceEnabled()) {
pipeline.addFirst(LOGGING_HANDLER, loggingHandler);
}
if (config.getHttpAdditionalChannelInitializer() != null)
config.getHttpAdditionalChannelInitializer().accept(ch);
}
});
wsBootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ChannelPipeline pipeline = ch.pipeline()
.addLast(HTTP_CLIENT_CODEC, newHttpClientCodec())
.addLast(AHC_WS_HANDLER, wsHandler);
if (config.isEnableWebSocketCompression()) {
pipeline.addBefore(AHC_WS_HANDLER, WS_COMPRESSOR_HANDLER, WebSocketClientCompressionHandler.INSTANCE);
}
if (LOGGER.isDebugEnabled()) {
pipeline.addFirst(LOGGING_HANDLER, loggingHandler);
}
if (config.getWsAdditionalChannelInitializer() != null)
config.getWsAdditionalChannelInitializer().accept(ch);
}
});
}
private HttpContentDecompressor newHttpContentDecompressor() {
if (config.isKeepEncodingHeader())
return new HttpContentDecompressor() {
@Override
protected String getTargetContentEncoding(String contentEncoding) {
return contentEncoding;
}
};
else
return new HttpContentDecompressor();
}
public final void tryToOfferChannelToPool(Channel channel, AsyncHandler<?> asyncHandler, boolean keepAlive, Object partitionKey) {
if (channel.isActive() && keepAlive) {
LOGGER.debug("Adding key: {} for channel {}", partitionKey, channel);
Channels.setDiscard(channel);
try {
asyncHandler.onConnectionOffer(channel);
} catch (Exception e) {
LOGGER.error("onConnectionOffer crashed", e);
}
if (!channelPool.offer(channel, partitionKey)) {
// rejected by pool
closeChannel(channel);
}
} else {
// not offered
closeChannel(channel);
}
}
public Channel poll(Uri uri, String virtualHost, ProxyServer proxy, ChannelPoolPartitioning connectionPoolPartitioning) {
Object partitionKey = connectionPoolPartitioning.getPartitionKey(uri, virtualHost, proxy);
return channelPool.poll(partitionKey);
}
public void removeAll(Channel connection) {
channelPool.removeAll(connection);
}
private void doClose() {
ChannelGroupFuture groupFuture = openChannels.close();
channelPool.destroy();
groupFuture.addListener(future -> sslEngineFactory.destroy());
}
public void close() {
if (allowReleaseEventLoopGroup) {
eventLoopGroup
.shutdownGracefully(config.getShutdownQuietPeriod(), config.getShutdownTimeout(), TimeUnit.MILLISECONDS)
.addListener(future -> doClose());
} else {
doClose();
}
}
public void closeChannel(Channel channel) {
LOGGER.debug("Closing Channel {} ", channel);
Channels.setDiscard(channel);
removeAll(channel);
Channels.silentlyCloseChannel(channel);
}
public void registerOpenChannel(Channel channel) {
openChannels.add(channel);
}
private HttpClientCodec newHttpClientCodec() {
return new HttpClientCodec(//
config.getHttpClientCodecMaxInitialLineLength(),
config.getHttpClientCodecMaxHeaderSize(),
config.getHttpClientCodecMaxChunkSize(),
false,
config.isValidateResponseHeaders(),
config.getHttpClientCodecInitialBufferSize());
}
private SslHandler createSslHandler(String peerHost, int peerPort) {
SSLEngine sslEngine = sslEngineFactory.newSslEngine(config, peerHost, peerPort);
SslHandler sslHandler = new SslHandler(sslEngine);
if (handshakeTimeout > 0)
sslHandler.setHandshakeTimeoutMillis(handshakeTimeout);
return sslHandler;
}
public Future<Channel> updatePipelineForHttpTunneling(ChannelPipeline pipeline, Uri requestUri, ProxyType proxyType) {
Future<Channel> whenHandshaked = null;
if (pipeline.get(HTTP_CLIENT_CODEC) != null)
pipeline.remove(HTTP_CLIENT_CODEC);
if (requestUri.isSecured()) {
if (!isSslHandlerConfigured(pipeline)) {
SslHandler sslHandler = createSslHandler(requestUri.getHost(), requestUri.getExplicitPort());
whenHandshaked = sslHandler.handshakeFuture();
pipeline.addBefore(INFLATER_HANDLER, SSL_HANDLER, sslHandler);
}
pipeline.addAfter(SSL_HANDLER, HTTP_CLIENT_CODEC, newHttpClientCodec());
if(ProxyType.HTTPS.equals(proxyType) && !isSslTunnelHandlerConfigured(pipeline)) {
SslHandler sslHandler = createSslHandler(requestUri.getHost(), requestUri.getExplicitPort());
whenHandshaked = sslHandler.handshakeFuture();
pipeline.addAfter(SSL_HANDLER, SSL_TUNNEL_HANDLER, sslHandler);
}
} else {
pipeline.addBefore(AHC_HTTP_HANDLER, HTTP_CLIENT_CODEC, newHttpClientCodec());
}
if (requestUri.isWebSocket()) {
pipeline.addAfter(AHC_HTTP_HANDLER, AHC_WS_HANDLER, wsHandler);
if (config.isEnableWebSocketCompression()) {
pipeline.addBefore(AHC_WS_HANDLER, WS_COMPRESSOR_HANDLER, WebSocketClientCompressionHandler.INSTANCE);
}
pipeline.remove(AHC_HTTP_HANDLER);
}
return whenHandshaked;
}
public SslHandler addSslHandler(ChannelPipeline pipeline, Uri uri, String virtualHost, boolean hasSocksProxyHandler) {
String peerHost;
int peerPort;
if (virtualHost != null) {
int i = virtualHost.indexOf(':');
if (i == -1) {
peerHost = virtualHost;
peerPort = uri.getSchemeDefaultPort();
} else {
peerHost = virtualHost.substring(0, i);
peerPort = Integer.valueOf(virtualHost.substring(i + 1));
}
} else {
peerHost = uri.getHost();
peerPort = uri.getExplicitPort();
}
SslHandler sslHandler = createSslHandler(peerHost, peerPort);
if (hasSocksProxyHandler) {
pipeline.addAfter(SOCKS_HANDLER, SSL_HANDLER, sslHandler);
} else {
pipeline.addFirst(SSL_HANDLER, sslHandler);
}
return sslHandler;
}
public Future<Bootstrap> getBootstrap(Uri uri, NameResolver<InetAddress> nameResolver, ProxyServer proxy) {
final Promise<Bootstrap> promise = ImmediateEventExecutor.INSTANCE.newPromise();
if (uri.isWebSocket() && proxy == null) {
return promise.setSuccess(wsBootstrap);
} else if (proxy != null && proxy.getProxyType().isSocks()) {
Bootstrap socksBootstrap = httpBootstrap.clone();
ChannelHandler httpBootstrapHandler = socksBootstrap.config().handler();
nameResolver.resolve(proxy.getHost()).addListener((Future<InetAddress> whenProxyAddress) -> {
if (whenProxyAddress.isSuccess()) {
socksBootstrap.handler(new ChannelInitializer<Channel>() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
httpBootstrapHandler.handlerAdded(ctx);
super.handlerAdded(ctx);
}
@Override
protected void initChannel(Channel channel) throws Exception {
InetSocketAddress proxyAddress = new InetSocketAddress(whenProxyAddress.get(), proxy.getPort());
Realm realm = proxy.getRealm();
String username = realm != null ? realm.getPrincipal() : null;
String password = realm != null ? realm.getPassword() : null;
ProxyHandler socksProxyHandler;
switch (proxy.getProxyType()) {
case SOCKS_V4:
socksProxyHandler = new Socks4ProxyHandler(proxyAddress, username);
break;
case SOCKS_V5:
socksProxyHandler = new Socks5ProxyHandler(proxyAddress, username, password);
break;
default:
throw new IllegalArgumentException("Only SOCKS4 and SOCKS5 supported at the moment.");
}
channel.pipeline().addFirst(SOCKS_HANDLER, socksProxyHandler);
}
});
promise.setSuccess(socksBootstrap);
} else {
promise.setFailure(whenProxyAddress.cause());
}
});
} else {
promise.setSuccess(httpBootstrap);
}
return promise;
}
public void upgradePipelineForWebSockets(ChannelPipeline pipeline) {
pipeline.addAfter(HTTP_CLIENT_CODEC, WS_ENCODER_HANDLER, new WebSocket08FrameEncoder(true));
pipeline.addAfter(WS_ENCODER_HANDLER, WS_DECODER_HANDLER, new WebSocket08FrameDecoder(false, config.isEnableWebSocketCompression(), config.getWebSocketMaxFrameSize()));
if (config.isAggregateWebSocketFrameFragments()) {
pipeline.addAfter(WS_DECODER_HANDLER, WS_FRAME_AGGREGATOR, new WebSocketFrameAggregator(config.getWebSocketMaxBufferSize()));
}
pipeline.remove(HTTP_CLIENT_CODEC);
}
private OnLastHttpContentCallback newDrainCallback(final NettyResponseFuture<?> future, final Channel channel, final boolean keepAlive, final Object partitionKey) {
return new OnLastHttpContentCallback(future) {
public void call() {
tryToOfferChannelToPool(channel, future.getAsyncHandler(), keepAlive, partitionKey);
}
};
}
public void drainChannelAndOffer(Channel channel, NettyResponseFuture<?> future) {
drainChannelAndOffer(channel, future, future.isKeepAlive(), future.getPartitionKey());
}
public void drainChannelAndOffer(Channel channel, NettyResponseFuture<?> future, boolean keepAlive, Object partitionKey) {
Channels.setAttribute(channel, newDrainCallback(future, channel, keepAlive, partitionKey));
}
public ChannelPool getChannelPool() {
return channelPool;
}
public EventLoopGroup getEventLoopGroup() {
return eventLoopGroup;
}
public ClientStats getClientStats() {
Map<String, Long> totalConnectionsPerHost = openChannels.stream().map(Channel::remoteAddress).filter(a -> a instanceof InetSocketAddress)
.map(a -> (InetSocketAddress) a).map(InetSocketAddress::getHostString).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Map<String, Long> idleConnectionsPerHost = channelPool.getIdleChannelCountPerHost();
Map<String, HostStats> statsPerHost = totalConnectionsPerHost.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> {
final long totalConnectionCount = entry.getValue();
final long idleConnectionCount = idleConnectionsPerHost.getOrDefault(entry.getKey(), 0L);
final long activeConnectionCount = totalConnectionCount - idleConnectionCount;
return new HostStats(activeConnectionCount, idleConnectionCount);
}));
return new ClientStats(statsPerHost);
}
public boolean isOpen() {
return channelPool.isOpen();
}
}