This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow for basic configuration of HttpClient
Allow the underlying OkHttpClient instance to be configured with a few basic properties from the Config, if specified. The configurable properties are: - connect timeout - `http.client.connectTimeout` - read timeout - `http.client.readTimeout` - write timeout - `http.client.writeTimeout` - connection pool: max idle connections - `http.client.maxIdleConnections` - connection pool: keep alive duration - `http.client.keepAliveDuration` - max async requests - `http.client.async.maxRequests` - max async requests per host - `http.client.async.maxRequestsPerHost` The default value from OkHttpClient is used for any unspecified properties (which is the behavior previous to this commit). Also rename the factory method `HttpClient.create()` to `HttpClient.createDefault()` to make it clear that the instance being returned is using all of the default configuration properties.
- Loading branch information
Showing
8 changed files
with
245 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
modules/okhttp-client/src/main/java/com/spotify/apollo/http/client/HttpClientProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* -\-\- | ||
* Spotify Apollo okhttp Client Module | ||
* -- | ||
* Copyright (C) 2013 - 2015 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
package com.spotify.apollo.http.client; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Provider; | ||
import com.squareup.okhttp.OkHttpClient; | ||
|
||
class HttpClientProvider implements Provider<HttpClient> { | ||
|
||
private final OkHttpClient okHttpClient; | ||
|
||
@Inject | ||
HttpClientProvider(OkHttpClient okHttpClient) { | ||
this.okHttpClient = okHttpClient; | ||
} | ||
|
||
@Override | ||
public HttpClient get() { | ||
return new HttpClient(okHttpClient); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
modules/okhttp-client/src/main/java/com/spotify/apollo/http/client/OkHttpClientProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* -\-\- | ||
* Spotify Apollo okhttp Client Module | ||
* -- | ||
* Copyright (C) 2013 - 2015 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
package com.spotify.apollo.http.client; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Provider; | ||
import com.squareup.okhttp.ConnectionPool; | ||
import com.squareup.okhttp.OkHttpClient; | ||
import com.typesafe.config.Config; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.spotify.apollo.environment.ConfigUtil.optionalInt; | ||
|
||
class OkHttpClientProvider implements Provider<OkHttpClient> { | ||
|
||
private final OkHttpClientConfig config; | ||
|
||
@Inject | ||
OkHttpClientProvider(Config config) { | ||
this.config = new OkHttpClientConfig(config); | ||
} | ||
|
||
@Override | ||
public OkHttpClient get() { | ||
final OkHttpClient client = new OkHttpClient(); | ||
|
||
//timeouts settings | ||
config.connectTimeoutMillis().ifPresent( | ||
millis -> client.setConnectTimeout(millis, TimeUnit.MILLISECONDS)); | ||
|
||
config.readTimeoutMillis().ifPresent( | ||
millis -> client.setReadTimeout(millis, TimeUnit.MILLISECONDS)); | ||
|
||
config.writeTimeoutMillis().ifPresent( | ||
millis -> client.setWriteTimeout(millis, TimeUnit.MILLISECONDS)); | ||
|
||
// connection pool settings | ||
client.setConnectionPool(new ConnectionPool( | ||
// defaults that come from com.squareup.okhttp.ConnectionPool | ||
config.maxIdleConnections().orElse(5), | ||
config.connectionKeepAliveDurationMillis().orElse(5 * 60 * 1000) | ||
)); | ||
|
||
// async dispatcher settings | ||
config.maxAsyncRequests().ifPresent(max -> client.getDispatcher().setMaxRequests(max)); | ||
|
||
config.maxAsyncRequestsPerHost().ifPresent( | ||
max -> client.getDispatcher().setMaxRequestsPerHost(max)); | ||
|
||
return client; | ||
} | ||
|
||
private static class OkHttpClientConfig { | ||
|
||
private final Config config; | ||
|
||
OkHttpClientConfig(final Config config) { | ||
this.config = config; | ||
} | ||
|
||
Optional<Integer> connectTimeoutMillis() { | ||
return optionalInt(config, "http.client.connectTimeout"); | ||
} | ||
|
||
Optional<Integer> readTimeoutMillis() { | ||
return optionalInt(config, "http.client.readTimeout"); | ||
} | ||
|
||
Optional<Integer> writeTimeoutMillis() { | ||
return optionalInt(config, "http.client.writeTimeout"); | ||
} | ||
|
||
Optional<Integer> maxIdleConnections() { | ||
return optionalInt(config, "http.client.maxIdleConnections"); | ||
} | ||
|
||
Optional<Integer> connectionKeepAliveDurationMillis() { | ||
return optionalInt(config, "http.client.keepAliveDuration"); | ||
} | ||
|
||
Optional<Integer> maxAsyncRequests() { | ||
return optionalInt(config, "http.client.async.maxRequests"); | ||
} | ||
|
||
Optional<Integer> maxAsyncRequestsPerHost() { | ||
return optionalInt(config, "http.client.async.maxRequestsPerHost"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
.../okhttp-client/src/test/java/com/spotify/apollo/http/client/OkHttpClientProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* -\-\- | ||
* Spotify Apollo okhttp Client Module | ||
* -- | ||
* Copyright (C) 2013 - 2015 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
package com.spotify.apollo.http.client; | ||
|
||
import com.squareup.okhttp.OkHttpClient; | ||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class OkHttpClientProviderTest { | ||
|
||
private static OkHttpClient buildClient(final String str) { | ||
final Config config = ConfigFactory.parseString(str); | ||
final OkHttpClientProvider provider = new OkHttpClientProvider(config); | ||
return provider.get(); | ||
} | ||
|
||
@Test | ||
public void testConnectTimeout() { | ||
assertEquals(1234, buildClient("http.client.connectTimeout: 1234").getConnectTimeout()); | ||
} | ||
|
||
@Test | ||
public void testReadTimeout() { | ||
assertEquals(444, buildClient("http.client.readTimeout: 444").getReadTimeout()); | ||
} | ||
|
||
@Test | ||
public void testWriteTimeout() { | ||
assertEquals(5555, buildClient("http.client.writeTimeout: 5555").getWriteTimeout()); | ||
} | ||
|
||
@Test | ||
public void testMaxRequests() { | ||
final OkHttpClient client = buildClient("http.client.async.maxRequests: 72"); | ||
assertEquals(72, client.getDispatcher().getMaxRequests()); | ||
} | ||
|
||
@Test | ||
public void testMaxRequestsPerHost() { | ||
final OkHttpClient client = buildClient("http.client.async.maxRequestsPerHost: 79"); | ||
assertEquals(79, client.getDispatcher().getMaxRequestsPerHost()); | ||
} | ||
} |