Skip to content

Commit

Permalink
Feat(timeout) changed the name of timeout property to defaultRequestT…
Browse files Browse the repository at this point in the history
…imeout
  • Loading branch information
Бацура Сергей Александрович authored and Бацура Сергей Александрович committed Sep 6, 2024
1 parent 9db6998 commit 102d09e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,42 @@
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.
*
* <p>
* You can disable this config by using:
* </p>
*
* <pre>
* <code>@ImportAutoConfiguration(exclude = GrpcClientTimeoutAutoConfiguration.class)</code>
* <code>@ImportAutoConfiguration(exclude = GrpcClientDefaultRequestTimeoutAutoConfiguration.class)</code>
* </pre>
*
* @author Sergei Batsura ([email protected])
*/
@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
* using a {@link ClientInterceptor}.
*
* @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));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

// --------------------------------------------------
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
* @author Sergei Batsura ([email protected])
*/
@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
Expand All @@ -50,7 +50,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<SomeType> streamRecorder = StreamRecorder.create();
StreamObserver<SomeType> echo =
this.testServiceStub.withDeadlineAfter(5L, TimeUnit.SECONDS).echo(streamRecorder);
Expand All @@ -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 {
}

}

0 comments on commit 102d09e

Please sign in to comment.