-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
NettyConnectListener.java
118 lines (98 loc) · 4.54 KB
/
NettyConnectListener.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
/*
* 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.providers.netty3.request;
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getBaseUrl;
import java.net.ConnectException;
import java.nio.channels.ClosedChannelException;
import org.asynchttpclient.AsyncHandlerExtensions;
import org.asynchttpclient.providers.netty.commons.future.StackTraceInspector;
import org.asynchttpclient.providers.netty3.channel.ChannelManager;
import org.asynchttpclient.providers.netty3.channel.Channels;
import org.asynchttpclient.providers.netty3.future.NettyResponseFuture;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Non Blocking connect.
*/
public final class NettyConnectListener<T> implements ChannelFutureListener {
private static final Logger LOGGER = LoggerFactory.getLogger(NettyConnectListener.class);
private final NettyResponseFuture<T> future;
private final NettyRequestSender requestSender;
private final ChannelManager channelManager;
private final boolean channelPreempted;
private final String poolKey;
public NettyConnectListener(NettyResponseFuture<T> future,//
NettyRequestSender requestSender,//
ChannelManager channelManager,//
boolean channelPreempted,//
String poolKey) {
this.future = future;
this.requestSender = requestSender;
this.channelManager = channelManager;
this.channelPreempted = channelPreempted;
this.poolKey = poolKey;
}
public NettyResponseFuture<T> future() {
return future;
}
private void abortChannelPreemption(String poolKey) {
if (channelPreempted)
channelManager.abortChannelPreemption(poolKey);
}
private void writeRequest(Channel channel, String poolKey) {
LOGGER.debug("Request using non cached Channel '{}':\n{}\n", channel, future.getNettyRequest().getHttpRequest());
if (future.isDone()) {
abortChannelPreemption(poolKey);
return;
}
future.attachChannel(channel, false);
if (future.getAsyncHandler() instanceof AsyncHandlerExtensions)
AsyncHandlerExtensions.class.cast(future.getAsyncHandler()).onConnectionOpen();
channelManager.registerOpenChannel(channel);
requestSender.writeRequest(future, channel);
}
private void onFutureSuccess(final Channel channel) throws ConnectException {
Channels.setAttribute(channel, future);
writeRequest(channel, poolKey);
}
private void onFutureFailure(Channel channel, Throwable cause) {
abortChannelPreemption(poolKey);
boolean canRetry = future.canRetry();
LOGGER.debug("Trying to recover from failing to connect channel {} with a retry value of {} ", channel, canRetry);
if (canRetry
&& cause != null
&& (cause instanceof ClosedChannelException || future.getState() != NettyResponseFuture.STATE.NEW || StackTraceInspector.recoverOnNetty3DisconnectException(cause))) {
if (!requestSender.retry(future))
return;
}
LOGGER.debug("Failed to recover from connect exception: {} with channel {}", cause, channel);
boolean printCause = cause != null && cause.getMessage() != null;
String printedCause = printCause ? cause.getMessage() : getBaseUrl(future.getUri());
ConnectException e = new ConnectException(printedCause);
if (cause != null) {
e.initCause(cause);
}
future.abort(e);
}
public final void operationComplete(ChannelFuture f) throws Exception {
Channel channel = f.getChannel();
if (f.isSuccess())
onFutureSuccess(channel);
else
onFutureFailure(channel, f.getCause());
}
}