Skip to content

Commit c90cf86

Browse files
author
Stephane Landelle
committed
Have one central place for configuration defaults, close AsyncHttpClient#565
1 parent e1e8e34 commit c90cf86

21 files changed

+290
-187
lines changed

api/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

Lines changed: 84 additions & 118 deletions
Large diffs are not rendered by default.

api/src/main/java/org/asynchttpclient/AsyncHttpClientConfigBean.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
*/
1313
package org.asynchttpclient;
1414

15+
import static org.asynchttpclient.AsyncHttpClientConfigDefaults.*;
16+
1517
import org.asynchttpclient.filter.IOExceptionFilter;
1618
import org.asynchttpclient.filter.RequestFilter;
1719
import org.asynchttpclient.filter.ResponseFilter;
18-
import org.asynchttpclient.util.DefaultHostnameVerifier;
1920
import org.asynchttpclient.util.ProxyUtils;
2021

2122
import javax.net.ssl.HostnameVerifier;
2223
import javax.net.ssl.SSLContext;
23-
import javax.net.ssl.SSLSession;
2424

2525
import java.util.LinkedList;
2626
import java.util.concurrent.ExecutorService;
@@ -45,34 +45,36 @@ void configureFilters() {
4545
}
4646

4747
void configureDefaults() {
48-
maxTotalConnections = Integer.getInteger(ASYNC_CLIENT + "defaultMaxTotalConnections", -1);
49-
maxConnectionPerHost = Integer.getInteger(ASYNC_CLIENT + "defaultMaxConnectionsPerHost", -1);
50-
connectionTimeOutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultConnectionTimeoutInMS", 60 * 1000);
51-
idleConnectionInPoolTimeoutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultIdleConnectionInPoolTimeoutInMS", 60 * 1000);
52-
idleConnectionTimeoutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultIdleConnectionTimeoutInMS", 60 * 1000);
53-
requestTimeoutInMs = Integer.getInteger(ASYNC_CLIENT + "defaultRequestTimeoutInMS", 60 * 1000);
54-
maxConnectionLifeTimeInMs = Integer.getInteger(ASYNC_CLIENT + "defaultMaxConnectionLifeTimeInMs", -1);
55-
redirectEnabled = Boolean.getBoolean(ASYNC_CLIENT + "defaultRedirectsEnabled");
56-
maxDefaultRedirects = Integer.getInteger(ASYNC_CLIENT + "defaultMaxRedirects", 5);
57-
compressionEnabled = Boolean.getBoolean(ASYNC_CLIENT + "compressionEnabled");
58-
userAgent = System.getProperty(ASYNC_CLIENT + "userAgent", "AsyncHttpClient/" + AHC_VERSION);
59-
ioThreadMultiplier = Integer.getInteger(ASYNC_CLIENT + "ioThreadMultiplier", 2);
60-
61-
boolean useProxySelector = Boolean.getBoolean(ASYNC_CLIENT + "useProxySelector");
62-
boolean useProxyProperties = Boolean.getBoolean(ASYNC_CLIENT + "useProxyProperties");
63-
if (useProxySelector) {
48+
maxTotalConnections = defaultMaxTotalConnections();
49+
maxConnectionPerHost = defaultMaxConnectionPerHost();
50+
connectionTimeOutInMs = defaultConnectionTimeOutInMs();
51+
webSocketIdleTimeoutInMs = defaultWebSocketIdleTimeoutInMs();
52+
idleConnectionInPoolTimeoutInMs = defaultIdleConnectionInPoolTimeoutInMs();
53+
idleConnectionTimeoutInMs = defaultIdleConnectionTimeoutInMs();
54+
requestTimeoutInMs = defaultRequestTimeoutInMs();
55+
maxConnectionLifeTimeInMs = defaultMaxConnectionLifeTimeInMs();
56+
redirectEnabled = defaultRedirectEnabled();
57+
maxRedirects = defaultMaxRedirects();
58+
compressionEnabled = defaultCompressionEnabled();
59+
userAgent = defaultUserAgent();
60+
allowPoolingConnection = defaultAllowPoolingConnection();
61+
useRelativeURIsWithSSLProxies = defaultUseRelativeURIsWithSSLProxies();
62+
requestCompressionLevel = defaultRequestCompressionLevel();
63+
maxRequestRetry = defaultMaxRequestRetry();
64+
ioThreadMultiplier = defaultIoThreadMultiplier();
65+
allowSslConnectionPool = defaultAllowSslConnectionPool();
66+
useRawUrl = defaultUseRawUrl();
67+
removeQueryParamOnRedirect = defaultRemoveQueryParamOnRedirect();
68+
strict302Handling = defaultStrict302Handling();
69+
hostnameVerifier = defaultHostnameVerifier();
70+
spdyEnabled = defaultSpdyEnabled();
71+
spdyInitialWindowSize = defaultSpdyInitialWindowSize();
72+
spdyMaxConcurrentStreams = defaultSpdyMaxConcurrentStreams();
73+
if (defaultUseProxySelector()) {
6474
proxyServerSelector = ProxyUtils.getJdkDefaultProxyServerSelector();
65-
} else if (useProxyProperties) {
75+
} else if (defaultUseProxyProperties()) {
6676
proxyServerSelector = ProxyUtils.createProxyServerSelector(System.getProperties());
6777
}
68-
69-
allowPoolingConnection = true;
70-
requestCompressionLevel = -1;
71-
maxRequestRetry = 5;
72-
allowSslConnectionPool = true;
73-
useRawUrl = false;
74-
removeQueryParamOnRedirect = true;
75-
hostnameVerifier = new DefaultHostnameVerifier();
7678
}
7779

7880
void configureExecutors() {
@@ -125,8 +127,8 @@ public AsyncHttpClientConfigBean setRedirectEnabled(boolean redirectEnabled) {
125127
return this;
126128
}
127129

128-
public AsyncHttpClientConfigBean setMaxDefaultRedirects(int maxDefaultRedirects) {
129-
this.maxDefaultRedirects = maxDefaultRedirects;
130+
public AsyncHttpClientConfigBean setMaxRedirects(int maxRedirects) {
131+
this.maxRedirects = maxRedirects;
130132
return this;
131133
}
132134

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package org.asynchttpclient;
14+
15+
import static org.asynchttpclient.util.MiscUtil.getBoolean;
16+
17+
import org.asynchttpclient.util.DefaultHostnameVerifier;
18+
19+
import javax.net.ssl.HostnameVerifier;
20+
21+
public final class AsyncHttpClientConfigDefaults {
22+
23+
private AsyncHttpClientConfigDefaults() {
24+
}
25+
26+
public static final String ASYNC_CLIENT = AsyncHttpClientConfig.class.getName() + ".";
27+
28+
public static int defaultMaxTotalConnections() {
29+
return Integer.getInteger(ASYNC_CLIENT + "maxTotalConnections", -1);
30+
}
31+
32+
public static int defaultMaxConnectionPerHost() {
33+
return Integer.getInteger(ASYNC_CLIENT + "maxConnectionsPerHost", -1);
34+
}
35+
36+
public static int defaultConnectionTimeOutInMs() {
37+
return Integer.getInteger(ASYNC_CLIENT + "connectionTimeoutInMs", 60 * 1000);
38+
}
39+
40+
public static int defaultIdleConnectionInPoolTimeoutInMs() {
41+
return Integer.getInteger(ASYNC_CLIENT + "idleConnectionInPoolTimeoutInMs", 60 * 1000);
42+
}
43+
44+
public static int defaultIdleConnectionTimeoutInMs() {
45+
return Integer.getInteger(ASYNC_CLIENT + "idleConnectionTimeoutInMs", 60 * 1000);
46+
}
47+
48+
public static int defaultRequestTimeoutInMs() {
49+
return Integer.getInteger(ASYNC_CLIENT + "requestTimeoutInMs", 60 * 1000);
50+
}
51+
52+
public static int defaultWebSocketIdleTimeoutInMs() {
53+
return Integer.getInteger(ASYNC_CLIENT + "webSocketTimoutInMS", 15 * 60 * 1000);
54+
}
55+
56+
public static int defaultMaxConnectionLifeTimeInMs() {
57+
return Integer.getInteger(ASYNC_CLIENT + "maxConnectionLifeTimeInMs", -1);
58+
}
59+
60+
public static boolean defaultRedirectEnabled() {
61+
return Boolean.getBoolean(ASYNC_CLIENT + "redirectsEnabled");
62+
}
63+
64+
public static int defaultMaxRedirects() {
65+
return Integer.getInteger(ASYNC_CLIENT + "maxRedirects", 5);
66+
}
67+
68+
public static boolean defaultCompressionEnabled() {
69+
return Boolean.getBoolean(ASYNC_CLIENT + "compressionEnabled");
70+
}
71+
72+
public static String defaultUserAgent() {
73+
return System.getProperty(ASYNC_CLIENT + "userAgent", "NING/1.0");
74+
}
75+
76+
public static int defaultIoThreadMultiplier() {
77+
return Integer.getInteger(ASYNC_CLIENT + "ioThreadMultiplier", 2);
78+
}
79+
80+
public static boolean defaultUseProxySelector() {
81+
return Boolean.getBoolean(ASYNC_CLIENT + "useProxySelector");
82+
}
83+
84+
public static boolean defaultUseProxyProperties() {
85+
return Boolean.getBoolean(ASYNC_CLIENT + "useProxyProperties");
86+
}
87+
88+
public static boolean defaultStrict302Handling() {
89+
return Boolean.getBoolean(ASYNC_CLIENT + "strict302Handling");
90+
}
91+
92+
public static boolean defaultAllowPoolingConnection() {
93+
return getBoolean(ASYNC_CLIENT + "allowPoolingConnection", true);
94+
}
95+
96+
public static boolean defaultUseRelativeURIsWithSSLProxies() {
97+
return getBoolean(ASYNC_CLIENT + "useRelativeURIsWithSSLProxies", true);
98+
}
99+
100+
// unused/broken, left there for compatibility, fixed in Netty 4
101+
public static int defaultRequestCompressionLevel() {
102+
return Integer.getInteger(ASYNC_CLIENT + "requestCompressionLevel", -1);
103+
}
104+
105+
public static int defaultMaxRequestRetry() {
106+
return Integer.getInteger(ASYNC_CLIENT + "maxRequestRetry", 5);
107+
}
108+
109+
public static boolean defaultAllowSslConnectionPool() {
110+
return getBoolean(ASYNC_CLIENT + "allowSslConnectionPool", true);
111+
}
112+
113+
public static boolean defaultUseRawUrl() {
114+
return Boolean.getBoolean(ASYNC_CLIENT + "useRawUrl");
115+
}
116+
117+
public static boolean defaultRemoveQueryParamOnRedirect() {
118+
return getBoolean(ASYNC_CLIENT + "removeQueryParamOnRedirect", true);
119+
}
120+
121+
public static HostnameVerifier defaultHostnameVerifier() {
122+
return new DefaultHostnameVerifier();
123+
}
124+
125+
public static boolean defaultSpdyEnabled() {
126+
return Boolean.getBoolean(ASYNC_CLIENT + "spdyEnabled");
127+
}
128+
129+
public static int defaultSpdyInitialWindowSize() {
130+
return Integer.getInteger(ASYNC_CLIENT + "spdyInitialWindowSize", 10 * 1024 * 1024);
131+
}
132+
133+
public static int defaultSpdyMaxConcurrentStreams() {
134+
return Integer.getInteger(ASYNC_CLIENT + "spdyMaxConcurrentStreams", 100);
135+
}
136+
}

api/src/main/java/org/asynchttpclient/SimpleAsyncHttpClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,13 @@ public Builder setFollowRedirects(boolean followRedirects) {
493493
return this;
494494
}
495495

496-
public Builder setMaximumConnectionsTotal(int defaultMaxTotalConnections) {
497-
configBuilder.setMaximumConnectionsTotal(defaultMaxTotalConnections);
496+
public Builder setMaxConnectionsTotal(int defaultMaxTotalConnections) {
497+
configBuilder.setMaxConnectionsTotal(defaultMaxTotalConnections);
498498
return this;
499499
}
500500

501-
public Builder setMaximumConnectionsPerHost(int defaultMaxConnectionPerHost) {
502-
configBuilder.setMaximumConnectionsPerHost(defaultMaxConnectionPerHost);
501+
public Builder setMaxConnectionsPerHost(int defaultMaxConnectionPerHost) {
502+
configBuilder.setMaxConnectionsPerHost(defaultMaxConnectionPerHost);
503503
return this;
504504
}
505505

@@ -518,8 +518,8 @@ public Builder setRequestTimeoutInMs(int defaultRequestTimeoutInMs) {
518518
return this;
519519
}
520520

521-
public Builder setMaximumNumberOfRedirects(int maxDefaultRedirects) {
522-
configBuilder.setMaximumNumberOfRedirects(maxDefaultRedirects);
521+
public Builder setMaxRedirects(int maxRedirects) {
522+
configBuilder.setMaxRedirects(maxRedirects);
523523
return this;
524524
}
525525

api/src/test/java/org/asynchttpclient/async/AsyncProvidersBasicTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ public Response onCompleted(Response response) throws Exception {
13431343

13441344
@Test(groups = { "online", "default_provider", "async" })
13451345
public void asyncDoGetMaxRedirectTest() throws Exception {
1346-
AsyncHttpClient client = getAsyncHttpClient(new Builder().setMaximumNumberOfRedirects(0).setFollowRedirects(true).build());
1346+
AsyncHttpClient client = getAsyncHttpClient(new Builder().setMaxRedirects(0).setFollowRedirects(true).build());
13471347
try {
13481348
// Use a l in case the assert fail
13491349
final CountDownLatch l = new CountDownLatch(1);

api/src/test/java/org/asynchttpclient/async/BasicAuthTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void basicAuthTest() throws IOException, ExecutionException, TimeoutExcep
197197

198198
@Test(groups = { "standalone", "default_provider" })
199199
public void redirectAndBasicAuthTest() throws Exception, ExecutionException, TimeoutException, InterruptedException {
200-
AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setFollowRedirects(true).setMaximumNumberOfRedirects(10).build());
200+
AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setFollowRedirects(true).setMaxRedirects(10).build());
201201
try {
202202
Future<Response> f = client.prepareGet(getTargetUrl2())//
203203
.setRealm((new Realm.RealmBuilder()).setPrincipal(USER).setPassword(ADMIN).build())//

api/src/test/java/org/asynchttpclient/async/BodyChunkTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void negativeContentTypeTest() throws Exception {
3636

3737
AsyncHttpClientConfig.Builder confbuilder = new AsyncHttpClientConfig.Builder();
3838
confbuilder = confbuilder.setConnectionTimeoutInMs(100);
39-
confbuilder = confbuilder.setMaximumConnectionsTotal(50);
39+
confbuilder = confbuilder.setMaxConnectionsTotal(50);
4040
confbuilder = confbuilder.setRequestTimeoutInMs(5 * 60 * 1000); // 5 minutes
4141

4242
// Create client

api/src/test/java/org/asynchttpclient/async/ChunkingTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public void testCustomChunking() throws Exception {
4646
AsyncHttpClientConfig.Builder bc = new AsyncHttpClientConfig.Builder();
4747

4848
bc.setAllowPoolingConnection(true);
49-
bc.setMaximumConnectionsPerHost(1);
50-
bc.setMaximumConnectionsTotal(1);
49+
bc.setMaxConnectionsPerHost(1);
50+
bc.setMaxConnectionsTotal(1);
5151
bc.setConnectionTimeoutInMs(1000);
5252
bc.setRequestTimeoutInMs(1000);
5353
bc.setFollowRedirects(true);

api/src/test/java/org/asynchttpclient/async/ConnectionPoolTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public abstract class ConnectionPoolTest extends AbstractBasicTest {
4242

4343
@Test(groups = { "standalone", "default_provider" })
4444
public void testMaxTotalConnections() {
45-
AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true).setMaximumConnectionsTotal(1).build());
45+
AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true).setMaxConnectionsTotal(1).build());
4646
try {
4747
String url = getTargetUrl();
4848
int i;
@@ -64,7 +64,7 @@ public void testMaxTotalConnections() {
6464

6565
@Test(groups = { "standalone", "default_provider" })
6666
public void testMaxTotalConnectionsException() {
67-
AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true).setMaximumConnectionsTotal(1).build());
67+
AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true).setMaxConnectionsTotal(1).build());
6868
try {
6969
String url = getTargetUrl();
7070
int i;
@@ -132,7 +132,7 @@ public Response onCompleted(Response response) throws Exception {
132132

133133
@Test(groups = { "standalone", "default_provider" })
134134
public void multipleMaxConnectionOpenTest() throws Exception {
135-
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true).setConnectionTimeoutInMs(5000).setMaximumConnectionsTotal(1).build();
135+
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true).setConnectionTimeoutInMs(5000).setMaxConnectionsTotal(1).build();
136136
AsyncHttpClient c = getAsyncHttpClient(cg);
137137
try {
138138
String body = "hello there";
@@ -160,7 +160,7 @@ public void multipleMaxConnectionOpenTest() throws Exception {
160160

161161
@Test(groups = { "standalone", "default_provider" })
162162
public void multipleMaxConnectionOpenTestWithQuery() throws Exception {
163-
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true).setConnectionTimeoutInMs(5000).setMaximumConnectionsTotal(1).build();
163+
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true).setConnectionTimeoutInMs(5000).setMaxConnectionsTotal(1).build();
164164
AsyncHttpClient c = getAsyncHttpClient(cg);
165165
try {
166166
String body = "hello there";

api/src/test/java/org/asynchttpclient/async/HttpToHttpsRedirectTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public void runAllSequentiallyBecauseNotThreadSafe() throws Exception {
9696
public void httpToHttpsRedirect() throws Exception {
9797
redirectDone.getAndSet(false);
9898

99-
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setMaximumNumberOfRedirects(5).setFollowRedirects(true).build();
99+
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setMaxRedirects(5).setFollowRedirects(true).build();
100100
AsyncHttpClient c = getAsyncHttpClient(cg);
101101
try {
102102
Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", getTargetUrl2()).execute().get();
@@ -112,7 +112,7 @@ public void httpToHttpsRedirect() throws Exception {
112112
public void httpToHttpsProperConfig() throws Exception {
113113
redirectDone.getAndSet(false);
114114

115-
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setMaximumNumberOfRedirects(5).setFollowRedirects(true).build();
115+
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setMaxRedirects(5).setFollowRedirects(true).build();
116116
AsyncHttpClient c = getAsyncHttpClient(cg);
117117
try {
118118
Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", getTargetUrl2() + "/test2").execute().get();
@@ -134,7 +134,7 @@ public void httpToHttpsProperConfig() throws Exception {
134134
public void relativeLocationUrl() throws Exception {
135135
redirectDone.getAndSet(false);
136136

137-
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setMaximumNumberOfRedirects(5).setFollowRedirects(true).build();
137+
AsyncHttpClientConfig cg = new AsyncHttpClientConfig.Builder().setMaxRedirects(5).setFollowRedirects(true).build();
138138
AsyncHttpClient c = getAsyncHttpClient(cg);
139139
try {
140140
Response response = c.prepareGet(getTargetUrl()).setHeader("X-redirect", "/foo/test").execute().get();

api/src/test/java/org/asynchttpclient/async/MaxConnectionsInThreads.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testMaxConnectionsWithinThreads() {
5252
String[] urls = new String[] { servletEndpointUri.toString(), servletEndpointUri.toString() };
5353

5454
final AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setConnectionTimeoutInMs(1000).setRequestTimeoutInMs(5000)
55-
.setAllowPoolingConnection(true).setMaximumConnectionsTotal(1).setMaximumConnectionsPerHost(1).build());
55+
.setAllowPoolingConnection(true).setMaxConnectionsTotal(1).setMaxConnectionsPerHost(1).build());
5656

5757
try {
5858
final Boolean[] caughtError = new Boolean[] { Boolean.FALSE };

0 commit comments

Comments
 (0)