From 28b073d8431fadac30ecafa298bdfaf5a6ae6d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D1=86=D1=83=D1=80=D0=B0=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Sat, 3 Aug 2024 15:27:43 +0300 Subject: [PATCH 01/20] Feat(deadline) add deadline property for client --- .../GrpcClientDeadlineAutoConfiguration.java | 83 ++++++++++++++++++ .../client/config/GrpcChannelProperties.java | 31 +++++++ .../DeadlineSetupClientInterceptor.java | 55 ++++++++++++ ...itional-spring-configuration-metadata.json | 6 ++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../test/config/BaseAutoConfiguration.java | 3 +- .../DefaultClientInterceptorTest.java | 2 + .../boot/grpc/test/setup/DeadlineTests.java | 86 +++++++++++++++++++ 8 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java create mode 100644 grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java create mode 100644 tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java new file mode 100644 index 000000000..74000992e --- /dev/null +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2016-2024 The gRPC-Spring Authors + * + * 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. + */ + +package net.devh.boot.grpc.client.autoconfigure; + +import static java.util.Objects.requireNonNull; + +import java.time.Duration; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import io.grpc.CallOptions; +import io.grpc.stub.AbstractStub; +import lombok.extern.slf4j.Slf4j; +import net.devh.boot.grpc.client.config.GrpcChannelProperties; +import net.devh.boot.grpc.client.config.GrpcChannelsProperties; +import net.devh.boot.grpc.client.inject.StubTransformer; +import net.devh.boot.grpc.client.interceptor.DeadlineSetupClientInterceptor; +import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor; + +/** + * The deadline autoconfiguration for the client. + * + *

+ * You can disable this config by using: + *

+ * + *
+ * @ImportAutoConfiguration(exclude = GrpcClientDeadlineAutoConfiguration.class)
+ * 
+ * + * @author Sergei Batsura (batsura.sa@gmail.com) + */ +@Slf4j +@Configuration(proxyBeanMethods = false) +@AutoConfigureBefore(GrpcClientAutoConfiguration.class) +public class GrpcClientDeadlineAutoConfiguration { + + private final CallOptions.Key deadlineDuration = + CallOptions.Key.createWithDefault("deadlineDuration", null); + + /** + * Creates a {@link StubTransformer} bean that will add the call credentials to the created stubs. + * + * @param props The properties for deadline configuration. + * @return The StubTransformer bean that will add the deadline from properties. + * @see AbstractStub#withDeadline(io.grpc.Deadline) + */ + @Bean + StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { + requireNonNull(props, "properties"); + + return (name, stub) -> { + GrpcChannelProperties channelProps = props.getChannel(name); + if (channelProps != null && channelProps.getDeadline() != null) { + return stub.withOption(deadlineDuration, channelProps.getDeadline()); + } else { + return stub; + } + }; + } + + @GrpcGlobalClientInterceptor + DeadlineSetupClientInterceptor deadlineClientInterceptor() { + return new DeadlineSetupClientInterceptor(deadlineDuration); + } + +} diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index 2d06a4e2c..bda69a4b4 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -118,6 +118,34 @@ public void setAddress(final String address) { this.address = address == null ? null : URI.create(address); } + // -------------------------------------------------- + // Target Deadline + // -------------------------------------------------- + + private Duration deadline = null; + + /** + * Gets the connection deadline. + * + * @return The connection deadline or null + * @see #setDeadline(Duration) + */ + public Duration getDeadline() { + return this.deadline; + } + + /** + * Set the deadline for the stub. If nothing is configured then the deadline will not be used by default. If zero + * value is configured then the deadline will immediately. + * + * @param deadline The connection deadline or null. + * + * @see #setDeadline(Duration) + */ + public void setDeadline(Duration deadline) { + this.deadline = deadline; + } + // -------------------------------------------------- // defaultLoadBalancingPolicy // -------------------------------------------------- @@ -480,6 +508,9 @@ public void copyDefaultsFrom(final GrpcChannelProperties config) { if (this.address == null) { this.address = config.address; } + if (this.deadline == null) { + this.deadline = config.deadline; + } if (this.defaultLoadBalancingPolicy == null) { this.defaultLoadBalancingPolicy = config.defaultLoadBalancingPolicy; } diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java new file mode 100644 index 000000000..c1e7aa74a --- /dev/null +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016-2024 The gRPC-Spring Authors + * + * 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. + */ + +package net.devh.boot.grpc.client.interceptor; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +import io.grpc.CallOptions; +import io.grpc.Channel; +import io.grpc.ClientCall; +import io.grpc.ClientInterceptor; +import io.grpc.MethodDescriptor; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * Deadline setup client interceptor that create new deadline object. + * + * @author Sergei Batsura (batsura.sa@gmail.com) + */ +@Slf4j +@GrpcGlobalClientInterceptor +@RequiredArgsConstructor +public class DeadlineSetupClientInterceptor implements ClientInterceptor { + + private final CallOptions.Key deadlineDuration; + + @Override + public ClientCall interceptCall( + final MethodDescriptor method, + final CallOptions callOptions, + final Channel next) { + + Duration duration = callOptions.getOption(deadlineDuration); + if (duration != null) { + return next.newCall(method, callOptions.withDeadlineAfter(duration.toMillis(), TimeUnit.MILLISECONDS)); + } else { + return next.newCall(method, callOptions); + } + } +} diff --git a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 44903c96e..ac55457c5 100644 --- a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -107,6 +107,12 @@ "description": "Connection timeout at application startup. If set to a positive duration instructs a client to connect to GRPC-endpoint when GRPC stub is created.", "defaultValue": 0 }, + { + "name": "grpc.client.GLOBAL.deadline", + "type": "java.time.Duration", + "sourceType": "net.devh.boot.grpc.client.config.GrpcChannelProperties", + "description": "A deadline is used to specify a point in time past which a client is unwilling to wait for a response from a server" + }, { "name": "grpc.client.GLOBAL.security.authority-override", "type": "java.lang.String", diff --git a/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index c43842c1c..293ff8e33 100644 --- a/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -4,3 +4,4 @@ net.devh.boot.grpc.client.autoconfigure.GrpcClientHealthAutoConfiguration net.devh.boot.grpc.client.autoconfigure.GrpcClientMicrometerTraceAutoConfiguration net.devh.boot.grpc.client.autoconfigure.GrpcClientSecurityAutoConfiguration net.devh.boot.grpc.client.autoconfigure.GrpcDiscoveryClientAutoConfiguration +net.devh.boot.grpc.client.autoconfigure.GrpcClientDeadlineAutoConfiguration diff --git a/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java b/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java index bea9d69ff..786d4365d 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java @@ -20,6 +20,7 @@ import org.springframework.context.annotation.Configuration; import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration; +import net.devh.boot.grpc.client.autoconfigure.GrpcClientDeadlineAutoConfiguration; import net.devh.boot.grpc.common.autoconfigure.GrpcCommonCodecAutoConfiguration; import net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration; import net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration; @@ -28,7 +29,7 @@ @Configuration @ImportAutoConfiguration({GrpcCommonCodecAutoConfiguration.class, GrpcServerAutoConfiguration.class, GrpcServerFactoryAutoConfiguration.class, GrpcServerSecurityAutoConfiguration.class, - GrpcClientAutoConfiguration.class}) + GrpcClientAutoConfiguration.class, GrpcClientDeadlineAutoConfiguration.class}) public class BaseAutoConfiguration { } diff --git a/tests/src/test/java/net/devh/boot/grpc/test/interceptor/DefaultClientInterceptorTest.java b/tests/src/test/java/net/devh/boot/grpc/test/interceptor/DefaultClientInterceptorTest.java index 17a51b931..2db639b4c 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/interceptor/DefaultClientInterceptorTest.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/interceptor/DefaultClientInterceptorTest.java @@ -33,6 +33,7 @@ import io.micrometer.core.instrument.binder.grpc.MetricCollectingClientInterceptor; import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor; import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration; +import net.devh.boot.grpc.client.interceptor.DeadlineSetupClientInterceptor; import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorRegistry; import net.devh.boot.grpc.client.metrics.MetricsClientInterceptor; @@ -54,6 +55,7 @@ void testDefaultInterceptors() { expected.add(this.applicationContext.getBean(MetricCollectingClientInterceptor.class)); expected.add(this.applicationContext.getBean(MetricsClientInterceptor.class)); expected.add(this.applicationContext.getBean(ObservationGrpcClientInterceptor.class)); + expected.add(this.applicationContext.getBean(DeadlineSetupClientInterceptor.class)); final List actual = new ArrayList<>(this.registry.getClientInterceptors()); assertThat(actual).containsExactlyInAnyOrderElementsOf(expected); diff --git a/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java b/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java new file mode 100644 index 000000000..e172e1742 --- /dev/null +++ b/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2016-2024 The gRPC-Spring Authors + * + * 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. + */ + +package net.devh.boot.grpc.test.setup; + +import static io.grpc.Status.DEADLINE_EXCEEDED; +import static net.devh.boot.grpc.test.util.GrpcAssertions.assertStatus; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.concurrent.ExecutionException; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import io.grpc.StatusRuntimeException; +import io.grpc.internal.testing.StreamRecorder; +import io.grpc.stub.StreamObserver; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import net.devh.boot.grpc.client.config.GrpcChannelProperties; +import net.devh.boot.grpc.test.config.BaseAutoConfiguration; +import net.devh.boot.grpc.test.config.ServiceConfiguration; +import net.devh.boot.grpc.test.proto.SomeType; + +/** + * These tests check the property {@link GrpcChannelProperties#getDeadline()}. + */ +@Slf4j +@SpringBootTest(properties = { + "grpc.client.GLOBAL.address=localhost:9090", + "grpc.client.GLOBAL.deadline=1s", + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", +}) +@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) +public class DeadlineTests extends AbstractSimpleServerClientTest { + + @Test + @SneakyThrows + @DirtiesContext + void testServiceStubDeadlineEnabledAndSuccessful() { + log.info("--- Starting test with unsuccessful and than successful call ---"); + final StreamRecorder streamRecorder1 = StreamRecorder.create(); + StreamObserver echo1 = this.testServiceStub.echo(streamRecorder1); + assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); + + final StreamRecorder streamRecorder2 = StreamRecorder.create(); + StreamObserver echo2 = testServiceStub.echo(streamRecorder2); + echo2.onNext(SomeType.getDefaultInstance()); + assertNull(streamRecorder2.getError()); + assertNotNull(streamRecorder2.firstValue().get().getVersion()); + log.info("--- Test completed --- "); + } + + @Test + @SneakyThrows + @DirtiesContext + void testServiceStubDeadlineEnabledAndUnsuccessful() { + log.info("--- Starting test with unsuccessful call ---"); + final StreamRecorder streamRecorder = StreamRecorder.create(); + this.testServiceStub.echo(streamRecorder); + assertThrows(ExecutionException.class, () -> streamRecorder.firstValue().get()); + assertNotNull(streamRecorder.getError()); + assertEquals(StatusRuntimeException.class, streamRecorder.getError().getClass()); + assertStatus(DEADLINE_EXCEEDED.getCode(), (StatusRuntimeException) streamRecorder.getError()); + log.info("--- Test completed --- "); + } + +} From 0da4da704c3a8271a65725c0f047160b4a17970e Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Wed, 4 Sep 2024 08:17:58 +0300 Subject: [PATCH 02/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java Co-authored-by: ST-DDT --- .../net/devh/boot/grpc/client/config/GrpcChannelProperties.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index bda69a4b4..1074a46f6 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -125,7 +125,7 @@ public void setAddress(final String address) { private Duration deadline = null; /** - * Gets the connection deadline. + * Gets the default deadline for each new call. * * @return The connection deadline or null * @see #setDeadline(Duration) From 3b295c7889e5c48c94c8a95b0b312ed79ec1a360 Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Wed, 4 Sep 2024 08:18:13 +0300 Subject: [PATCH 03/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java Co-authored-by: ST-DDT --- .../devh/boot/grpc/client/config/GrpcChannelProperties.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index 1074a46f6..69c166ba0 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -135,8 +135,9 @@ public Duration getDeadline() { } /** - * Set the deadline for the stub. If nothing is configured then the deadline will not be used by default. If zero - * value is configured then the deadline will immediately. + * Set the default deadline duration for new calls (on a per call basis). + * If nothing is configured then the deadline will not be used by default. + * If zero value is configured, then the call will timeout immediately. * * @param deadline The connection deadline or null. * From e14b9fe2ea4093116ff3f8247b00d5b188a2513b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D1=86=D1=83=D1=80=D0=B0=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Wed, 4 Sep 2024 09:19:35 +0300 Subject: [PATCH 04/20] Feat(deadline) changed zero deadline behavior, javadoc corrections --- .../GrpcClientDeadlineAutoConfiguration.java | 13 ++-- .../client/config/GrpcChannelProperties.java | 8 +-- .../DeadlineSetupClientInterceptor.java | 2 +- .../boot/grpc/test/setup/DeadlineTests.java | 70 +++++++++---------- 4 files changed, 45 insertions(+), 48 deletions(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java index 74000992e..9782cc404 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java @@ -25,7 +25,8 @@ import org.springframework.context.annotation.Configuration; import io.grpc.CallOptions; -import io.grpc.stub.AbstractStub; +import io.grpc.Channel; +import io.grpc.MethodDescriptor; import lombok.extern.slf4j.Slf4j; import net.devh.boot.grpc.client.config.GrpcChannelProperties; import net.devh.boot.grpc.client.config.GrpcChannelsProperties; @@ -55,11 +56,12 @@ public class GrpcClientDeadlineAutoConfiguration { CallOptions.Key.createWithDefault("deadlineDuration", null); /** - * Creates a {@link StubTransformer} bean that will add the call credentials to the created stubs. + * Creates a {@link StubTransformer} bean that will add the deadlineDuration to the callOptions for using in + * DeadlineSetupClientInterceptor. * * @param props The properties for deadline configuration. - * @return The StubTransformer bean that will add the deadline from properties. - * @see AbstractStub#withDeadline(io.grpc.Deadline) + * @return The StubTransformer bean that will add the deadlineDuration from properties to the callOptions. + * @see DeadlineSetupClientInterceptor#interceptCall(MethodDescriptor, CallOptions, Channel) */ @Bean StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { @@ -67,7 +69,8 @@ StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { return (name, stub) -> { GrpcChannelProperties channelProps = props.getChannel(name); - if (channelProps != null && channelProps.getDeadline() != null) { + if (channelProps != null && channelProps.getDeadline() != null + && channelProps.getDeadline().toMillis() > 0L) { return stub.withOption(deadlineDuration, channelProps.getDeadline()); } else { return stub; diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index 69c166ba0..c77216735 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -32,6 +32,7 @@ import org.springframework.util.unit.DataSize; import org.springframework.util.unit.DataUnit; +import io.grpc.CallOptions; import io.grpc.LoadBalancerRegistry; import io.grpc.ManagedChannelBuilder; import io.grpc.NameResolverProvider; @@ -135,13 +136,12 @@ public Duration getDeadline() { } /** - * Set the default deadline duration for new calls (on a per call basis). - * If nothing is configured then the deadline will not be used by default. - * If zero value is configured, then the call will timeout immediately. + * Set the default deadline duration for new calls (on a per call basis). If nothing is configured then the deadline + * will not be used by default. If zero value is configured, the deadline will not be used. * * @param deadline The connection deadline or null. * - * @see #setDeadline(Duration) + * @see CallOptions#withDeadlineAfter(long, TimeUnit) */ public void setDeadline(Duration deadline) { this.deadline = deadline; diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java index c1e7aa74a..4ff2329c5 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java @@ -28,7 +28,7 @@ import lombok.extern.slf4j.Slf4j; /** - * Deadline setup client interceptor that create new deadline object. + * Deadline setup client interceptor that create new deadline instance from deadlineDuration. * * @author Sergei Batsura (batsura.sa@gmail.com) */ diff --git a/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java b/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java index e172e1742..b3054e6d9 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java @@ -16,9 +16,6 @@ package net.devh.boot.grpc.test.setup; -import static io.grpc.Status.DEADLINE_EXCEEDED; -import static net.devh.boot.grpc.test.util.GrpcAssertions.assertStatus; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -30,7 +27,6 @@ import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import io.grpc.StatusRuntimeException; import io.grpc.internal.testing.StreamRecorder; import io.grpc.stub.StreamObserver; import lombok.SneakyThrows; @@ -43,44 +39,42 @@ /** * These tests check the property {@link GrpcChannelProperties#getDeadline()}. */ -@Slf4j -@SpringBootTest(properties = { - "grpc.client.GLOBAL.address=localhost:9090", - "grpc.client.GLOBAL.deadline=1s", - "grpc.client.GLOBAL.negotiationType=PLAINTEXT", -}) -@SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) -public class DeadlineTests extends AbstractSimpleServerClientTest { +public class DeadlineTests { - @Test - @SneakyThrows - @DirtiesContext - void testServiceStubDeadlineEnabledAndSuccessful() { - log.info("--- Starting test with unsuccessful and than successful call ---"); - final StreamRecorder streamRecorder1 = StreamRecorder.create(); - StreamObserver echo1 = this.testServiceStub.echo(streamRecorder1); - assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); + @Slf4j + @SpringBootTest(properties = { + "grpc.client.GLOBAL.address=localhost:9090", + "grpc.client.GLOBAL.deadline=1s", + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", + }) + @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) + static class DeadlineSetupTest extends AbstractSimpleServerClientTest { + @Test + @SneakyThrows + @DirtiesContext + void testServiceStubDeadlineEnabledAndSuccessful() { + log.info("--- Starting test with unsuccessful and than successful call ---"); + final StreamRecorder streamRecorder1 = StreamRecorder.create(); + StreamObserver echo1 = this.testServiceStub.echo(streamRecorder1); + assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); - final StreamRecorder streamRecorder2 = StreamRecorder.create(); - StreamObserver echo2 = testServiceStub.echo(streamRecorder2); - echo2.onNext(SomeType.getDefaultInstance()); - assertNull(streamRecorder2.getError()); - assertNotNull(streamRecorder2.firstValue().get().getVersion()); - log.info("--- Test completed --- "); + final StreamRecorder streamRecorder2 = StreamRecorder.create(); + StreamObserver echo2 = testServiceStub.echo(streamRecorder2); + echo2.onNext(SomeType.getDefaultInstance()); + assertNull(streamRecorder2.getError()); + assertNotNull(streamRecorder2.firstValue().get().getVersion()); + log.info("--- Test completed --- "); + } } - @Test - @SneakyThrows - @DirtiesContext - void testServiceStubDeadlineEnabledAndUnsuccessful() { - log.info("--- Starting test with unsuccessful call ---"); - final StreamRecorder streamRecorder = StreamRecorder.create(); - this.testServiceStub.echo(streamRecorder); - assertThrows(ExecutionException.class, () -> streamRecorder.firstValue().get()); - assertNotNull(streamRecorder.getError()); - assertEquals(StatusRuntimeException.class, streamRecorder.getError().getClass()); - assertStatus(DEADLINE_EXCEEDED.getCode(), (StatusRuntimeException) streamRecorder.getError()); - log.info("--- Test completed --- "); + @Slf4j + @SpringBootTest(properties = { + "grpc.client.GLOBAL.address=localhost:9090", + "grpc.client.GLOBAL.deadline=0s", + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", + }) + @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) + static class ZeroDeadlineSetupTest extends AbstractSimpleServerClientTest { } } From 7505c7d7e2669bd8aed7ff26eacc4696f9a17966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D1=86=D1=83=D1=80=D0=B0=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Wed, 4 Sep 2024 15:53:09 +0300 Subject: [PATCH 05/20] Feat(deadline) removed wasted DeadlineSetupClientInterceptor calls in runtime --- .../GrpcClientDeadlineAutoConfiguration.java | 20 +++++-------------- .../DeadlineSetupClientInterceptor.java | 10 +++++----- .../DefaultClientInterceptorTest.java | 2 -- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java index 9782cc404..8fdb9773e 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java @@ -18,8 +18,6 @@ import static java.util.Objects.requireNonNull; -import java.time.Duration; - import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -32,7 +30,6 @@ import net.devh.boot.grpc.client.config.GrpcChannelsProperties; import net.devh.boot.grpc.client.inject.StubTransformer; import net.devh.boot.grpc.client.interceptor.DeadlineSetupClientInterceptor; -import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor; /** * The deadline autoconfiguration for the client. @@ -52,15 +49,13 @@ @AutoConfigureBefore(GrpcClientAutoConfiguration.class) public class GrpcClientDeadlineAutoConfiguration { - private final CallOptions.Key deadlineDuration = - CallOptions.Key.createWithDefault("deadlineDuration", null); - /** - * Creates a {@link StubTransformer} bean that will add the deadlineDuration to the callOptions for using in - * DeadlineSetupClientInterceptor. + * Creates a {@link StubTransformer} bean with interceptor that will call withDeadlineAfter with deadline from + * props. + * * * @param props The properties for deadline configuration. - * @return The StubTransformer bean that will add the deadlineDuration from properties to the callOptions. + * @return The StubTransformer bean with interceptor if deadline is configured. * @see DeadlineSetupClientInterceptor#interceptCall(MethodDescriptor, CallOptions, Channel) */ @Bean @@ -71,16 +66,11 @@ StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { GrpcChannelProperties channelProps = props.getChannel(name); if (channelProps != null && channelProps.getDeadline() != null && channelProps.getDeadline().toMillis() > 0L) { - return stub.withOption(deadlineDuration, channelProps.getDeadline()); + return stub.withInterceptors(new DeadlineSetupClientInterceptor(channelProps.getDeadline())); } else { return stub; } }; } - @GrpcGlobalClientInterceptor - DeadlineSetupClientInterceptor deadlineClientInterceptor() { - return new DeadlineSetupClientInterceptor(deadlineDuration); - } - } diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java index 4ff2329c5..60b1a6081 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java @@ -28,7 +28,7 @@ import lombok.extern.slf4j.Slf4j; /** - * Deadline setup client interceptor that create new deadline instance from deadlineDuration. + * Deadline setup client interceptor that create new deadline instance from defaultDeadline. * * @author Sergei Batsura (batsura.sa@gmail.com) */ @@ -37,7 +37,7 @@ @RequiredArgsConstructor public class DeadlineSetupClientInterceptor implements ClientInterceptor { - private final CallOptions.Key deadlineDuration; + private final Duration defaultDeadline; @Override public ClientCall interceptCall( @@ -45,9 +45,9 @@ public ClientCall interceptCall( final CallOptions callOptions, final Channel next) { - Duration duration = callOptions.getOption(deadlineDuration); - if (duration != null) { - return next.newCall(method, callOptions.withDeadlineAfter(duration.toMillis(), TimeUnit.MILLISECONDS)); + if (defaultDeadline != null) { + return next.newCall(method, + callOptions.withDeadlineAfter(defaultDeadline.toMillis(), TimeUnit.MILLISECONDS)); } else { return next.newCall(method, callOptions); } diff --git a/tests/src/test/java/net/devh/boot/grpc/test/interceptor/DefaultClientInterceptorTest.java b/tests/src/test/java/net/devh/boot/grpc/test/interceptor/DefaultClientInterceptorTest.java index 2db639b4c..17a51b931 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/interceptor/DefaultClientInterceptorTest.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/interceptor/DefaultClientInterceptorTest.java @@ -33,7 +33,6 @@ import io.micrometer.core.instrument.binder.grpc.MetricCollectingClientInterceptor; import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor; import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration; -import net.devh.boot.grpc.client.interceptor.DeadlineSetupClientInterceptor; import net.devh.boot.grpc.client.interceptor.GlobalClientInterceptorRegistry; import net.devh.boot.grpc.client.metrics.MetricsClientInterceptor; @@ -55,7 +54,6 @@ void testDefaultInterceptors() { expected.add(this.applicationContext.getBean(MetricCollectingClientInterceptor.class)); expected.add(this.applicationContext.getBean(MetricsClientInterceptor.class)); expected.add(this.applicationContext.getBean(ObservationGrpcClientInterceptor.class)); - expected.add(this.applicationContext.getBean(DeadlineSetupClientInterceptor.class)); final List actual = new ArrayList<>(this.registry.getClientInterceptors()); assertThat(actual).containsExactlyInAnyOrderElementsOf(expected); From 42ebf40ef80b1f871caea50292709b23473132e4 Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Thu, 5 Sep 2024 08:15:40 +0300 Subject: [PATCH 06/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java Co-authored-by: ST-DDT --- .../grpc/client/interceptor/DeadlineSetupClientInterceptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java index 60b1a6081..5a7910c86 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java @@ -45,7 +45,7 @@ public ClientCall interceptCall( final CallOptions callOptions, final Channel next) { - if (defaultDeadline != null) { + if (defaultDeadline != null && callOptions.getDeadline() == null) { return next.newCall(method, callOptions.withDeadlineAfter(defaultDeadline.toMillis(), TimeUnit.MILLISECONDS)); } else { From 809bc5b276ad3d90186d8c7cc4388450f63a28ba Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Thu, 5 Sep 2024 08:15:49 +0300 Subject: [PATCH 07/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java Co-authored-by: ST-DDT --- .../grpc/client/interceptor/DeadlineSetupClientInterceptor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java index 5a7910c86..5fd315955 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java @@ -33,7 +33,6 @@ * @author Sergei Batsura (batsura.sa@gmail.com) */ @Slf4j -@GrpcGlobalClientInterceptor @RequiredArgsConstructor public class DeadlineSetupClientInterceptor implements ClientInterceptor { From a8bbe07c5415f4a18c6cf22a2433d8a6abe72cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D1=86=D1=83=D1=80=D0=B0=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Thu, 5 Sep 2024 08:54:49 +0300 Subject: [PATCH 08/20] Feat(deadline) replaced bean deadlineStubTransformer with deadlineGrpcChannelConfigurer --- .../GrpcClientDeadlineAutoConfiguration.java | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java index 8fdb9773e..31eb0d932 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java @@ -18,6 +18,8 @@ import static java.util.Objects.requireNonNull; +import java.time.Duration; + import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -26,9 +28,8 @@ import io.grpc.Channel; import io.grpc.MethodDescriptor; import lombok.extern.slf4j.Slf4j; -import net.devh.boot.grpc.client.config.GrpcChannelProperties; +import net.devh.boot.grpc.client.channelfactory.GrpcChannelConfigurer; import net.devh.boot.grpc.client.config.GrpcChannelsProperties; -import net.devh.boot.grpc.client.inject.StubTransformer; import net.devh.boot.grpc.client.interceptor.DeadlineSetupClientInterceptor; /** @@ -50,25 +51,21 @@ public class GrpcClientDeadlineAutoConfiguration { /** - * Creates a {@link StubTransformer} bean with interceptor that will call withDeadlineAfter with deadline from + * Creates a {@link GrpcChannelConfigurer} bean with interceptor that will call withDeadlineAfter with deadline from * props. * - * * @param props The properties for deadline configuration. * @return The StubTransformer bean with interceptor if deadline is configured. * @see DeadlineSetupClientInterceptor#interceptCall(MethodDescriptor, CallOptions, Channel) */ @Bean - StubTransformer deadlineStubTransformer(final GrpcChannelsProperties props) { + GrpcChannelConfigurer deadlineGrpcChannelConfigurer(final GrpcChannelsProperties props) { requireNonNull(props, "properties"); - return (name, stub) -> { - GrpcChannelProperties channelProps = props.getChannel(name); - if (channelProps != null && channelProps.getDeadline() != null - && channelProps.getDeadline().toMillis() > 0L) { - return stub.withInterceptors(new DeadlineSetupClientInterceptor(channelProps.getDeadline())); - } else { - return stub; + return (channel, name) -> { + Duration deadline = props.getChannel(name).getDeadline(); + if (deadline != null && deadline.toMillis() > 0L) { + channel.intercept(new DeadlineSetupClientInterceptor(deadline)); } }; } From 5a219a70d0335db1dca41b01590e4be321b37f22 Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Thu, 5 Sep 2024 13:41:42 +0300 Subject: [PATCH 09/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java Co-authored-by: ST-DDT --- .../autoconfigure/GrpcClientDeadlineAutoConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java index 31eb0d932..308d843c9 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java @@ -56,7 +56,7 @@ public class GrpcClientDeadlineAutoConfiguration { * * @param props The properties for deadline configuration. * @return The StubTransformer bean with interceptor if deadline is configured. - * @see DeadlineSetupClientInterceptor#interceptCall(MethodDescriptor, CallOptions, Channel) + * @see DeadlineSetupClientInterceptor */ @Bean GrpcChannelConfigurer deadlineGrpcChannelConfigurer(final GrpcChannelsProperties props) { From 24928659f4055e145f8b39f02870bb147382fff9 Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Thu, 5 Sep 2024 13:42:01 +0300 Subject: [PATCH 10/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java Co-authored-by: ST-DDT --- .../autoconfigure/GrpcClientDeadlineAutoConfiguration.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java index 308d843c9..ddf8127d6 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java @@ -51,8 +51,7 @@ public class GrpcClientDeadlineAutoConfiguration { /** - * Creates a {@link GrpcChannelConfigurer} bean with interceptor that will call withDeadlineAfter with deadline from - * props. + * Creates a {@link GrpcChannelConfigurer} bean applying the default deadline from config to each new call using a {@link ClientInterceptor}. * * @param props The properties for deadline configuration. * @return The StubTransformer bean with interceptor if deadline is configured. From dd26ff42b6f881dd06229bfd196c51674a46fb19 Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Thu, 5 Sep 2024 13:42:27 +0300 Subject: [PATCH 11/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java Co-authored-by: ST-DDT --- .../devh/boot/grpc/client/config/GrpcChannelProperties.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index c77216735..fdcbc28e9 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -136,8 +136,9 @@ public Duration getDeadline() { } /** - * Set the default deadline duration for new calls (on a per call basis). If nothing is configured then the deadline - * will not be used by default. If zero value is configured, the deadline will not be used. + * Set the default deadline duration for new calls (on a per call basis). + * By default and if zero value is configured, the deadline will not be used. + * The default deadline will be ignored, if a deadline has been applied manually. * * @param deadline The connection deadline or null. * From f365af7ff5bc09541d95c1b6836a1928805fb46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D1=86=D1=83=D1=80=D0=B0=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Thu, 5 Sep 2024 15:14:57 +0300 Subject: [PATCH 12/20] Feat(deadline) add a test showing that the manually configured deadline takes precedence of the config one. --- .../GrpcClientDeadlineAutoConfiguration.java | 9 ++++----- .../grpc/client/config/GrpcChannelProperties.java | 6 +++--- .../additional-spring-configuration-metadata.json | 2 +- .../devh/boot/grpc/test/setup/DeadlineTests.java | 15 ++++++++++++++- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java index ddf8127d6..be8ef7586 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java @@ -24,9 +24,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import io.grpc.CallOptions; -import io.grpc.Channel; -import io.grpc.MethodDescriptor; +import io.grpc.ClientInterceptor; import lombok.extern.slf4j.Slf4j; import net.devh.boot.grpc.client.channelfactory.GrpcChannelConfigurer; import net.devh.boot.grpc.client.config.GrpcChannelsProperties; @@ -51,10 +49,11 @@ public class GrpcClientDeadlineAutoConfiguration { /** - * Creates a {@link GrpcChannelConfigurer} bean applying the default deadline from config to each new call using a {@link ClientInterceptor}. + * Creates a {@link GrpcChannelConfigurer} bean applying the default deadline from config to each new call using a + * {@link ClientInterceptor}. * * @param props The properties for deadline configuration. - * @return The StubTransformer bean with interceptor if deadline is configured. + * @return The GrpcChannelConfigurer bean with interceptor if deadline is configured. * @see DeadlineSetupClientInterceptor */ @Bean diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index fdcbc28e9..1ca339e96 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -136,9 +136,9 @@ public Duration getDeadline() { } /** - * Set the default deadline duration for new calls (on a per call basis). - * By default and if zero value is configured, the deadline will not be used. - * The default deadline will be ignored, if a deadline has been applied manually. + * Set the default deadline duration for new calls (on a per call basis). By default and if zero value is + * configured, the deadline will not be used. The default deadline will be ignored, if a deadline has been applied + * manually. * * @param deadline The connection deadline or null. * diff --git a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index ac55457c5..da447abcf 100644 --- a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -111,7 +111,7 @@ "name": "grpc.client.GLOBAL.deadline", "type": "java.time.Duration", "sourceType": "net.devh.boot.grpc.client.config.GrpcChannelProperties", - "description": "A deadline is used to specify a point in time past which a client is unwilling to wait for a response from a server" + "description": "The deadline is used to applying from config to each new call." }, { "name": "grpc.client.GLOBAL.security.authority-override", diff --git a/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java b/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java index b3054e6d9..1b71c9f3a 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @@ -55,7 +56,7 @@ static class DeadlineSetupTest extends AbstractSimpleServerClientTest { void testServiceStubDeadlineEnabledAndSuccessful() { log.info("--- Starting test with unsuccessful and than successful call ---"); final StreamRecorder streamRecorder1 = StreamRecorder.create(); - StreamObserver echo1 = this.testServiceStub.echo(streamRecorder1); + this.testServiceStub.echo(streamRecorder1); assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); final StreamRecorder streamRecorder2 = StreamRecorder.create(); @@ -75,6 +76,18 @@ void testServiceStubDeadlineEnabledAndSuccessful() { }) @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) static class ZeroDeadlineSetupTest extends AbstractSimpleServerClientTest { + + @Test + @SneakyThrows + @DirtiesContext + void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() { + log.info("--- Starting test that manually configured deadline takes precedence of the config one ---"); + final StreamRecorder streamRecorder1 = StreamRecorder.create(); + this.testServiceStub.withDeadlineAfter(1L, TimeUnit.SECONDS).echo(streamRecorder1); + assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); + log.info("--- Test completed --- "); + } + } } From 650a978539bb444d1dce24054260b564bf9e663e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D1=86=D1=83=D1=80=D0=B0=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 6 Sep 2024 09:54:17 +0300 Subject: [PATCH 13/20] Feat(timeout) changed the name of deadline property to timeout. --- ...> GrpcClientTimeoutAutoConfiguration.java} | 24 +++++++-------- .../client/config/GrpcChannelProperties.java | 29 +++++++++---------- ...ava => TimeoutSetupClientInterceptor.java} | 10 +++---- ...itional-spring-configuration-metadata.json | 4 +-- ...ot.autoconfigure.AutoConfiguration.imports | 2 +- .../test/config/BaseAutoConfiguration.java | 4 +-- ...dlineTests.java => TimeoutSetupTests.java} | 16 +++++----- 7 files changed, 44 insertions(+), 45 deletions(-) rename grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/{GrpcClientDeadlineAutoConfiguration.java => GrpcClientTimeoutAutoConfiguration.java} (68%) rename grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/{DeadlineSetupClientInterceptor.java => TimeoutSetupClientInterceptor.java} (77%) rename tests/src/test/java/net/devh/boot/grpc/test/setup/{DeadlineTests.java => TimeoutSetupTests.java} (89%) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java similarity index 68% rename from grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java rename to grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java index be8ef7586..1932c2456 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDeadlineAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java @@ -28,17 +28,17 @@ import lombok.extern.slf4j.Slf4j; import net.devh.boot.grpc.client.channelfactory.GrpcChannelConfigurer; import net.devh.boot.grpc.client.config.GrpcChannelsProperties; -import net.devh.boot.grpc.client.interceptor.DeadlineSetupClientInterceptor; +import net.devh.boot.grpc.client.interceptor.TimeoutSetupClientInterceptor; /** - * The deadline autoconfiguration for the client. + * The timeout autoconfiguration for the client. * *

* You can disable this config by using: *

* *
- * @ImportAutoConfiguration(exclude = GrpcClientDeadlineAutoConfiguration.class)
+ * @ImportAutoConfiguration(exclude = GrpcClientTimeoutAutoConfiguration.class)
  * 
* * @author Sergei Batsura (batsura.sa@gmail.com) @@ -46,24 +46,24 @@ @Slf4j @Configuration(proxyBeanMethods = false) @AutoConfigureBefore(GrpcClientAutoConfiguration.class) -public class GrpcClientDeadlineAutoConfiguration { +public class GrpcClientTimeoutAutoConfiguration { /** - * Creates a {@link GrpcChannelConfigurer} bean applying the default deadline from config to each new call using a + * Creates a {@link GrpcChannelConfigurer} bean applying the default timeout from config to each new call using a * {@link ClientInterceptor}. * - * @param props The properties for deadline configuration. - * @return The GrpcChannelConfigurer bean with interceptor if deadline is configured. - * @see DeadlineSetupClientInterceptor + * @param props The properties for timeout configuration. + * @return The GrpcChannelConfigurer bean with interceptor if timeout is configured. + * @see TimeoutSetupClientInterceptor */ @Bean - GrpcChannelConfigurer deadlineGrpcChannelConfigurer(final GrpcChannelsProperties props) { + GrpcChannelConfigurer timeoutGrpcChannelConfigurer(final GrpcChannelsProperties props) { requireNonNull(props, "properties"); return (channel, name) -> { - Duration deadline = props.getChannel(name).getDeadline(); - if (deadline != null && deadline.toMillis() > 0L) { - channel.intercept(new DeadlineSetupClientInterceptor(deadline)); + Duration timeout = props.getChannel(name).getTimeout(); + if (timeout != null && timeout.toMillis() > 0L) { + channel.intercept(new TimeoutSetupClientInterceptor(timeout)); } }; } diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index 1ca339e96..0fea730c4 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -120,32 +120,31 @@ public void setAddress(final String address) { } // -------------------------------------------------- - // Target Deadline + // Target Timeout // -------------------------------------------------- - private Duration deadline = null; + private Duration timeout = null; /** - * Gets the default deadline for each new call. + * Gets the default timeout for each new call. * - * @return The connection deadline or null - * @see #setDeadline(Duration) + * @return The connection timeout or null + * @see #setTimeout(Duration) */ - public Duration getDeadline() { - return this.deadline; + public Duration getTimeout() { + return this.timeout; } /** - * Set the default deadline duration for new calls (on a per call basis). By default and if zero value is - * configured, the deadline will not be used. The default deadline will be ignored, if a deadline has been applied - * manually. + * Set the default timeout duration for new calls (on a per call basis). By default and if zero value is configured, + * the timeout will not be used. The default timeout will be ignored, if a deadline has been applied manually. * - * @param deadline The connection deadline or null. + * @param timeout The connection timeout or null. * * @see CallOptions#withDeadlineAfter(long, TimeUnit) */ - public void setDeadline(Duration deadline) { - this.deadline = deadline; + public void setTimeout(Duration timeout) { + this.timeout = timeout; } // -------------------------------------------------- @@ -510,8 +509,8 @@ public void copyDefaultsFrom(final GrpcChannelProperties config) { if (this.address == null) { this.address = config.address; } - if (this.deadline == null) { - this.deadline = config.deadline; + if (this.timeout == null) { + this.timeout = config.timeout; } if (this.defaultLoadBalancingPolicy == null) { this.defaultLoadBalancingPolicy = config.defaultLoadBalancingPolicy; diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java similarity index 77% rename from grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java rename to grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java index 5fd315955..af05f00d4 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DeadlineSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java @@ -28,15 +28,15 @@ import lombok.extern.slf4j.Slf4j; /** - * Deadline setup client interceptor that create new deadline instance from defaultDeadline. + * Timeout setup client interceptor that create new deadline instance from the timeout. * * @author Sergei Batsura (batsura.sa@gmail.com) */ @Slf4j @RequiredArgsConstructor -public class DeadlineSetupClientInterceptor implements ClientInterceptor { +public class TimeoutSetupClientInterceptor implements ClientInterceptor { - private final Duration defaultDeadline; + private final Duration timeout; @Override public ClientCall interceptCall( @@ -44,9 +44,9 @@ public ClientCall interceptCall( final CallOptions callOptions, final Channel next) { - if (defaultDeadline != null && callOptions.getDeadline() == null) { + if (timeout != null && callOptions.getDeadline() == null) { return next.newCall(method, - callOptions.withDeadlineAfter(defaultDeadline.toMillis(), TimeUnit.MILLISECONDS)); + callOptions.withDeadlineAfter(timeout.toMillis(), TimeUnit.MILLISECONDS)); } else { return next.newCall(method, callOptions); } diff --git a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index da447abcf..06b079a01 100644 --- a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -108,10 +108,10 @@ "defaultValue": 0 }, { - "name": "grpc.client.GLOBAL.deadline", + "name": "grpc.client.GLOBAL.timeout", "type": "java.time.Duration", "sourceType": "net.devh.boot.grpc.client.config.GrpcChannelProperties", - "description": "The deadline is used to applying from config to each new call." + "description": "The timeout is used to applying from config to each new call. By default and if zero value is configured, the timeout will not be used. The default timeout will be ignored, if a deadline has been applied manually." }, { "name": "grpc.client.GLOBAL.security.authority-override", diff --git a/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 293ff8e33..15107b8b8 100644 --- a/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -4,4 +4,4 @@ net.devh.boot.grpc.client.autoconfigure.GrpcClientHealthAutoConfiguration net.devh.boot.grpc.client.autoconfigure.GrpcClientMicrometerTraceAutoConfiguration net.devh.boot.grpc.client.autoconfigure.GrpcClientSecurityAutoConfiguration net.devh.boot.grpc.client.autoconfigure.GrpcDiscoveryClientAutoConfiguration -net.devh.boot.grpc.client.autoconfigure.GrpcClientDeadlineAutoConfiguration +net.devh.boot.grpc.client.autoconfigure.GrpcClientTimeoutAutoConfiguration diff --git a/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java b/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java index 786d4365d..928c9a3da 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java @@ -20,7 +20,7 @@ import org.springframework.context.annotation.Configuration; import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration; -import net.devh.boot.grpc.client.autoconfigure.GrpcClientDeadlineAutoConfiguration; +import net.devh.boot.grpc.client.autoconfigure.GrpcClientTimeoutAutoConfiguration; import net.devh.boot.grpc.common.autoconfigure.GrpcCommonCodecAutoConfiguration; import net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration; import net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration; @@ -29,7 +29,7 @@ @Configuration @ImportAutoConfiguration({GrpcCommonCodecAutoConfiguration.class, GrpcServerAutoConfiguration.class, GrpcServerFactoryAutoConfiguration.class, GrpcServerSecurityAutoConfiguration.class, - GrpcClientAutoConfiguration.class, GrpcClientDeadlineAutoConfiguration.class}) + GrpcClientAutoConfiguration.class, GrpcClientTimeoutAutoConfiguration.class}) public class BaseAutoConfiguration { } diff --git a/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java b/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java similarity index 89% rename from tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java rename to tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java index 1b71c9f3a..c25089c1d 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/setup/DeadlineTests.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java @@ -38,22 +38,22 @@ import net.devh.boot.grpc.test.proto.SomeType; /** - * These tests check the property {@link GrpcChannelProperties#getDeadline()}. + * These tests check the property {@link GrpcChannelProperties#getTimeout()} ()}. */ -public class DeadlineTests { +public class TimeoutSetupTests { @Slf4j @SpringBootTest(properties = { "grpc.client.GLOBAL.address=localhost:9090", - "grpc.client.GLOBAL.deadline=1s", + "grpc.client.GLOBAL.timeout=1s", "grpc.client.GLOBAL.negotiationType=PLAINTEXT", }) @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) - static class DeadlineSetupTest extends AbstractSimpleServerClientTest { + static class TimeoutSetupTest extends AbstractSimpleServerClientTest { @Test @SneakyThrows @DirtiesContext - void testServiceStubDeadlineEnabledAndSuccessful() { + void testServiceStubTimeoutEnabledAndSuccessful() { log.info("--- Starting test with unsuccessful and than successful call ---"); final StreamRecorder streamRecorder1 = StreamRecorder.create(); this.testServiceStub.echo(streamRecorder1); @@ -71,17 +71,17 @@ void testServiceStubDeadlineEnabledAndSuccessful() { @Slf4j @SpringBootTest(properties = { "grpc.client.GLOBAL.address=localhost:9090", - "grpc.client.GLOBAL.deadline=0s", + "grpc.client.GLOBAL.timeout=0s", "grpc.client.GLOBAL.negotiationType=PLAINTEXT", }) @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) - static class ZeroDeadlineSetupTest extends AbstractSimpleServerClientTest { + static class ZeroTimeoutSetupTest extends AbstractSimpleServerClientTest { @Test @SneakyThrows @DirtiesContext void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() { - log.info("--- Starting test that manually configured deadline takes precedence of the config one ---"); + log.info("--- Starting test that manually configured deadline takes precedence of the config timeout ---"); final StreamRecorder streamRecorder1 = StreamRecorder.create(); this.testServiceStub.withDeadlineAfter(1L, TimeUnit.SECONDS).echo(streamRecorder1); assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); From 0ebcc0a6312a43959a6b989333b03075917a59b1 Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Fri, 6 Sep 2024 12:11:30 +0300 Subject: [PATCH 14/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java Co-authored-by: ST-DDT --- .../autoconfigure/GrpcClientTimeoutAutoConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java index 1932c2456..ff12ffb39 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java @@ -31,7 +31,7 @@ import net.devh.boot.grpc.client.interceptor.TimeoutSetupClientInterceptor; /** - * The timeout autoconfiguration for the client. + * The request timeout autoconfiguration for the client. * *

* You can disable this config by using: From 6ab4d0d65bd22ceda7a196555794388165945c84 Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Fri, 6 Sep 2024 12:11:51 +0300 Subject: [PATCH 15/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java Co-authored-by: ST-DDT --- .../autoconfigure/GrpcClientTimeoutAutoConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java index ff12ffb39..5392bd080 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java @@ -49,7 +49,7 @@ public class GrpcClientTimeoutAutoConfiguration { /** - * Creates a {@link GrpcChannelConfigurer} bean applying the default timeout from config to each new call using a + * Creates a {@link GrpcChannelConfigurer} bean applying the default request timeout from config to each new call using a * {@link ClientInterceptor}. * * @param props The properties for timeout configuration. From e6403b682545827ab9c01877c529781dbfb3e2cd Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Fri, 6 Sep 2024 12:13:12 +0300 Subject: [PATCH 16/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java Co-authored-by: ST-DDT --- .../grpc/client/interceptor/TimeoutSetupClientInterceptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java index af05f00d4..85f186433 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java @@ -28,7 +28,7 @@ import lombok.extern.slf4j.Slf4j; /** - * Timeout setup client interceptor that create new deadline instance from the timeout. + * A client interceptor configuring the default request timeout / deadline for each call. * * @author Sergei Batsura (batsura.sa@gmail.com) */ From 20dc1975b404e9c72e019a46573099163660dd7e Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Fri, 6 Sep 2024 12:13:43 +0300 Subject: [PATCH 17/20] Update grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json Co-authored-by: ST-DDT --- .../META-INF/additional-spring-configuration-metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 06b079a01..bc69852af 100644 --- a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -111,7 +111,7 @@ "name": "grpc.client.GLOBAL.timeout", "type": "java.time.Duration", "sourceType": "net.devh.boot.grpc.client.config.GrpcChannelProperties", - "description": "The timeout is used to applying from config to each new call. By default and if zero value is configured, the timeout will not be used. The default timeout will be ignored, if a deadline has been applied manually." + "description": "The default timeout is applied to each new call. By default, and if a zero value is configured, the timeout will not be set. The default timeout will be ignored if a deadline has been set manually." }, { "name": "grpc.client.GLOBAL.security.authority-override", From 9db699801738e0a71dd22f9b8557e76c07ab849b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D1=86=D1=83=D1=80=D0=B0=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 6 Sep 2024 12:46:17 +0300 Subject: [PATCH 18/20] Feat(timeout) constructor and test changed. --- .../GrpcClientTimeoutAutoConfiguration.java | 4 +-- .../TimeoutSetupClientInterceptor.java | 10 +++++-- .../grpc/test/setup/TimeoutSetupTests.java | 28 +++++++++++-------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java index 5392bd080..79b66ad68 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java @@ -49,8 +49,8 @@ public class GrpcClientTimeoutAutoConfiguration { /** - * Creates a {@link GrpcChannelConfigurer} bean applying the default request timeout from config to each new call using a - * {@link ClientInterceptor}. + * Creates a {@link GrpcChannelConfigurer} bean applying the default request timeout from config to each new call + * using a {@link ClientInterceptor}. * * @param props The properties for timeout configuration. * @return The GrpcChannelConfigurer bean with interceptor if timeout is configured. diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java index 85f186433..cfbec9300 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java @@ -16,6 +16,8 @@ package net.devh.boot.grpc.client.interceptor; +import static java.util.Objects.requireNonNull; + import java.time.Duration; import java.util.concurrent.TimeUnit; @@ -24,7 +26,6 @@ import io.grpc.ClientCall; import io.grpc.ClientInterceptor; import io.grpc.MethodDescriptor; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; /** @@ -33,18 +34,21 @@ * @author Sergei Batsura (batsura.sa@gmail.com) */ @Slf4j -@RequiredArgsConstructor public class TimeoutSetupClientInterceptor implements ClientInterceptor { private final Duration timeout; + public TimeoutSetupClientInterceptor(Duration timeout) { + this.timeout = requireNonNull(timeout, "timeout"); + } + @Override public ClientCall interceptCall( final MethodDescriptor method, final CallOptions callOptions, final Channel next) { - if (timeout != null && callOptions.getDeadline() == null) { + if (callOptions.getDeadline() == null) { return next.newCall(method, callOptions.withDeadlineAfter(timeout.toMillis(), TimeUnit.MILLISECONDS)); } else { diff --git a/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java b/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java index c25089c1d..b93c94d6b 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java @@ -50,6 +50,7 @@ public class TimeoutSetupTests { }) @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) static class TimeoutSetupTest extends AbstractSimpleServerClientTest { + @Test @SneakyThrows @DirtiesContext @@ -66,6 +67,21 @@ void testServiceStubTimeoutEnabledAndSuccessful() { assertNotNull(streamRecorder2.firstValue().get().getVersion()); log.info("--- Test completed --- "); } + + @Test + @SneakyThrows + @DirtiesContext + void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() { + log.info("--- Starting test that manually configured deadline takes precedence of the config timeout ---"); + final StreamRecorder streamRecorder = StreamRecorder.create(); + StreamObserver echo = + this.testServiceStub.withDeadlineAfter(5L, TimeUnit.SECONDS).echo(streamRecorder); + TimeUnit.SECONDS.sleep(2); + echo.onNext(SomeType.getDefaultInstance()); + assertNull(streamRecorder.getError()); + assertNotNull(streamRecorder.firstValue().get().getVersion()); + log.info("--- Test completed --- "); + } } @Slf4j @@ -76,18 +92,6 @@ void testServiceStubTimeoutEnabledAndSuccessful() { }) @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) static class ZeroTimeoutSetupTest extends AbstractSimpleServerClientTest { - - @Test - @SneakyThrows - @DirtiesContext - void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() { - log.info("--- Starting test that manually configured deadline takes precedence of the config timeout ---"); - final StreamRecorder streamRecorder1 = StreamRecorder.create(); - this.testServiceStub.withDeadlineAfter(1L, TimeUnit.SECONDS).echo(streamRecorder1); - assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); - log.info("--- Test completed --- "); - } - } } From 102d09e921b0e54070e8914af20057430a9fabec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D1=86=D1=83=D1=80=D0=B0=20=D0=A1=D0=B5=D1=80?= =?UTF-8?q?=D0=B3=D0=B5=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 6 Sep 2024 15:42:09 +0300 Subject: [PATCH 19/20] Feat(timeout) changed the name of timeout property to defaultRequestTimeout --- ...faultRequestTimeoutAutoConfiguration.java} | 14 ++++----- .../client/config/GrpcChannelProperties.java | 29 ++++++++++--------- ...RequestTimeoutSetupClientInterceptor.java} | 10 +++---- ...itional-spring-configuration-metadata.json | 2 +- ...ot.autoconfigure.AutoConfiguration.imports | 2 +- .../test/config/BaseAutoConfiguration.java | 4 +-- ...a => DefaultRequestTimeoutSetupTests.java} | 15 +++++----- 7 files changed, 39 insertions(+), 37 deletions(-) rename grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/{GrpcClientTimeoutAutoConfiguration.java => GrpcClientDefaultRequestTimeoutAutoConfiguration.java} (78%) rename grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/{TimeoutSetupClientInterceptor.java => DefaultRequestTimeoutSetupClientInterceptor.java} (77%) rename tests/src/test/java/net/devh/boot/grpc/test/setup/{TimeoutSetupTests.java => DefaultRequestTimeoutSetupTests.java} (87%) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDefaultRequestTimeoutAutoConfiguration.java similarity index 78% rename from grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java rename to grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDefaultRequestTimeoutAutoConfiguration.java index 79b66ad68..68fd79d36 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientTimeoutAutoConfiguration.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/autoconfigure/GrpcClientDefaultRequestTimeoutAutoConfiguration.java @@ -28,17 +28,17 @@ import lombok.extern.slf4j.Slf4j; import net.devh.boot.grpc.client.channelfactory.GrpcChannelConfigurer; import net.devh.boot.grpc.client.config.GrpcChannelsProperties; -import net.devh.boot.grpc.client.interceptor.TimeoutSetupClientInterceptor; +import net.devh.boot.grpc.client.interceptor.DefaultRequestTimeoutSetupClientInterceptor; /** - * The request timeout autoconfiguration for the client. + * The default request timeout autoconfiguration for the client. * *

* You can disable this config by using: *

* *
- * @ImportAutoConfiguration(exclude = GrpcClientTimeoutAutoConfiguration.class)
+ * @ImportAutoConfiguration(exclude = GrpcClientDefaultRequestTimeoutAutoConfiguration.class)
  * 
* * @author Sergei Batsura (batsura.sa@gmail.com) @@ -46,7 +46,7 @@ @Slf4j @Configuration(proxyBeanMethods = false) @AutoConfigureBefore(GrpcClientAutoConfiguration.class) -public class GrpcClientTimeoutAutoConfiguration { +public class GrpcClientDefaultRequestTimeoutAutoConfiguration { /** * Creates a {@link GrpcChannelConfigurer} bean applying the default request timeout from config to each new call @@ -54,16 +54,16 @@ public class GrpcClientTimeoutAutoConfiguration { * * @param props The properties for timeout configuration. * @return The GrpcChannelConfigurer bean with interceptor if timeout is configured. - * @see TimeoutSetupClientInterceptor + * @see DefaultRequestTimeoutSetupClientInterceptor */ @Bean GrpcChannelConfigurer timeoutGrpcChannelConfigurer(final GrpcChannelsProperties props) { requireNonNull(props, "properties"); return (channel, name) -> { - Duration timeout = props.getChannel(name).getTimeout(); + Duration timeout = props.getChannel(name).getDefaultRequestTimeout(); if (timeout != null && timeout.toMillis() > 0L) { - channel.intercept(new TimeoutSetupClientInterceptor(timeout)); + channel.intercept(new DefaultRequestTimeoutSetupClientInterceptor(timeout)); } }; } diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index 0fea730c4..9044678fc 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -120,31 +120,32 @@ public void setAddress(final String address) { } // -------------------------------------------------- - // Target Timeout + // Target DefaultRequestTimeout // -------------------------------------------------- - private Duration timeout = null; + private Duration defaultRequestTimeout = null; /** - * Gets the default timeout for each new call. + * Gets the default request timeout for each new call. * - * @return The connection timeout or null - * @see #setTimeout(Duration) + * @return The default request timeout or null + * @see #setDefaultRequestTimeout(Duration) */ - public Duration getTimeout() { - return this.timeout; + public Duration getDefaultRequestTimeout() { + return this.defaultRequestTimeout; } /** - * Set the default timeout duration for new calls (on a per call basis). By default and if zero value is configured, - * the timeout will not be used. The default timeout will be ignored, if a deadline has been applied manually. + * Set the default request timeout duration for new calls (on a per call basis). By default and if zero value is + * configured, the timeout will not be used. The default request timeout will be ignored, if a deadline has been + * applied manually. * - * @param timeout The connection timeout or null. + * @param defaultRequestTimeout the default request timeout or null. * * @see CallOptions#withDeadlineAfter(long, TimeUnit) */ - public void setTimeout(Duration timeout) { - this.timeout = timeout; + public void setDefaultRequestTimeout(Duration defaultRequestTimeout) { + this.defaultRequestTimeout = defaultRequestTimeout; } // -------------------------------------------------- @@ -509,8 +510,8 @@ public void copyDefaultsFrom(final GrpcChannelProperties config) { if (this.address == null) { this.address = config.address; } - if (this.timeout == null) { - this.timeout = config.timeout; + if (this.defaultRequestTimeout == null) { + this.defaultRequestTimeout = config.defaultRequestTimeout; } if (this.defaultLoadBalancingPolicy == null) { this.defaultLoadBalancingPolicy = config.defaultLoadBalancingPolicy; diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DefaultRequestTimeoutSetupClientInterceptor.java similarity index 77% rename from grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java rename to grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DefaultRequestTimeoutSetupClientInterceptor.java index cfbec9300..3a7e90cc6 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/TimeoutSetupClientInterceptor.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/interceptor/DefaultRequestTimeoutSetupClientInterceptor.java @@ -34,12 +34,12 @@ * @author Sergei Batsura (batsura.sa@gmail.com) */ @Slf4j -public class TimeoutSetupClientInterceptor implements ClientInterceptor { +public class DefaultRequestTimeoutSetupClientInterceptor implements ClientInterceptor { - private final Duration timeout; + private final Duration defaultRequestTimeout; - public TimeoutSetupClientInterceptor(Duration timeout) { - this.timeout = requireNonNull(timeout, "timeout"); + public DefaultRequestTimeoutSetupClientInterceptor(Duration defaultRequestTimeout) { + this.defaultRequestTimeout = requireNonNull(defaultRequestTimeout, "defaultRequestTimeout"); } @Override @@ -50,7 +50,7 @@ public ClientCall interceptCall( if (callOptions.getDeadline() == null) { return next.newCall(method, - callOptions.withDeadlineAfter(timeout.toMillis(), TimeUnit.MILLISECONDS)); + callOptions.withDeadlineAfter(defaultRequestTimeout.toMillis(), TimeUnit.MILLISECONDS)); } else { return next.newCall(method, callOptions); } diff --git a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index bc69852af..d259292f1 100644 --- a/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/grpc-client-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -108,7 +108,7 @@ "defaultValue": 0 }, { - "name": "grpc.client.GLOBAL.timeout", + "name": "grpc.client.GLOBAL.defaultRequestTimeout", "type": "java.time.Duration", "sourceType": "net.devh.boot.grpc.client.config.GrpcChannelProperties", "description": "The default timeout is applied to each new call. By default, and if a zero value is configured, the timeout will not be set. The default timeout will be ignored if a deadline has been set manually." diff --git a/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 15107b8b8..1608d654b 100644 --- a/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/grpc-client-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -4,4 +4,4 @@ net.devh.boot.grpc.client.autoconfigure.GrpcClientHealthAutoConfiguration net.devh.boot.grpc.client.autoconfigure.GrpcClientMicrometerTraceAutoConfiguration net.devh.boot.grpc.client.autoconfigure.GrpcClientSecurityAutoConfiguration net.devh.boot.grpc.client.autoconfigure.GrpcDiscoveryClientAutoConfiguration -net.devh.boot.grpc.client.autoconfigure.GrpcClientTimeoutAutoConfiguration +net.devh.boot.grpc.client.autoconfigure.GrpcClientDefaultRequestTimeoutAutoConfiguration diff --git a/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java b/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java index 928c9a3da..780cb77ee 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/config/BaseAutoConfiguration.java @@ -20,7 +20,7 @@ import org.springframework.context.annotation.Configuration; import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration; -import net.devh.boot.grpc.client.autoconfigure.GrpcClientTimeoutAutoConfiguration; +import net.devh.boot.grpc.client.autoconfigure.GrpcClientDefaultRequestTimeoutAutoConfiguration; import net.devh.boot.grpc.common.autoconfigure.GrpcCommonCodecAutoConfiguration; import net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration; import net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration; @@ -29,7 +29,7 @@ @Configuration @ImportAutoConfiguration({GrpcCommonCodecAutoConfiguration.class, GrpcServerAutoConfiguration.class, GrpcServerFactoryAutoConfiguration.class, GrpcServerSecurityAutoConfiguration.class, - GrpcClientAutoConfiguration.class, GrpcClientTimeoutAutoConfiguration.class}) + GrpcClientAutoConfiguration.class, GrpcClientDefaultRequestTimeoutAutoConfiguration.class}) public class BaseAutoConfiguration { } diff --git a/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java b/tests/src/test/java/net/devh/boot/grpc/test/setup/DefaultRequestTimeoutSetupTests.java similarity index 87% rename from tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java rename to tests/src/test/java/net/devh/boot/grpc/test/setup/DefaultRequestTimeoutSetupTests.java index b93c94d6b..25d5c6df7 100644 --- a/tests/src/test/java/net/devh/boot/grpc/test/setup/TimeoutSetupTests.java +++ b/tests/src/test/java/net/devh/boot/grpc/test/setup/DefaultRequestTimeoutSetupTests.java @@ -38,18 +38,18 @@ import net.devh.boot.grpc.test.proto.SomeType; /** - * These tests check the property {@link GrpcChannelProperties#getTimeout()} ()}. + * These tests check the property {@link GrpcChannelProperties#getDefaultRequestTimeout()}}. */ -public class TimeoutSetupTests { +public class DefaultRequestTimeoutSetupTests { @Slf4j @SpringBootTest(properties = { "grpc.client.GLOBAL.address=localhost:9090", - "grpc.client.GLOBAL.timeout=1s", + "grpc.client.GLOBAL.defaultRequestTimeout=1s", "grpc.client.GLOBAL.negotiationType=PLAINTEXT", }) @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) - static class TimeoutSetupTest extends AbstractSimpleServerClientTest { + static class DefaultRequestTimeoutSetupTest extends AbstractSimpleServerClientTest { @Test @SneakyThrows @@ -72,7 +72,8 @@ void testServiceStubTimeoutEnabledAndSuccessful() { @SneakyThrows @DirtiesContext void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() { - log.info("--- Starting test that manually configured deadline takes precedence of the config timeout ---"); + log.info( + "--- Starting test that manually configured deadline takes precedence of the config default request timeout ---"); final StreamRecorder streamRecorder = StreamRecorder.create(); StreamObserver echo = this.testServiceStub.withDeadlineAfter(5L, TimeUnit.SECONDS).echo(streamRecorder); @@ -87,11 +88,11 @@ void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() { @Slf4j @SpringBootTest(properties = { "grpc.client.GLOBAL.address=localhost:9090", - "grpc.client.GLOBAL.timeout=0s", + "grpc.client.GLOBAL.defaultRequestTimeout=0s", "grpc.client.GLOBAL.negotiationType=PLAINTEXT", }) @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) - static class ZeroTimeoutSetupTest extends AbstractSimpleServerClientTest { + static class ZeroDefaultRequestTimeoutSetupTest extends AbstractSimpleServerClientTest { } } From da2f32cacd43b249deb827540eb49a76ea1ee042 Mon Sep 17 00:00:00 2001 From: Sergei Batsura Date: Sat, 7 Sep 2024 04:48:20 +0300 Subject: [PATCH 20/20] Update grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java Co-authored-by: ST-DDT --- .../net/devh/boot/grpc/client/config/GrpcChannelProperties.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java index 9044678fc..301a89e73 100644 --- a/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java +++ b/grpc-client-spring-boot-starter/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java @@ -120,7 +120,7 @@ public void setAddress(final String address) { } // -------------------------------------------------- - // Target DefaultRequestTimeout + // defaultRequestTimeout // -------------------------------------------------- private Duration defaultRequestTimeout = null;