Skip to content

Commit 1a60826

Browse files
committed
Fix for #328.
- Introduce async connect mode configuration option within AsyncHttpClientConfig. Defaults to false meaning connections will be made synchronously. - Removed Netty-specific version of async connect mode configuration option. - Updated both providers to respect new configuration option.
1 parent 638e2fa commit 1a60826

File tree

5 files changed

+62
-32
lines changed

5 files changed

+62
-32
lines changed

Diff for: api/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

+45-12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public class AsyncHttpClientConfig {
114114
protected int spdyInitialWindowSize;
115115
protected int spdyMaxConcurrentStreams;
116116
protected boolean rfc6265CookieEncoding;
117+
protected boolean asyncConnectMode;
117118

118119
protected AsyncHttpClientConfig() {
119120
}
@@ -153,7 +154,8 @@ private AsyncHttpClientConfig(int maxTotalConnections,
153154
boolean spdyEnabled,
154155
int spdyInitialWindowSize,
155156
int spdyMaxConcurrentStreams,
156-
boolean rfc6265CookieEncoding) {
157+
boolean rfc6265CookieEncoding,
158+
boolean asyncConnectMode) {
157159

158160
this.maxTotalConnections = maxTotalConnections;
159161
this.maxConnectionPerHost = maxConnectionPerHost;
@@ -197,6 +199,7 @@ private AsyncHttpClientConfig(int maxTotalConnections,
197199
this.spdyInitialWindowSize = spdyInitialWindowSize;
198200
this.spdyMaxConcurrentStreams = spdyMaxConcurrentStreams;
199201
this.rfc6265CookieEncoding = rfc6265CookieEncoding;
202+
this.asyncConnectMode = asyncConnectMode;
200203
}
201204

202205
/**
@@ -567,6 +570,16 @@ public boolean isRfc6265CookieEncoding() {
567570
return rfc6265CookieEncoding;
568571
}
569572

573+
/**
574+
* @return <code>true</code> if the underlying provider should make new connections asynchronously or not. By default
575+
* new connections are made synchronously.
576+
*
577+
* @since 2.0.0
578+
*/
579+
public boolean isAsyncConnectMode() {
580+
return asyncConnectMode;
581+
}
582+
570583
/**
571584
* Builder for an {@link AsyncHttpClient}
572585
*/
@@ -609,6 +622,7 @@ public static class Builder {
609622
private int spdyInitialWindowSize = 10 * 1024 * 1024;
610623
private int spdyMaxConcurrentStreams = 100;
611624
private boolean rfc6265CookieEncoding;
625+
private boolean asyncConnectMode;
612626

613627
public Builder() {
614628
}
@@ -1121,6 +1135,21 @@ public Builder setRfc6265CookieEncoding(boolean rfc6265CookieEncoding) {
11211135
return this;
11221136
}
11231137

1138+
/**
1139+
* Configures how the underlying providers make new connections. By default,
1140+
* connections will be made synchronously.
1141+
*
1142+
* @param asyncConnectMode pass <code>true</code> to enable async connect mode.
1143+
*
1144+
* @return this
1145+
*
1146+
* @since 2.0.0
1147+
*/
1148+
public Builder setAsyncConnectMode(boolean asyncConnectMode) {
1149+
this.asyncConnectMode = asyncConnectMode;
1150+
return this;
1151+
}
1152+
11241153
/**
11251154
* Create a config builder with values taken from the given prototype configuration.
11261155
*
@@ -1166,6 +1195,7 @@ public Builder(AsyncHttpClientConfig prototype) {
11661195
strict302Handling = prototype.isStrict302Handling();
11671196
useRelativeURIsWithSSLProxies = prototype.isUseRelativeURIsWithSSLProxies();
11681197
rfc6265CookieEncoding = prototype.isRfc6265CookieEncoding();
1198+
asyncConnectMode = prototype.isAsyncConnectMode();
11691199
}
11701200

11711201
/**
@@ -1184,16 +1214,18 @@ public Thread newThread(Runnable r) {
11841214
}
11851215
});
11861216
}
1187-
1188-
if (applicationThreadPool == null) {
1189-
applicationThreadPool = Executors.newCachedThreadPool(new ThreadFactory() {
1190-
public Thread newThread(Runnable r) {
1191-
Thread t = new Thread(r, "AsyncHttpClient-Callback");
1192-
t.setDaemon(true);
1193-
return t;
1194-
}
1195-
});
1196-
}
1217+
1218+
if (applicationThreadPool == null) {
1219+
applicationThreadPool =
1220+
Executors.newCachedThreadPool(new ThreadFactory() {
1221+
public Thread newThread(Runnable r) {
1222+
Thread t = new Thread(r,
1223+
"AsyncHttpClient-Callback");
1224+
t.setDaemon(true);
1225+
return t;
1226+
}
1227+
});
1228+
}
11971229

11981230
if (applicationThreadPool.isShutdown()) {
11991231
throw new IllegalStateException("ExecutorServices closed");
@@ -1239,7 +1271,8 @@ public Thread newThread(Runnable r) {
12391271
spdyEnabled,
12401272
spdyInitialWindowSize,
12411273
spdyMaxConcurrentStreams,
1242-
rfc6265CookieEncoding);
1274+
rfc6265CookieEncoding,
1275+
asyncConnectMode);
12431276
}
12441277
}
12451278
}

Diff for: providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/ConnectionManager.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class ConnectionManager {
6161
private final ProxyAwareConnectorHandler connectionHandler;
6262
private final ConnectionMonitor connectionMonitor;
6363
private final GrizzlyAsyncHttpProvider provider;
64+
private final boolean connectAsync;
6465

6566

6667
// ------------------------------------------------------------ Constructors
@@ -87,25 +88,34 @@ public class ConnectionManager {
8788
pool = connectionPool;
8889
this.connectionHandler = connectionHandler;
8990
connectionMonitor = new ConnectionMonitor(config.getMaxTotalConnections());
90-
91+
connectAsync = provider.getClientConfig().isAsyncConnectMode();
9192

9293
}
9394

9495

9596
// ---------------------------------------------------------- Public Methods
9697

9798

98-
public void doAsyncTrackedConnection(final Request request,
99-
final GrizzlyResponseFuture requestFuture,
100-
final CompletionHandler<Connection> connectHandler)
99+
public void doTrackedConnection(final Request request,
100+
final GrizzlyResponseFuture requestFuture,
101+
final CompletionHandler<Connection> connectHandler)
101102
throws IOException {
102103
Connection c =
103104
pool.poll(getPoolKey(request, requestFuture.getProxyServer()));
104105
if (c == null) {
105106
if (!connectionMonitor.acquire()) {
106107
throw new IOException("Max connections exceeded");
107108
}
108-
doAsyncConnect(request, requestFuture, connectHandler);
109+
if (connectAsync) {
110+
doAsyncConnect(request, requestFuture, connectHandler);
111+
} else {
112+
try {
113+
c = obtainConnection0(request, requestFuture);
114+
connectHandler.completed(c);
115+
} catch (Exception e) {
116+
connectHandler.failed(e);
117+
}
118+
}
109119
} else {
110120
provider.touchConnection(c, request);
111121
connectHandler.completed(c);

Diff for: providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/GrizzlyAsyncHttpProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void updated(final Connection c) {
165165
}
166166
};
167167

168-
connectionManager.doAsyncTrackedConnection(request, future, connectHandler);
168+
connectionManager.doTrackedConnection(request, future, connectHandler);
169169

170170
return future;
171171
}

Diff for: providers/netty/src/main/java/org/asynchttpclient/providers/netty/NettyAsyncHttpProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ void configureNetty() {
260260
DefaultChannelFuture.setUseDeadLockChecker(false);
261261

262262
if (asyncHttpProviderConfig != null) {
263-
executeConnectAsync = asyncHttpProviderConfig.isAsyncConnect();
263+
executeConnectAsync = config.isAsyncConnectMode();
264264
if (!executeConnectAsync) {
265265
DefaultChannelFuture.setUseDeadLockChecker(true);
266266
}

Diff for: providers/netty/src/main/java/org/asynchttpclient/providers/netty/NettyAsyncHttpProviderConfig.java

-13
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ public class NettyAsyncHttpProviderConfig implements AsyncHttpProviderConfig<Str
4949
*/
5050
private ExecutorService bossExecutorService;
5151

52-
/**
53-
* Execute the connect operation asynchronously.
54-
*/
55-
private boolean asyncConnect;
56-
5752
/**
5853
* HttpClientCodec's maxInitialLineLength
5954
*/
@@ -163,14 +158,6 @@ public void setBossExecutorService(ExecutorService bossExecutorService) {
163158
this.bossExecutorService = bossExecutorService;
164159
}
165160

166-
public boolean isAsyncConnect() {
167-
return asyncConnect;
168-
}
169-
170-
public void setAsyncConnect(boolean asyncConnect) {
171-
this.asyncConnect = asyncConnect;
172-
}
173-
174161
public int getMaxInitialLineLength() {
175162
return maxInitialLineLength;
176163
}

0 commit comments

Comments
 (0)