From b5bc6f7a7d9dbbaa32da7e92c195fce5734e4c39 Mon Sep 17 00:00:00 2001 From: anton Date: Sat, 18 Apr 2020 05:18:30 +0300 Subject: [PATCH 1/2] keep alive fixes --- .../client/communicator/GrpcCommunicator.java | 6 +++++ .../GrpcCommunicatorImplementation.java | 24 +++++++++++++++++++ .../HttpCommunicationConfiguration.java | 3 ++- .../HttpCommunicationExecutor.java | 4 ++++ .../client/communicator/HttpCommunicator.java | 3 +++ .../HttpCommunicatorImplementation.java | 6 +++++ .../constants/HttpClientModuleConstants.java | 2 ++ .../SoapCommunicationConfiguration.java | 1 + .../SoapCommunicationExecutor.java | 6 +++++ .../client/communicator/SoapCommunicator.java | 2 ++ .../SoapCommunicatorImplementation.java | 6 +++++ 11 files changed, 62 insertions(+), 1 deletion(-) diff --git a/application-grpc-client/src/main/java/ru/art/grpc/client/communicator/GrpcCommunicator.java b/application-grpc-client/src/main/java/ru/art/grpc/client/communicator/GrpcCommunicator.java index e601361cc..4e1d5a885 100644 --- a/application-grpc-client/src/main/java/ru/art/grpc/client/communicator/GrpcCommunicator.java +++ b/application-grpc-client/src/main/java/ru/art/grpc/client/communicator/GrpcCommunicator.java @@ -64,6 +64,12 @@ static GrpcCommunicator grpcCommunicator(String host, int port, String path) { GrpcCommunicator secured(); + GrpcCommunicator keepAliveTimeNanos(long time); + + GrpcCommunicator keepAliveTimeOutNanos(long timeOut); + + GrpcCommunicator keepAliveWithoutCalls(boolean keepAliveWithoutCalls); + void shutdownChannel(); GrpcAsynchronousCommunicator asynchronous(); diff --git a/application-grpc-client/src/main/java/ru/art/grpc/client/communicator/GrpcCommunicatorImplementation.java b/application-grpc-client/src/main/java/ru/art/grpc/client/communicator/GrpcCommunicatorImplementation.java index 1cb2b43d7..923df6fc6 100644 --- a/application-grpc-client/src/main/java/ru/art/grpc/client/communicator/GrpcCommunicatorImplementation.java +++ b/application-grpc-client/src/main/java/ru/art/grpc/client/communicator/GrpcCommunicatorImplementation.java @@ -68,9 +68,15 @@ public class GrpcCommunicatorImplementation implements GrpcCommunicator, GrpcCom GrpcCommunicatorImplementation(GrpcCommunicationTargetConfiguration targetConfiguration) { configuration.setPath(validator.notEmptyField(targetConfiguration.path(), "path")); deadlineTimeout(targetConfiguration.timeout()); + keepAliveTimeNanos(targetConfiguration.keepAliveTimeNanos()); + keepAliveTimeNanos(targetConfiguration.keepAliveTimeOutNanos()); + keepAliveWithoutCalls(targetConfiguration.keepAliveWithoutCalls()); if (targetConfiguration.secured()) { secured(); } + if (targetConfiguration.waitForReady()) { + waitForReady(); + } if (isNotEmpty(targetConfiguration.url())) { configuration.setUrl(targetConfiguration.url()); return; @@ -141,6 +147,24 @@ public GrpcCommunicator secured() { return this; } + @Override + public GrpcCommunicator keepAliveTimeNanos(long time) { + configuration.setKeepAliveTimeNanos(time); + return this; + } + + @Override + public GrpcCommunicator keepAliveTimeOutNanos(long timeOut) { + configuration.setKeepAliveTimeNanos(timeOut); + return this; + } + + @Override + public GrpcCommunicator keepAliveWithoutCalls(boolean keepAliveWithoutCalls) { + configuration.setKeepAliveWithoutCalls(keepAliveWithoutCalls); + return null; + } + @Override public void shutdownChannel() { ManagedChannel channel = this.channel.safeValue(); diff --git a/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicationConfiguration.java b/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicationConfiguration.java index 2ee7d9e92..b6fa43be7 100644 --- a/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicationConfiguration.java +++ b/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicationConfiguration.java @@ -56,7 +56,6 @@ class HttpCommunicationConfiguration { private HttpCommunicationCancellationHandler cancellationHandler; private boolean chunkedBody; private boolean gzipCompressedBody; - private ConnectionClosingPolicy connectionClosingPolicy = CLOSE_AFTER_RESPONSE; private RequestConfig requestConfig = httpClientModule().getRequestConfig(); private HttpVersion httpProtocolVersion = httpClientModule().getHttpVersion(); private MimeToContentTypeMapper producesMimeType = httpClientModule().getProducesMimeTypeMapper(); @@ -68,4 +67,6 @@ class HttpCommunicationConfiguration { private CloseableHttpAsyncClient asynchronousClient; private List> requestValueInterceptors = linkedListOf(httpClientModule().getRequestValueInterceptors()); private List> responseValueInterceptors = linkedListOf(httpClientModule().getResponseValueInterceptors()); + private ConnectionClosingPolicy connectionClosingPolicy = CLOSE_AFTER_RESPONSE; + private boolean enableKeepAlive; } diff --git a/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicationExecutor.java b/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicationExecutor.java index 4d9e3fc70..7329945a7 100644 --- a/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicationExecutor.java +++ b/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicationExecutor.java @@ -53,7 +53,10 @@ import static ru.art.http.client.builder.HttpUriBuilder.*; import static ru.art.http.client.constants.HttpClientExceptionMessages.*; import static ru.art.http.client.constants.HttpClientModuleConstants.ConnectionClosingPolicy.CLOSE_AFTER_RESPONSE; +import static ru.art.http.client.constants.HttpClientModuleConstants.HTTP_HEADER_CONNECTION_CLOSE; +import static ru.art.http.client.constants.HttpClientModuleConstants.HTTP_HEADER_CONNECTION_KEEP_ALIVE; import static ru.art.http.client.module.HttpClientModule.*; +import static ru.art.http.constants.HttpHeaders.CONNECTION; import static ru.art.http.constants.HttpHeaders.KEEP_ALIVE; import static ru.art.logging.LoggingModule.*; import javax.annotation.*; @@ -159,6 +162,7 @@ private static HttpUriRequest buildRequest(HttpCommunicationConfiguration config .setCharset(configuration.getRequestContentCharset()) .setVersion(configuration.getHttpProtocolVersion()); configuration.getHeaders().forEach(requestBuilder::addHeader); + requestBuilder.addHeader(CONNECTION, configuration.isEnableKeepAlive() ? HTTP_HEADER_CONNECTION_KEEP_ALIVE : HTTP_HEADER_CONNECTION_CLOSE); if (isNull(request)) { return requestBuilder.build(); } diff --git a/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicator.java b/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicator.java index e661c27df..422c3e2f8 100644 --- a/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicator.java +++ b/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicator.java @@ -96,6 +96,8 @@ static HttpCommunicator httpCommunicator(HttpCommunicationTargetConfiguration ta HttpCommunicator requestEncoding(String encoding); + HttpCommunicator enableKeepAlive(); + Optional execute(RequestType request); Optional execute(); @@ -108,6 +110,7 @@ static HttpCommunicator httpCommunicator(HttpCommunicationTargetConfiguration ta HttpAsynchronousCommunicator asynchronous(); + interface HttpAsynchronousCommunicator { void closeAsynchronousClient(); diff --git a/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicatorImplementation.java b/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicatorImplementation.java index aaee219ed..18e1f58d5 100644 --- a/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicatorImplementation.java +++ b/application-http-client/src/main/java/ru/art/http/client/communicator/HttpCommunicatorImplementation.java @@ -232,6 +232,12 @@ public HttpCommunicator requestEncoding(String encoding) { return this; } + @Override + public HttpCommunicator enableKeepAlive() { + configuration.setEnableKeepAlive(true); + return this; + } + @Override public HttpCommunicator addRequestValueInterceptor(ValueInterceptor interceptor) { configuration.getRequestValueInterceptors().add(validator.notNullField(interceptor, "requestValueInterceptor")); diff --git a/application-http-client/src/main/java/ru/art/http/client/constants/HttpClientModuleConstants.java b/application-http-client/src/main/java/ru/art/http/client/constants/HttpClientModuleConstants.java index 0234eb354..1e09f5a0d 100644 --- a/application-http-client/src/main/java/ru/art/http/client/constants/HttpClientModuleConstants.java +++ b/application-http-client/src/main/java/ru/art/http/client/constants/HttpClientModuleConstants.java @@ -29,6 +29,8 @@ public interface HttpClientModuleConstants { int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 2; int DEFAULT_MAX_CONNECTIONS_TOTAL = 20; int DEFAULT_VALIDATE_AFTER_INACTIVITY_MILLIS = 4000; + String HTTP_HEADER_CONNECTION_CLOSE = "Close"; + String HTTP_HEADER_CONNECTION_KEEP_ALIVE = "Keep-Alive"; enum ConnectionClosingPolicy { CLOSE_AFTER_RESPONSE, diff --git a/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicationConfiguration.java b/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicationConfiguration.java index 4b136421e..dba1cab8f 100644 --- a/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicationConfiguration.java +++ b/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicationConfiguration.java @@ -75,6 +75,7 @@ class SoapCommunicationConfiguration { private List> responseValueInterceptors = linkedListOf(); private OperationIdSource operationIdSource = REQUEST; private ConnectionClosingPolicy connectionClosingPolicy = CLOSE_AFTER_RESPONSE; + private boolean enableKeepAlive; void validateRequiredFields() { boolean urlIsEmpty = isEmpty(url); diff --git a/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicationExecutor.java b/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicationExecutor.java index bbeb6cc79..6a8a57890 100644 --- a/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicationExecutor.java +++ b/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicationExecutor.java @@ -39,6 +39,9 @@ static Optional execute(SoapCommunicat configuration.getResponseInterceptors().forEach(httpCommunicator::addResponseInterceptor); configuration.getRequestValueInterceptors().forEach(interceptor -> httpCommunicator.addRequestValueInterceptor(cast(interceptor))); configuration.getResponseValueInterceptors().forEach(interceptor -> httpCommunicator.addResponseValueInterceptor(cast(interceptor))); + if (configuration.isEnableKeepAlive()) { + httpCommunicator.enableKeepAlive(); + } httpCommunicator .connectionClosingPolicy(configuration.getConnectionClosingPolicy()) .version(configuration.getHttpVersion()) @@ -68,6 +71,9 @@ static CompletableFuture> exe configuration.getRequestInterceptors().forEach(httpCommunicator::addResponseInterceptor); configuration.getRequestValueInterceptors().forEach(interceptor -> httpCommunicator.addRequestValueInterceptor(cast(interceptor))); configuration.getResponseValueInterceptors().forEach(interceptor -> httpCommunicator.addResponseValueInterceptor(cast(interceptor))); + if (configuration.isEnableKeepAlive()) { + httpCommunicator.enableKeepAlive(); + } httpCommunicator .connectionClosingPolicy(configuration.getConnectionClosingPolicy()) .version(configuration.getHttpVersion()) diff --git a/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicator.java b/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicator.java index f2b676370..13f8c7ae5 100644 --- a/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicator.java +++ b/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicator.java @@ -85,6 +85,8 @@ static SoapCommunicator soapCommunicator(HttpCommunicationTargetConfiguration ta SoapCommunicator version(HttpVersion httpVersion); + SoapCommunicator enableKeepAlive(); + Optional execute(RequestType request); SoapAsynchronousCommunicator asynchronous(); diff --git a/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicatorImplementation.java b/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicatorImplementation.java index d142ba022..99b8ee8a9 100644 --- a/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicatorImplementation.java +++ b/application-soap-client/src/main/java/ru/art/soap/client/communicator/SoapCommunicatorImplementation.java @@ -195,6 +195,12 @@ public SoapCommunicator version(HttpVersion httpVersion) { return this; } + @Override + public SoapCommunicator enableKeepAlive() { + configuration.setEnableKeepAlive(true); + return this; + } + @Override public Optional execute(RequestType request) { request = validator.notNullField(request, "request"); From 01847fd2ff786c05b17072028c32f83c5b64508b Mon Sep 17 00:00:00 2001 From: anton Date: Sun, 19 Apr 2020 08:46:28 +0300 Subject: [PATCH 2/2] revert --- application-test/build.gradle.kts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/application-test/build.gradle.kts b/application-test/build.gradle.kts index 5d0b1789e..6dc4266c1 100644 --- a/application-test/build.gradle.kts +++ b/application-test/build.gradle.kts @@ -1,3 +1,5 @@ +import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL +import org.gradle.api.tasks.testing.logging.TestLogEvent.* import ru.art.gradle.constants.lombok /* @@ -36,4 +38,11 @@ dependencies { annotationProcessor(lombok().inGradleNotation()) testAnnotationProcessor(lombok().inGradleNotation()) testImplementation("org.hsqldb", "hsqldb", "2+") +} + +tasks.withType { + testLogging { + events = setOf(PASSED, FAILED, SKIPPED) + exceptionFormat = FULL + } } \ No newline at end of file