From 159f1c86e508bf67ee08a7d42105afa2c2e1bf3a Mon Sep 17 00:00:00 2001 From: Pawel Date: Wed, 3 Feb 2021 16:04:14 +0100 Subject: Add possibility to modify the configuration for persistent connection Issue-ID: DCAEGEN2-1483 Signed-off-by: Pawel Change-Id: Ia5c0699359fe2317bea6177fe6dfce5c68579ba9 --- rest-services/http-client/pom.xml | 2 +- .../adapters/http/RxHttpClientFactory.java | 18 ++++++++++-- .../adapters/http/config/ConnectionPoolConfig.java | 33 ++++++++++++++++++++++ .../adapters/http/config/RxHttpClientConfig.java | 1 + .../services/adapters/http/RxHttpClientIT.java | 32 +++++++++++++++++++++ 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/config/ConnectionPoolConfig.java (limited to 'rest-services/http-client') diff --git a/rest-services/http-client/pom.xml b/rest-services/http-client/pom.xml index d9a11065..6b0ab0c1 100644 --- a/rest-services/http-client/pom.xml +++ b/rest-services/http-client/pom.xml @@ -28,7 +28,7 @@ org.onap.dcaegen2.services.sdk dcaegen2-services-sdk-rest-services - 1.6.0-SNAPSHOT + 1.7.0-SNAPSHOT org.onap.dcaegen2.services.sdk.rest.services diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientFactory.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientFactory.java index 118df52b..0567b1b4 100644 --- a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientFactory.java +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientFactory.java @@ -23,11 +23,13 @@ package org.onap.dcaegen2.services.sdk.rest.services.adapters.http; import io.netty.handler.ssl.SslContext; import io.vavr.control.Option; import org.jetbrains.annotations.NotNull; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.ConnectionPoolConfig; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.RxHttpClientConfig; import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys; import org.onap.dcaegen2.services.sdk.security.ssl.SslFactory; import org.onap.dcaegen2.services.sdk.security.ssl.TrustStoreKeys; import reactor.netty.http.client.HttpClient; +import reactor.netty.resources.ConnectionProvider; /** * @author Piotr Jaszczyk @@ -44,8 +46,11 @@ public final class RxHttpClientFactory { return new RxHttpClient(HttpClient.create()); } - public static RxHttpClient create(RxHttpClientConfig config) { - return createWithConfig(HttpClient.create(), config); + public static RxHttpClient create(RxHttpClientConfig config){ + return Option.of(config.connectionPool()) + .map(RxHttpClientFactory::createConnectionProvider) + .map(provider -> createWithConfig(HttpClient.create(provider), config)) + .getOrElse(createWithConfig(HttpClient.create(), config)); } public static RxHttpClient create(SecurityKeys securityKeys) { @@ -93,4 +98,13 @@ public final class RxHttpClientFactory { .map(retryConfig -> new RxHttpClient(httpClient, retryConfig)) .getOrElse(() -> new RxHttpClient(httpClient)); } + + @NotNull + private static ConnectionProvider createConnectionProvider(ConnectionPoolConfig connectionPoolConfig) { + return ConnectionProvider.builder("fixed") + .maxConnections(connectionPoolConfig.connectionPool()) + .maxIdleTime(connectionPoolConfig.maxIdleTime()) + .maxLifeTime(connectionPoolConfig.maxLifeTime()) + .build(); + } } diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/config/ConnectionPoolConfig.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/config/ConnectionPoolConfig.java new file mode 100644 index 00000000..d1d5c565 --- /dev/null +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/config/ConnectionPoolConfig.java @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START==================================== + * DCAEGEN2-SERVICES-SDK + * ========================================================= + * Copyright (C) 2021 Nokia. All rights reserved. + * ========================================================= + * 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. + * ============LICENSE_END===================================== + */ + +package org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config; + +import org.immutables.value.Value; + +import java.time.Duration; + +@Value.Immutable +public interface ConnectionPoolConfig { + + int connectionPool(); + Duration maxIdleTime(); + Duration maxLifeTime(); +} \ No newline at end of file diff --git a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/config/RxHttpClientConfig.java b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/config/RxHttpClientConfig.java index 78a88a47..b17a35cd 100644 --- a/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/config/RxHttpClientConfig.java +++ b/rest-services/http-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/config/RxHttpClientConfig.java @@ -26,4 +26,5 @@ import org.jetbrains.annotations.Nullable; @Value.Immutable public interface RxHttpClientConfig { @Nullable RetryConfig retryConfig(); + @Nullable ConnectionPoolConfig connectionPool(); } diff --git a/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java index 8d076e02..ff4f0297 100644 --- a/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java +++ b/rest-services/http-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/adapters/http/RxHttpClientIT.java @@ -26,6 +26,7 @@ import io.vavr.Tuple; import io.vavr.collection.HashSet; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.ImmutableConnectionPoolConfig; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.ImmutableRetryConfig; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.config.ImmutableRxHttpClientConfig; import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.exceptions.HttpException; @@ -336,6 +337,28 @@ class RxHttpClientIT { assertNoRetry(); } + @Test + void simpleGetWithCustomConnectionPool() throws Exception { + // given + final HttpRequest httpRequest = requestFor("/sample-get") + .method(HttpMethod.GET) + .build(); + final RxHttpClient cut = RxHttpClientFactory.create(ImmutableRxHttpClientConfig.builder() + .connectionPool(defaultConnectionPoolConfig()) + .build()); + + // when + final Mono bodyAsString = cut.call(httpRequest) + .doOnNext(HttpResponse::throwIfUnsuccessful) + .map(HttpResponse::bodyAsString); + + // then + StepVerifier.create(bodyAsString) + .expectNext("OK") + .expectComplete() + .verify(TIMEOUT); + } + private ImmutableHttpRequest.Builder requestFor(String path) throws MalformedURLException { return ImmutableHttpRequest.builder() .url(new URL("http", HTTP_SERVER.host(), HTTP_SERVER.port(), path).toString()); @@ -367,4 +390,13 @@ class RxHttpClientIT { private void assert400(HttpResponse httpResponse) { assertThat(httpResponse.statusCode()).isEqualTo(400); } + + private ImmutableConnectionPoolConfig defaultConnectionPoolConfig(){ + return ImmutableConnectionPoolConfig + .builder() + .connectionPool(1) + .maxIdleTime(Duration.ofSeconds(5)) + .maxLifeTime(Duration.ofSeconds(10)) + .build(); + } } -- cgit 1.2.3-korg