Skip to content

Commit

Permalink
Feat(deadline) changed the name of deadline property to timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
Бацура Сергей Александрович authored and Бацура Сергей Александрович committed Sep 6, 2024
1 parent f365af7 commit 1aaef80
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 45 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.DeadlineSetupClientInterceptor;
import net.devh.boot.grpc.client.interceptor.TimeoutSetupClientInterceptor;

/**
* The deadline autoconfiguration for the client.
* The timeout autoconfiguration for the client.
*
* <p>
* You can disable this config by using:
* </p>
*
* <pre>
* <code>@ImportAutoConfiguration(exclude = GrpcClientDeadlineAutoConfiguration.class)</code>
* <code>@ImportAutoConfiguration(exclude = GrpcClientTimeoutAutoConfiguration.class)</code>
* </pre>
*
* @author Sergei Batsura ([email protected])
*/
@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));
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

// --------------------------------------------------
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@
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 ([email protected])
*/
@Slf4j
@RequiredArgsConstructor
public class DeadlineSetupClientInterceptor implements ClientInterceptor {
public class TimeoutSetupClientInterceptor implements ClientInterceptor {

private final Duration defaultDeadline;
private final Duration timeout;

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
final MethodDescriptor<ReqT, RespT> method,
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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.GrpcClientDeadlineAutoConfiguration
net.devh.boot.grpc.client.autoconfigure.GrpcClientTimeoutAutoConfiguration
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.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;
Expand All @@ -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 {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SomeType> streamRecorder1 = StreamRecorder.create();
this.testServiceStub.echo(streamRecorder1);
Expand All @@ -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<SomeType> streamRecorder1 = StreamRecorder.create();
this.testServiceStub.withDeadlineAfter(1L, TimeUnit.SECONDS).echo(streamRecorder1);
assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get());
Expand Down

0 comments on commit 1aaef80

Please sign in to comment.