made client connection timeouts configurable

This commit is contained in:
anhefti 2019-11-27 10:38:04 +01:00
parent fccfb2ca84
commit d1cdd188a3
2 changed files with 27 additions and 4 deletions

View file

@ -36,6 +36,7 @@ import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.ssl.SSLContextBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
@ -56,15 +57,25 @@ public class ClientHttpRequestFactoryService {
private static final Collection<String> DEV_PROFILES = Arrays.asList("dev-gui", "test", "demo", "dev-ws");
private static final Collection<String> PROD_PROFILES = Arrays.asList("prod-gui", "prod-ws");
private final int connectTimeout;
private final int connectionRequestTimeout;
private final int readTimeout;
private final Environment environment;
private final ClientCredentialService clientCredentialService;
public ClientHttpRequestFactoryService(
final Environment environment,
final ClientCredentialService clientCredentialService) {
final ClientCredentialService clientCredentialService,
@Value("${sebserver.http.client.connect-timeout:15000}") final int connectTimeout,
@Value("${sebserver.http.client.connection-request-timeout:10000}") final int connectionRequestTimeout,
@Value("${sebserver.http.client.read-timeout:10000}") final int readTimeout) {
this.environment = environment;
this.clientCredentialService = clientCredentialService;
this.connectTimeout = connectTimeout;
this.connectionRequestTimeout = connectionRequestTimeout;
this.readTimeout = readTimeout;
}
public Result<ClientHttpRequestFactory> getClientHttpRequestFactory() {
@ -104,6 +115,9 @@ public class ClientHttpRequestFactoryService {
new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(this.createProxiedClient(proxy, null));
factory.setBufferRequestBody(false);
factory.setConnectionRequestTimeout(this.connectionRequestTimeout);
factory.setConnectTimeout(this.connectTimeout);
factory.setReadTimeout(this.readTimeout);
return factory;
} else {
@ -112,7 +126,9 @@ public class ClientHttpRequestFactoryService {
new HttpComponentsClientHttpRequestFactory();
devClientHttpRequestFactory.setBufferRequestBody(false);
devClientHttpRequestFactory.setConnectionRequestTimeout(this.connectionRequestTimeout);
devClientHttpRequestFactory.setConnectTimeout(this.connectTimeout);
devClientHttpRequestFactory.setReadTimeout(this.readTimeout);
return devClientHttpRequestFactory;
}
}
@ -191,11 +207,14 @@ public class ClientHttpRequestFactoryService {
final HttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.build();
return new HttpComponentsClientHttpRequestFactory(client);
final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);
factory.setConnectionRequestTimeout(this.connectionRequestTimeout);
factory.setConnectTimeout(this.connectTimeout);
factory.setReadTimeout(this.readTimeout);
return factory;
}
}
// TODO set connection and read timeout!? configurable!?
private HttpClient createProxiedClient(final ProxyData proxy, final SSLContext sslContext) {
final HttpHost httpHost = new HttpHost(

View file

@ -15,6 +15,10 @@ spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.maxLifetime=1800000
sebserver.http.client.connect-timeout=1500
sebserver.http.client.connection-request-timeout=10000
sebserver.http.client.read-timeout=1000
# webservice configuration
sebserver.webservice.distributed=false
sebserver.webservice.http.scheme=http