Skip to content

Commit

Permalink
feat(apache): Add maxConnTotal configuration
Browse files Browse the repository at this point in the history
Closes #355
  • Loading branch information
ElPicador committed Aug 22, 2017
1 parent 6098fef commit cfbfec2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public APIClientBuilder setBuildHosts(List<String> buildHosts) {
return this;
}

@Override
public APIClientBuilder setMaxConnTotal(int maxConnTotal) {
super.setMaxConnTotal(maxConnTotal);
return this;
}

protected abstract APIClient build(@Nonnull APIClientConfiguration configuration);

/**
Expand All @@ -126,6 +132,7 @@ public APIClient build() {
.setConnectTimeout(connectTimeout)
.setReadTimeout(readTimeout)
.setHostDownTimeout(hostDownTimeout)
.setMaxConnTotal(maxConnTotal)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class APIClientConfiguration {
protected int connectTimeout;
protected int readTimeout;
protected int hostDownTimeout;
protected int maxConnTotal;

public String getApplicationId() {
return applicationId;
Expand Down Expand Up @@ -97,4 +98,14 @@ public APIClientConfiguration setHostDownTimeout(int hostDownTimeout) {
this.hostDownTimeout = hostDownTimeout;
return this;
}

public int getMaxConnTotal() {
return maxConnTotal;
}

public APIClientConfiguration setMaxConnTotal(int maxConnTotal) {
this.maxConnTotal = maxConnTotal;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class GenericAPIClientBuilder {
protected int readTimeout = READ_TIMEOUT_MS;
protected int hostDownTimeout = HOST_DOWN_TIMEOUT_MS;
protected ObjectMapper objectMapper = DEFAULT_OBJECT_MAPPER;
protected int maxConnTotal = 10;

/**
* Initialize this builder with the applicationId and apiKey
Expand Down Expand Up @@ -143,6 +144,17 @@ public GenericAPIClientBuilder setBuildHosts(List<String> buildHosts) {
return this;
}

/**
* Set the maximum of connection, only available for {@link ApacheAPIClientBuilder} and {@link AsyncAPIClientBuilder}
*
* @param maxConnTotal the max number of connections
* @return this
*/
public GenericAPIClientBuilder setMaxConnTotal(int maxConnTotal) {
this.maxConnTotal = maxConnTotal;
return this;
}

private String getApiClientVersion() {
try (InputStream versionStream = getClass().getResourceAsStream("version.properties")) {
BufferedReader versionReader = new BufferedReader(new InputStreamReader(versionStream));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ public ApacheAPIClientBuilder setBuildHosts(List<String> buildHosts) {
return this;
}

@Override
public ApacheAPIClientBuilder setMaxConnTotal(int maxConnTotal) {
super.setMaxConnTotal(maxConnTotal);
return this;
}

@Override
protected APIClient build(@Nonnull APIClientConfiguration configuration) {
return new APIClient(new ApacheHttpClient(configuration), configuration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public ApacheHttpClient(APIClientConfiguration configuration) {
.disableAutomaticRetries()
.setDefaultHeaders(httpHeaders)
.setDnsResolver(new TimeoutableHostNameResolver(configuration.getConnectTimeout()))
.setDefaultRequestConfig(requestConfig).build();
.setDefaultRequestConfig(requestConfig)
.setMaxConnTotal(configuration.getMaxConnTotal())
.build();

this.objectMapper = configuration.getObjectMapper();
this.queryHosts = configuration.getQueryHosts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void buildAPIClient() throws IOException {
assertThat(apiClient.configuration).isEqualToIgnoringGivenFields(new APIClientConfiguration()
.setApplicationId("appId")
.setApiKey("apiKey")
.setMaxConnTotal(10)
.setConnectTimeout(100)
.setReadTimeout(200)
.setHostDownTimeout(300000)
Expand Down

0 comments on commit cfbfec2

Please sign in to comment.