|
13 | 13 | package org.asynchttpclient.netty.request;
|
14 | 14 |
|
15 | 15 | import static org.asynchttpclient.handler.AsyncHandlerExtensionsUtils.toAsyncHandlerExtensions;
|
16 |
| -import static org.asynchttpclient.util.Assertions.assertNotNull; |
17 | 16 | import io.netty.bootstrap.Bootstrap;
|
18 | 17 | import io.netty.channel.Channel;
|
19 |
| -import io.netty.channel.ChannelFuture; |
20 | 18 |
|
21 | 19 | import java.net.InetAddress;
|
22 | 20 | import java.net.InetSocketAddress;
|
|
29 | 27 | import org.asynchttpclient.netty.SimpleChannelFutureListener;
|
30 | 28 | import org.asynchttpclient.netty.channel.Channels;
|
31 | 29 | import org.asynchttpclient.netty.channel.NettyConnectListener;
|
32 |
| -import org.asynchttpclient.netty.timeout.TimeoutsHolder; |
33 | 30 | import org.slf4j.Logger;
|
34 | 31 | import org.slf4j.LoggerFactory;
|
35 | 32 |
|
36 | 33 | public class NettyChannelConnector {
|
37 |
| - |
| 34 | + |
38 | 35 | private static final Logger LOGGER = LoggerFactory.getLogger(NettyChannelConnector.class);
|
39 | 36 |
|
40 | 37 | private final AsyncHandlerExtensions asyncHandlerExtensions;
|
41 | 38 | private final InetSocketAddress localAddress;
|
42 | 39 | private final List<InetSocketAddress> remoteAddresses;
|
43 |
| - private final TimeoutsHolder timeoutsHolder; |
44 | 40 | private final AsyncHttpClientState clientState;
|
45 | 41 | private final boolean connectionTtlEnabled;
|
46 | 42 | private volatile int i = 0;
|
47 | 43 |
|
48 | 44 | public NettyChannelConnector(InetAddress localAddress,//
|
49 | 45 | List<InetSocketAddress> remoteAddresses,//
|
50 | 46 | AsyncHandler<?> asyncHandler,//
|
51 |
| - TimeoutsHolder timeoutsHolder,// |
52 | 47 | AsyncHttpClientState clientState,//
|
53 | 48 | AsyncHttpClientConfig config) {
|
54 | 49 | this.localAddress = localAddress != null ? new InetSocketAddress(localAddress, 0) : null;
|
55 | 50 | this.remoteAddresses = remoteAddresses;
|
56 | 51 | this.asyncHandlerExtensions = toAsyncHandlerExtensions(asyncHandler);
|
57 |
| - this.timeoutsHolder = assertNotNull(timeoutsHolder, "timeoutsHolder"); |
58 | 52 | this.clientState = clientState;
|
59 | 53 | this.connectionTtlEnabled = config.getConnectionTtl() > 0;
|
60 | 54 | }
|
@@ -83,32 +77,31 @@ public void connect(final Bootstrap bootstrap, final NettyConnectListener<?> con
|
83 | 77 | }
|
84 | 78 |
|
85 | 79 | private void connect0(Bootstrap bootstrap, final NettyConnectListener<?> connectListener, InetSocketAddress remoteAddress) {
|
86 |
| - final ChannelFuture future = bootstrap.connect(remoteAddress, localAddress); |
87 | 80 |
|
88 |
| - future.addListener(new SimpleChannelFutureListener() { |
| 81 | + bootstrap.connect(remoteAddress, localAddress)// |
| 82 | + .addListener(new SimpleChannelFutureListener() { |
89 | 83 |
|
90 |
| - @Override |
91 |
| - public void onSuccess(Channel channel) { |
92 |
| - if (asyncHandlerExtensions != null) { |
93 |
| - asyncHandlerExtensions.onTcpConnectSuccess(remoteAddress, future.channel()); |
94 |
| - } |
95 |
| - timeoutsHolder.initRemoteAddress(remoteAddress); |
96 |
| - if (connectionTtlEnabled) { |
97 |
| - Channels.initChannelId(channel); |
98 |
| - } |
99 |
| - connectListener.onSuccess(channel); |
100 |
| - } |
| 84 | + @Override |
| 85 | + public void onSuccess(Channel channel) { |
| 86 | + if (asyncHandlerExtensions != null) { |
| 87 | + asyncHandlerExtensions.onTcpConnectSuccess(remoteAddress, channel); |
| 88 | + } |
| 89 | + if (connectionTtlEnabled) { |
| 90 | + Channels.initChannelId(channel); |
| 91 | + } |
| 92 | + connectListener.onSuccess(channel, remoteAddress); |
| 93 | + } |
101 | 94 |
|
102 |
| - @Override |
103 |
| - public void onFailure(Channel channel, Throwable t) { |
104 |
| - if (asyncHandlerExtensions != null) |
105 |
| - asyncHandlerExtensions.onTcpConnectFailure(remoteAddress, t); |
106 |
| - boolean retry = pickNextRemoteAddress(); |
107 |
| - if (retry) |
108 |
| - NettyChannelConnector.this.connect(bootstrap, connectListener); |
109 |
| - else |
110 |
| - connectListener.onFailure(channel, t); |
111 |
| - } |
112 |
| - }); |
| 95 | + @Override |
| 96 | + public void onFailure(Channel channel, Throwable t) { |
| 97 | + if (asyncHandlerExtensions != null) |
| 98 | + asyncHandlerExtensions.onTcpConnectFailure(remoteAddress, t); |
| 99 | + boolean retry = pickNextRemoteAddress(); |
| 100 | + if (retry) |
| 101 | + NettyChannelConnector.this.connect(bootstrap, connectListener); |
| 102 | + else |
| 103 | + connectListener.onFailure(channel, t); |
| 104 | + } |
| 105 | + }); |
113 | 106 | }
|
114 | 107 | }
|
0 commit comments