@@ -86,39 +86,42 @@ static boolean isConnectionCacheable(final Connection c) {
86
86
return (canCache != null ) ? canCache : false ;
87
87
}
88
88
89
- void doAsyncTrackedConnection (final Request request ,
89
+ void getConnectionAsync (final Request request ,
90
90
final GrizzlyResponseFuture requestFuture ,
91
91
final CompletionHandler <Connection > connectHandler )
92
92
throws IOException , ExecutionException , InterruptedException {
93
93
94
- Connection c = pool .poll (getPartitionId (request , requestFuture .getProxy ()));
94
+ final ProxyServer proxy = ProxyUtils .getProxyServer (config , request );
95
+
96
+ Connection c = pool .poll (getPartitionId (request , proxy ));
95
97
if (c == null ) {
96
98
if (!connectionMonitor .acquire ()) {
97
99
throw new IOException ("Max connections exceeded" );
98
100
}
99
- doAsyncConnect (request , requestFuture , connectHandler );
101
+ openConnectionAsync (request , proxy , requestFuture , connectHandler );
100
102
} else {
101
103
provider .touchConnection (c , request );
102
104
connectHandler .completed (c );
103
105
}
104
106
}
105
107
106
- Connection obtainConnection (final Request request ,
108
+ Connection openConnectionSync (final Request request ,
107
109
final GrizzlyResponseFuture requestFuture )
108
110
throws IOException , ExecutionException ,
109
111
InterruptedException , TimeoutException {
110
112
111
- final Connection c = obtainConnection0 (request , requestFuture );
113
+ final Connection c = openConnectionSync0 (request ,
114
+ ProxyUtils .getProxyServer (config , request ), requestFuture );
112
115
DO_NOT_CACHE .set (c , Boolean .TRUE );
113
116
return c ;
114
117
}
115
118
116
- void doAsyncConnect (final Request request ,
119
+ private void openConnectionAsync (final Request request ,
120
+ final ProxyServer proxy ,
117
121
final GrizzlyResponseFuture requestFuture ,
118
122
final CompletionHandler <Connection > connectHandler )
119
123
throws IOException , ExecutionException , InterruptedException {
120
124
121
- ProxyServer proxy = requestFuture .getProxy ();
122
125
final Uri uri = request .getUri ();
123
126
String host = (proxy != null ) ? proxy .getHost () : uri .getHost ();
124
127
int port = (proxy != null ) ? proxy .getPort () : uri .getPort ();
@@ -132,40 +135,33 @@ void doAsyncConnect(final Request request,
132
135
HostnameVerifierListener .wrapWithHostnameVerifierHandler (
133
136
completionHandler , verifier , uri .getHost ());
134
137
}
135
- if (request .getLocalAddress () != null ) {
136
- connectionHandler .connect (
137
- new InetSocketAddress (host , getPort (uri , port )),
138
- new InetSocketAddress (request .getLocalAddress (), 0 ),
139
- completionHandler );
140
- } else {
141
- connectionHandler .connect (
142
- new InetSocketAddress (host , getPort (uri , port )),
143
- completionHandler );
144
- }
138
+
139
+ connectionHandler .connect (
140
+ new InetSocketAddress (host , getPort (uri , port )),
141
+ new InetSocketAddress (request .getLocalAddress (), 0 ),
142
+ completionHandler );
145
143
}
146
144
147
- private Connection obtainConnection0 (final Request request ,
145
+ private Connection openConnectionSync0 (final Request request ,
146
+ final ProxyServer proxy ,
148
147
final GrizzlyResponseFuture requestFuture )
149
148
throws IOException , ExecutionException ,
150
149
InterruptedException , TimeoutException {
151
150
152
151
final Uri uri = request .getUri ();
153
- final ProxyServer proxy = requestFuture .getProxy ();
154
152
String host = (proxy != null ) ? proxy .getHost () : uri .getHost ();
155
153
int port = (proxy != null ) ? proxy .getPort () : uri .getPort ();
156
154
int cTimeout = config .getConnectTimeout ();
157
155
FutureImpl <Connection > future = Futures .createSafeFuture ();
158
156
CompletionHandler <Connection > ch = Futures .toCompletionHandler (future ,
159
157
createConnectionCompletionHandler (request , requestFuture , null ));
160
- if (cTimeout > 0 ) {
161
- connectionHandler .connect (
162
- new InetSocketAddress (host , getPort (uri , port )), ch );
163
- return future .get (cTimeout , TimeUnit .MILLISECONDS );
164
- } else {
165
- connectionHandler .connect (
166
- new InetSocketAddress (host , getPort (uri , port )), ch );
167
- return future .get ();
168
- }
158
+
159
+ connectionHandler .connect (
160
+ new InetSocketAddress (host , getPort (uri , port )), ch );
161
+
162
+ return cTimeout > 0
163
+ ? future .get (cTimeout , TimeUnit .MILLISECONDS )
164
+ : future .get ();
169
165
}
170
166
171
167
boolean returnConnection (final Request request , final Connection c ) {
@@ -188,7 +184,7 @@ void destroy() {
188
184
pool .destroy ();
189
185
}
190
186
191
- CompletionHandler <Connection > createConnectionCompletionHandler (
187
+ private CompletionHandler <Connection > createConnectionCompletionHandler (
192
188
final Request request , final GrizzlyResponseFuture future ,
193
189
final CompletionHandler <Connection > wrappedHandler ) {
194
190
@@ -201,16 +197,15 @@ public void cancelled() {
201
197
}
202
198
}
203
199
204
- public void failed (Throwable throwable ) {
200
+ public void failed (final Throwable throwable ) {
205
201
if (wrappedHandler != null ) {
206
202
wrappedHandler .failed (throwable );
207
203
} else {
208
204
future .abort (throwable );
209
205
}
210
206
}
211
207
212
- public void completed (Connection connection ) {
213
- future .setConnection (connection );
208
+ public void completed (final Connection connection ) {
214
209
provider .touchConnection (connection , request );
215
210
if (wrappedHandler != null ) {
216
211
connection .addCloseListener (connectionMonitor );
0 commit comments