From 0e4b91a1a2f65eb1e5b0ceb17682e89ddca48b16 Mon Sep 17 00:00:00 2001 From: Bjørn Christian Seime Date: Fri, 26 Jul 2019 11:15:28 +0200 Subject: Make 'connection time to live' a configurable parameter --- .../vespa/http/client/config/ConnectionParams.java | 25 ++++++++++++++++++++-- .../communication/ApacheGatewayConnection.java | 2 +- .../http/client/runner/CommandLineArguments.java | 8 +++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/config/ConnectionParams.java b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/config/ConnectionParams.java index adf61b124ab..ec9471e68ed 100644 --- a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/config/ConnectionParams.java +++ b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/config/ConnectionParams.java @@ -8,6 +8,7 @@ import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; +import java.time.Duration; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -44,6 +45,7 @@ public final class ConnectionParams { private int traceEveryXOperation = 0; private boolean printTraceToStdErr = true; private boolean useTlsConfigFromEnvironment = false; + private Duration connectionTimeToLive = Duration.ofSeconds(15); /** * Use TLS configuration through the standard Vespa environment variables. @@ -227,6 +229,13 @@ public final class ConnectionParams { return this; } + /** + * Set the maximum time to live for persistent connections + */ + public Builder setConnectionTimeToLive(Duration connectionTimeToLive) { + this.connectionTimeToLive = connectionTimeToLive; + return this; + } public ConnectionParams build() { return new ConnectionParams( @@ -244,7 +253,8 @@ public final class ConnectionParams { traceLevel, traceEveryXOperation, printTraceToStdErr, - useTlsConfigFromEnvironment); + useTlsConfigFromEnvironment, + connectionTimeToLive); } public int getNumPersistentConnectionsPerEndpoint() { @@ -288,6 +298,10 @@ public final class ConnectionParams { public boolean useTlsConfigFromEnvironment() { return useTlsConfigFromEnvironment; } + + public Duration getConnectionTimeToLive() { + return connectionTimeToLive; + } } private final SSLContext sslContext; private final HostnameVerifier hostnameVerifier; @@ -304,6 +318,7 @@ public final class ConnectionParams { private final int traceEveryXOperation; private final boolean printTraceToStdErr; private final boolean useTlsConfigFromEnvironment; + private final Duration connectionTimeToLive; private ConnectionParams( SSLContext sslContext, @@ -320,10 +335,12 @@ public final class ConnectionParams { int traceLevel, int traceEveryXOperation, boolean printTraceToStdErr, - boolean useTlsConfigFromEnvironment) { + boolean useTlsConfigFromEnvironment, + Duration connectionTimeToLive) { this.sslContext = sslContext; this.hostnameVerifier = hostnameVerifier; this.useTlsConfigFromEnvironment = useTlsConfigFromEnvironment; + this.connectionTimeToLive = connectionTimeToLive; this.headers.putAll(headers); this.headerProviders.putAll(headerProviders); this.numPersistentConnectionsPerEndpoint = numPersistentConnectionsPerEndpoint; @@ -400,6 +417,10 @@ public final class ConnectionParams { return useTlsConfigFromEnvironment; } + public Duration getConnectionTimeToLive() { + return connectionTimeToLive; + } + /** * A header provider that provides a header value. {@link #getHeaderValue()} is called each time a new HTTP request * is constructed by {@link com.yahoo.vespa.http.client.FeedClient}. diff --git a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java index 0b92427bcbe..77a18b631ee 100644 --- a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java +++ b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnection.java @@ -406,7 +406,7 @@ class ApacheGatewayConnection implements GatewayConnection { } clientBuilder.setMaxConnPerRoute(1); clientBuilder.setMaxConnTotal(1); - clientBuilder.setConnectionTimeToLive(15, TimeUnit.SECONDS); + clientBuilder.setConnectionTimeToLive(connectionParams.getConnectionTimeToLive().toSeconds(), TimeUnit.SECONDS); clientBuilder.setUserAgent(String.format("vespa-http-client (%s)", Vtag.currentVersion)); clientBuilder.setDefaultHeaders(Collections.singletonList(new BasicHeader(Headers.CLIENT_VERSION, Vtag.currentVersion))); clientBuilder.disableContentCompression(); diff --git a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/CommandLineArguments.java b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/CommandLineArguments.java index ea0b3f29509..8a2a1652b4a 100644 --- a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/CommandLineArguments.java +++ b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/CommandLineArguments.java @@ -20,6 +20,7 @@ import org.apache.http.message.BasicLineParser; import javax.inject.Inject; import java.net.MalformedURLException; import java.net.URL; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -110,6 +111,8 @@ public class CommandLineArguments { } } + // TODO Don't duplicate default values from ConnectionParams.Builder. Some defaults are already inconsistent. + @Inject private HelpOption helpOption; @@ -213,6 +216,10 @@ public class CommandLineArguments { description = "BETA! Use Vespa TLS configuration from environment if available. Other HTTPS/TLS configuration will be ignored if this is set.") private boolean useTlsConfigFromEnvironment = false; + @Option(name = {"--connectionTimeToLive"}, + description = "Maximum time to live for persistent connections. Specified as integer, in seconds.") + private long connectionTimeToLive = 15; + private final List
parsedHeaders = new ArrayList<>(); int getWhenVerboseEnabledPrintMessageForEveryXDocuments() { @@ -257,6 +264,7 @@ public class CommandLineArguments { .setPrintTraceToStdErr(traceArg > 0) .setNumPersistentConnectionsPerEndpoint(numPersistentConnectionsPerEndpoint) .setUseTlsConfigFromEnvironment(useTlsConfigFromEnvironment) + .setConnectionTimeToLive(Duration.ofSeconds(connectionTimeToLive)) .build() ) // Enable dynamic throttling. -- cgit v1.2.3