Skip to content

Commit 983e8da

Browse files
author
Nakul Sabharwal
committed
Configure connection timeout
1 parent 970267a commit 983e8da

File tree

9 files changed

+275
-10
lines changed

9 files changed

+275
-10
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2017 Microsoft Corporation
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
// ------------------------------------------------------------------------------
22+
23+
package com.microsoft.graph.core;
24+
25+
public class DefaultConnectionConfig implements IConnectionConfig{
26+
27+
/**
28+
* Default connect timeout
29+
*/
30+
private static int DEFAULT_CONNECT_TIMEOUT_MS = 30_000;
31+
32+
/**
33+
* Default connection read timeout
34+
*/
35+
private static int DEFAULT_READ_TIMEOUT_MS = 30_000;
36+
37+
/**
38+
* Gets the connect timeout
39+
*
40+
* @return the timeout in milliseconds
41+
*/
42+
@Override
43+
public int getConnectTimeout() {
44+
return DEFAULT_CONNECT_TIMEOUT_MS;
45+
}
46+
47+
/**
48+
* Sets the connect timeout
49+
*
50+
* @param connectTimeoutValue Connect timeout in milliseconds to be set to.
51+
*/
52+
@Override
53+
public void setConnectTimeout(int connectTimeoutValue) {
54+
DEFAULT_CONNECT_TIMEOUT_MS = connectTimeoutValue;
55+
}
56+
57+
/**
58+
* Gets the read timeout
59+
*
60+
* @return the timeout in milliseconds
61+
*/
62+
@Override
63+
public int getReadTimeout() {
64+
return DEFAULT_READ_TIMEOUT_MS;
65+
}
66+
67+
/**
68+
* Sets the connect timeout
69+
*
70+
* @param readTimeoutValue Read timeout in milliseconds to be set to.
71+
*/
72+
@Override
73+
public void setReadTimeout(int readTimeoutValue) {
74+
DEFAULT_READ_TIMEOUT_MS = readTimeoutValue;
75+
}
76+
77+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2017 Microsoft Corporation
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
// ------------------------------------------------------------------------------
22+
23+
package com.microsoft.graph.core;
24+
25+
public interface IConnectionConfig {
26+
27+
/**
28+
* Gets the connect timeout
29+
*
30+
* @return the timeout in milliseconds
31+
*/
32+
int getConnectTimeout();
33+
34+
/**
35+
* Sets the connect timeout
36+
*
37+
* @param connectTimeoutValue Connect timeout in milliseconds to be set to.
38+
*/
39+
void setConnectTimeout(int connectTimeoutValue);
40+
41+
/**
42+
* Gets the read timeout
43+
*
44+
* @return the timeout in milliseconds
45+
*/
46+
int getReadTimeout();
47+
48+
/**
49+
* Sets the connect timeout
50+
*
51+
* @param readTimeoutValue Read timeout in milliseconds to be set to.
52+
*/
53+
void setReadTimeout(int readTimeoutValue);
54+
}

src/main/java/com/microsoft/graph/http/DefaultHttpProvider.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import com.microsoft.graph.concurrency.IExecutors;
2929
import com.microsoft.graph.concurrency.IProgressCallback;
3030
import com.microsoft.graph.core.ClientException;
31+
import com.microsoft.graph.core.DefaultConnectionConfig;
32+
import com.microsoft.graph.core.IConnectionConfig;
3133
import com.microsoft.graph.logger.ILogger;
3234
import com.microsoft.graph.logger.LoggerLevel;
3335
import com.microsoft.graph.options.HeaderOption;
@@ -89,6 +91,11 @@ public class DefaultHttpProvider implements IHttpProvider {
8991
* The connection factory
9092
*/
9193
private IConnectionFactory connectionFactory;
94+
95+
/**
96+
* The connection config
97+
*/
98+
private IConnectionConfig connectionConfig;
9299

93100
/**
94101
* Creates the DefaultHttpProvider
@@ -232,6 +239,11 @@ private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRe
232239
final URL requestUrl = request.getRequestUrl();
233240
logger.logDebug("Starting to send request, URL " + requestUrl.toString());
234241
final IConnection connection = connectionFactory.createFromRequest(request);
242+
if(this.connectionConfig == null) {
243+
this.connectionConfig = new DefaultConnectionConfig();
244+
}
245+
connection.setConnectTimeout(connectionConfig.getConnectTimeout());
246+
connection.setReadTimeout(connectionConfig.getReadTimeout());
235247

236248
try {
237249
logger.logDebug("Request Method " + request.getHttpMethod().toString());
@@ -479,4 +491,26 @@ public IExecutors getExecutors() {
479491
public IAuthenticationProvider getAuthenticationProvider() {
480492
return authenticationProvider;
481493
}
494+
495+
496+
/**
497+
* Get connection config for read and connect timeout in requests
498+
*
499+
* @return Connection configuration to be used for timeout values
500+
*/
501+
public IConnectionConfig getConnectionConfig() {
502+
if(this.connectionConfig == null) {
503+
this.connectionConfig = new DefaultConnectionConfig();
504+
}
505+
return connectionConfig;
506+
}
507+
508+
/**
509+
* Set connection config for read and connect timeout in requests
510+
*
511+
* @param connectionConfig Connection configuration to be used for timeout values
512+
*/
513+
public void setConnectionConfig(IConnectionConfig connectionConfig) {
514+
this.connectionConfig = connectionConfig;
515+
}
482516
}

src/main/java/com/microsoft/graph/http/IConnection.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,18 @@ public interface IConnection {
114114
* @param length the length of content
115115
*/
116116
void setContentLength(final int length);
117+
118+
/**
119+
* Set the connect timeout on the connection
120+
*
121+
* @param connectTimeoutMilliseconds the connection timeout in milliseconds
122+
*/
123+
void setConnectTimeout(final int connectTimeoutMilliseconds);
124+
125+
/**
126+
* Set the read timeout on the connection
127+
*
128+
* @param readTimeoutMilliseconds the read timeout in milliseconds
129+
*/
130+
void setReadTimeout(final int readTimeoutMilliseconds);
117131
}

src/main/java/com/microsoft/graph/http/IHttpProvider.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.microsoft.graph.concurrency.ICallback;
2626
import com.microsoft.graph.core.ClientException;
27+
import com.microsoft.graph.core.IConnectionConfig;
2728
import com.microsoft.graph.serializer.ISerializer;
2829

2930
/**
@@ -37,6 +38,22 @@ public interface IHttpProvider {
3738
* @return the serializer for this provider
3839
*/
3940
ISerializer getSerializer();
41+
42+
/**
43+
* Get connection config for read and connect timeout in requests
44+
*
45+
* @return Connection configuration to be used for timeout values
46+
*
47+
*/
48+
public IConnectionConfig getConnectionConfig();
49+
50+
/**
51+
* Set connection config for read and connect timeout in requests
52+
*
53+
* @param connectionConfig Connection configuration to be used for timeout values
54+
*
55+
*/
56+
public void setConnectionConfig(IConnectionConfig connectionConfig);
4057

4158
/**
4259
* Sends the HTTP request asynchronously

src/main/java/com/microsoft/graph/http/UrlConnection.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@
3939
*/
4040
public class UrlConnection implements IConnection {
4141

42-
/**
43-
* Default connection read timeout
44-
*/
45-
private static final int DEFAULT_CONNECTION_READ_TIMEOUT_MS = 30_000;
46-
/**
47-
* Default connect timeout
48-
*/
49-
private static final int DEFAULT_CONNECT_TIMEOUT_MS = 30_000;
5042
/**
5143
* The backing HTTP URL connection instance
5244
*/
@@ -71,8 +63,6 @@ public UrlConnection(final IHttpRequest request) throws IOException {
7163
}
7264

7365
connection.setUseCaches(request.getUseCaches());
74-
connection.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MS);
75-
connection.setReadTimeout(DEFAULT_CONNECTION_READ_TIMEOUT_MS);
7666

7767
try {
7868
connection.setRequestMethod(request.getHttpMethod().toString());
@@ -164,6 +154,16 @@ public String getRequestMethod() {
164154
public void setContentLength(final int length) {
165155
connection.setFixedLengthStreamingMode(length);
166156
}
157+
158+
@Override
159+
public void setReadTimeout(final int readTimeoutMilliseconds) {
160+
connection.setReadTimeout(readTimeoutMilliseconds);
161+
}
162+
163+
@Override
164+
public void setConnectTimeout(final int connectTimeoutMilliseconds) {
165+
connection.setConnectTimeout(connectTimeoutMilliseconds);
166+
}
167167

168168
/**
169169
* Gets the response headers from an HTTP URL connection

src/test/java/com/microsoft/graph/http/MockConnection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ public int getContentLength() {
7878
public void setContentLength(int length) {
7979
// noop
8080
}
81+
82+
@Override
83+
public void setConnectTimeout(int connectionTimeoutMilliseconds) {
84+
// noop
85+
}
86+
87+
@Override
88+
public void setReadTimeout(int readTimeoutMilliseconds) {
89+
// noop
90+
}
8191

8292
@Override
8393
public Map<String, List<String>> getResponseHeaders() {

src/test/java/com/microsoft/graph/http/MockHttpProvider.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.microsoft.graph.concurrency.IExecutors;
1111
import com.microsoft.graph.concurrency.MockExecutors;
1212
import com.microsoft.graph.core.ClientException;
13+
import com.microsoft.graph.core.DefaultConnectionConfig;
14+
import com.microsoft.graph.core.IConnectionConfig;
1315
import com.microsoft.graph.logger.ILogger;
1416
import com.microsoft.graph.logger.MockLogger;
1517
import com.microsoft.graph.serializer.ISerializer;
@@ -25,6 +27,7 @@ public class MockHttpProvider implements IHttpProvider {
2527
private final IExecutors mExecutors;
2628
private final ILogger mLogger;
2729
private IConnectionFactory mConnectionFactory;
30+
private IConnectionConfig connectionConfig;
2831

2932
@Override
3033
public ISerializer getSerializer() {
@@ -109,4 +112,15 @@ public <Result, BodyType, DeserializeType> Result send(IHttpRequest request, Cla
109112
void setConnectionFactory(final IConnectionFactory factory) {
110113
mConnectionFactory = factory;
111114
}
115+
116+
public IConnectionConfig getConnectionConfig() {
117+
if(this.connectionConfig == null) {
118+
this.connectionConfig = new DefaultConnectionConfig();
119+
}
120+
return this.connectionConfig;
121+
}
122+
123+
public void setConnectionConfig(IConnectionConfig connectionConfig) {
124+
this.connectionConfig = connectionConfig;
125+
}
112126
}

src/test/java/com/microsoft/graph/requests/extensions/GraphServiceClientTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import com.microsoft.graph.concurrency.IProgressCallback;
1717
import com.microsoft.graph.core.ClientException;
1818
import com.microsoft.graph.core.DefaultClientConfig;
19+
import com.microsoft.graph.core.DefaultConnectionConfig;
20+
import com.microsoft.graph.core.IConnectionConfig;
1921
import com.microsoft.graph.http.DefaultHttpProvider;
2022
import com.microsoft.graph.http.IHttpProvider;
2123
import com.microsoft.graph.http.IHttpRequest;
@@ -190,6 +192,16 @@ public <Result, BodyType, DeserializeType> Result send(IHttpRequest request,
190192
throws ClientException {
191193
return null;
192194
}
195+
196+
@Override
197+
public IConnectionConfig getConnectionConfig() {
198+
return null;
199+
}
200+
201+
@Override
202+
public void setConnectionConfig(IConnectionConfig connectionConfig) {
203+
// do nothing
204+
}
193205
};
194206
IGraphServiceClient client = GraphServiceClient //
195207
.builder() //
@@ -222,6 +234,39 @@ public void testExecutorsCannotBeNull() {
222234
public void testLoggerCannotBeNull() {
223235
GraphServiceClient.builder().authenticationProvider(auth).logger(null);
224236
}
237+
238+
@Test
239+
public void connectionConfigTest() {
240+
IAuthenticationProvider ap = new IAuthenticationProvider() {
241+
@Override
242+
public void authenticateRequest(IHttpRequest request) {
243+
// do nothing
244+
}
245+
};
246+
IGraphServiceClient client = GraphServiceClient.builder()
247+
.authenticationProvider(ap)
248+
.buildClient();
249+
client.getHttpProvider().setConnectionConfig(new DefaultConnectionConfig());
250+
assertEquals(30_000, client.getHttpProvider().getConnectionConfig().getConnectTimeout());
251+
assertEquals(30_000, client.getHttpProvider().getConnectionConfig().getReadTimeout());
252+
}
253+
254+
@Test
255+
public void connectionConfigValuesChangeTest() {
256+
IAuthenticationProvider ap = new IAuthenticationProvider() {
257+
@Override
258+
public void authenticateRequest(IHttpRequest request) {
259+
// do nothing
260+
}
261+
};
262+
IGraphServiceClient client = GraphServiceClient.builder()
263+
.authenticationProvider(ap)
264+
.buildClient();
265+
client.getHttpProvider().getConnectionConfig().setConnectTimeout(20_000);
266+
client.getHttpProvider().getConnectionConfig().setReadTimeout(10_000);
267+
assertEquals(20_000, client.getHttpProvider().getConnectionConfig().getConnectTimeout());
268+
assertEquals(10_000, client.getHttpProvider().getConnectionConfig().getReadTimeout());
269+
}
225270

226271
private static ILogger createLogger() {
227272
return new ILogger() {

0 commit comments

Comments
 (0)