Skip to content

Commit

Permalink
add keep-alive-enabled parameter to enable/disable reactive REST clie…
Browse files Browse the repository at this point in the history
…nt keep alive
  • Loading branch information
gefloh committed Feb 15, 2023
1 parent d3f5ac8 commit 1cc9c5b
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class RestClientConfig {
EMPTY.hostnameVerifier = Optional.empty();
EMPTY.connectionTTL = Optional.empty();
EMPTY.connectionPoolSize = Optional.empty();
EMPTY.connectionPoolEnabled = Optional.empty();
EMPTY.keepAliveEnabled = Optional.empty();
EMPTY.maxRedirects = Optional.empty();
EMPTY.headers = Collections.emptyMap();
EMPTY.shared = Optional.empty();
Expand Down Expand Up @@ -198,10 +198,10 @@ public class RestClientConfig {
public Optional<Integer> connectionPoolSize;

/**
* If set to false disables the connection pool completely.
* If set to false disables the keep alive completely.
*/
@ConfigItem(defaultValue = "true")
public Optional<Boolean> connectionPoolEnabled;
public Optional<Boolean> keepAliveEnabled;

/**
* The maximum number of redirection a request can follow.
Expand Down Expand Up @@ -270,7 +270,7 @@ public static RestClientConfig load(String configKey) {
instance.hostnameVerifier = getConfigValue(configKey, "hostname-verifier", String.class);
instance.connectionTTL = getConfigValue(configKey, "connection-ttl", Integer.class);
instance.connectionPoolSize = getConfigValue(configKey, "connection-pool-size", Integer.class);
instance.connectionPoolEnabled = getConfigValue(configKey, "connection-pool-enabled", Boolean.class);
instance.keepAliveEnabled = getConfigValue(configKey, "keep-alive-enabled", Boolean.class);
instance.maxRedirects = getConfigValue(configKey, "max-redirects", Integer.class);
instance.headers = getConfigValues(configKey, "headers", String.class, String.class);
instance.shared = getConfigValue(configKey, "shared", Boolean.class);
Expand Down Expand Up @@ -305,7 +305,7 @@ public static RestClientConfig load(Class<?> interfaceClass) {
instance.hostnameVerifier = getConfigValue(interfaceClass, "hostname-verifier", String.class);
instance.connectionTTL = getConfigValue(interfaceClass, "connection-ttl", Integer.class);
instance.connectionPoolSize = getConfigValue(interfaceClass, "connection-pool-size", Integer.class);
instance.connectionPoolEnabled = getConfigValue(interfaceClass, "connection-pool-enabled", Boolean.class);
instance.keepAliveEnabled = getConfigValue(interfaceClass, "keep-alive-enabled", Boolean.class);
instance.maxRedirects = getConfigValue(interfaceClass, "max-redirects", Integer.class);
instance.headers = getConfigValues(interfaceClass, "headers", String.class, String.class);
instance.shared = getConfigValue(interfaceClass, "shared", Boolean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ public class RestClientsConfig {
public Optional<Integer> connectionPoolSize;

/**
* If set to false disables the connection pool completely.
* If set to false disables the keep alive completely.
*
* Can be overwritten by client-specific settings.
*/
@ConfigItem(defaultValue = "true")
public Optional<Boolean> connectionPoolEnabled;
public Optional<Boolean> keepAliveEnabled;

/**
* The maximum number of redirection a request can follow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ private void verifyClientConfig(RestClientConfig clientConfig, boolean checkExtr
assertThat(clientConfig.connectionTTL.get()).isEqualTo(30000);
assertThat(clientConfig.connectionPoolSize).isPresent();
assertThat(clientConfig.connectionPoolSize.get()).isEqualTo(10);
assertThat(clientConfig.connectionPoolEnabled).isPresent();
assertThat(clientConfig.connectionPoolEnabled.get()).isFalse();
assertThat(clientConfig.keepAliveEnabled).isPresent();
assertThat(clientConfig.keepAliveEnabled.get()).isFalse();
assertThat(clientConfig.maxRedirects).isPresent();
assertThat(clientConfig.maxRedirects.get()).isEqualTo(5);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void checkGlobalConfigValues() {
.isEqualTo("io.quarkus.rest.client.reactive.HelloClientWithBaseUri$MyHostnameVerifier");
assertThat(configRoot.connectionTTL.get()).isEqualTo(20000); // value in ms, will be converted to seconds
assertThat(configRoot.connectionPoolSize.get()).isEqualTo(2);
assertThat(configRoot.connectionPoolEnabled.get()).isTrue();
assertThat(configRoot.keepAliveEnabled.get()).isTrue();
assertThat(configRoot.maxRedirects.get()).isEqualTo(2);
assertThat(configRoot.followRedirects.get()).isTrue();
assertThat(configRoot.providers.get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".que
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".hostname-verifier=io.quarkus.rest.client.reactive.HelloClientWithBaseUri$MyHostnameVerifier
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".connection-ttl=30000
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".connection-pool-size=10
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".connection-pool-enabled=false
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".keep-alive-enabled=false
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".max-redirects=5
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".headers.message=hi
quarkus.rest-client."io.quarkus.rest.client.reactive.HelloClientWithBaseUri".headers.suffix=!
Expand All @@ -33,7 +33,7 @@ quarkus.rest-client.client-prefix.query-param-style=COMMA_SEPARATED
quarkus.rest-client.client-prefix.hostname-verifier=io.quarkus.rest.client.reactive.HelloClientWithBaseUri$MyHostnameVerifier
quarkus.rest-client.client-prefix.connection-ttl=30000
quarkus.rest-client.client-prefix.connection-pool-size=10
quarkus.rest-client.client-prefix.connection-pool-enabled=false
quarkus.rest-client.client-prefix.keep-alive-enabled=false
quarkus.rest-client.client-prefix.max-redirects=5
quarkus.rest-client.client-prefix.headers.user-agent=MP REST Client
quarkus.rest-client.client-prefix.headers.foo=bar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ quarkus.rest-client.headers.foo=bar
quarkus.rest-client.hostname-verifier=io.quarkus.rest.client.reactive.HelloClientWithBaseUri$MyHostnameVerifier
quarkus.rest-client.connection-ttl=20000
quarkus.rest-client.connection-pool-size=2
quarkus.rest-client.connection-pool-enabled=true
quarkus.rest-client.keep-alive-enabled=true
quarkus.rest-client.max-redirects=2
quarkus.rest-client.follow-redirects=true
quarkus.rest-client.providers=io.quarkus.rest.client.reactive.HelloClientWithBaseUri$MyResponseFilter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ private void configureCustomProperties(RestClientBuilder builder) {
builder.property(QuarkusRestClientProperties.CONNECTION_TTL, connectionTTLSeconds);
}

Optional<Boolean> poolEnabled = oneOf(clientConfigByClassName().connectionPoolEnabled,
clientConfigByConfigKey().connectionPoolEnabled, configRoot.connectionPoolEnabled);
if (poolEnabled.isPresent()) {
builder.property(QuarkusRestClientProperties.CONNECTION_POOL_ENABLED, poolEnabled.get());
Optional<Boolean> keepAliveEnabled = oneOf(clientConfigByClassName().keepAliveEnabled,
clientConfigByConfigKey().keepAliveEnabled, configRoot.keepAliveEnabled);
if (keepAliveEnabled.isPresent()) {
builder.property(QuarkusRestClientProperties.KEEP_ALIVE_ENABLED, keepAliveEnabled.get());
}

Map<String, String> headers = clientConfigByClassName().headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void testClientSpecificConfigs() {
Mockito.verify(restClientBuilderMock).hostnameVerifier(Mockito.any(MyHostnameVerifier1.class));
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_TTL, 10); // value converted to seconds
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_POOL_SIZE, 103);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_POOL_ENABLED, false);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.KEEP_ALIVE_ENABLED, false);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.MAX_REDIRECTS, 104);
Mockito.verify(restClientBuilderMock).followRedirects(true);
Mockito.verify(restClientBuilderMock).register(MyResponseFilter1.class);
Expand Down Expand Up @@ -148,7 +148,7 @@ public void testGlobalConfigs() {
Mockito.verify(restClientBuilderMock).hostnameVerifier(Mockito.any(MyHostnameVerifier2.class));
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_TTL, 20);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_POOL_SIZE, 203);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.CONNECTION_POOL_ENABLED, true);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.KEEP_ALIVE_ENABLED, true);
Mockito.verify(restClientBuilderMock).property(QuarkusRestClientProperties.MAX_REDIRECTS, 204);
Mockito.verify(restClientBuilderMock).followRedirects(true);
Mockito.verify(restClientBuilderMock).register(MyResponseFilter2.class);
Expand Down Expand Up @@ -178,7 +178,7 @@ private static RestClientsConfig createSampleConfigRoot() {
.of("io.quarkus.rest.client.reactive.runtime.RestClientCDIDelegateBuilderTest$MyHostnameVerifier2");
configRoot.connectionTTL = Optional.of(20000); // value in ms, will be converted to seconds
configRoot.connectionPoolSize = Optional.of(203);
configRoot.connectionPoolEnabled = Optional.of(true);
configRoot.keepAliveEnabled = Optional.of(true);
configRoot.maxRedirects = Optional.of(204);
configRoot.followRedirects = Optional.of(true);
configRoot.providers = Optional
Expand Down Expand Up @@ -217,7 +217,7 @@ private static RestClientConfig createSampleClientConfig() {
.of("io.quarkus.rest.client.reactive.runtime.RestClientCDIDelegateBuilderTest$MyHostnameVerifier1");
clientConfig.connectionTTL = Optional.of(10000); // value in milliseconds, will be converted to seconds
clientConfig.connectionPoolSize = Optional.of(103);
clientConfig.connectionPoolEnabled = Optional.of(false);
clientConfig.keepAliveEnabled = Optional.of(false);
clientConfig.maxRedirects = Optional.of(104);
clientConfig.followRedirects = Optional.of(true);
clientConfig.providers = Optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public class QuarkusRestClientProperties {
public static final String CONNECTION_POOL_SIZE = "io.quarkus.rest.client.connection-pool-size";

/**
* A boolean value used to determine whether the connection pool is enabled or disabled.
* A boolean value used to determine whether the keep alive is enabled or disabled.
*/
public static final String CONNECTION_POOL_ENABLED = "io.quarkus.rest.client.connection-pool-enabled";
public static final String KEEP_ALIVE_ENABLED = "io.quarkus.rest.client.keep-alive-enabled";

public static final String STATIC_HEADERS = "io.quarkus.rest.client.static-headers";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.jboss.resteasy.reactive.client.impl;

import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.CONNECTION_POOL_ENABLED;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.CONNECTION_POOL_SIZE;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.CONNECTION_TTL;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.CONNECT_TIMEOUT;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.KEEP_ALIVE_ENABLED;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.MAX_HEADER_SIZE;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.MAX_INITIAL_LINE_LENGTH;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.MAX_REDIRECTS;
Expand Down Expand Up @@ -156,13 +156,13 @@ public Vertx get() {
}
options.setMaxPoolSize((int) connectionPoolSize);

Object connectionPoolEnabled = configuration.getProperty(CONNECTION_POOL_ENABLED);
if (connectionPoolEnabled != null) {
Boolean enabled = (Boolean) connectionPoolEnabled;
Object keepAliveEnabled = configuration.getProperty(KEEP_ALIVE_ENABLED);
if (keepAliveEnabled != null) {
Boolean enabled = (Boolean) keepAliveEnabled;
options.setKeepAlive(enabled);

if (!enabled) {
log.debug("connectionPool disabled");
log.debug("keep alive disabled");
}
}

Expand Down

0 comments on commit 1cc9c5b

Please sign in to comment.