Skip to content

Commit

Permalink
Feat(deadline) removed wasted DeadlineSetupClientInterceptor calls in…
Browse files Browse the repository at this point in the history
… runtime
  • Loading branch information
Бацура Сергей Александрович authored and Бацура Сергей Александрович committed Sep 4, 2024
1 parent e14b9fe commit 7505c7d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -52,15 +49,13 @@
@AutoConfigureBefore(GrpcClientAutoConfiguration.class)
public class GrpcClientDeadlineAutoConfiguration {

private final CallOptions.Key<Duration> 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
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([email protected])
*/
Expand All @@ -37,17 +37,17 @@
@RequiredArgsConstructor
public class DeadlineSetupClientInterceptor implements ClientInterceptor {

private final CallOptions.Key<Duration> deadlineDuration;
private final Duration defaultDeadline;

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
final MethodDescriptor<ReqT, RespT> 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));
if (defaultDeadline != null) {
return next.newCall(method,
callOptions.withDeadlineAfter(defaultDeadline.toMillis(), TimeUnit.MILLISECONDS));
} else {
return next.newCall(method, callOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<ClientInterceptor> actual = new ArrayList<>(this.registry.getClientInterceptors());
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
Expand Down

0 comments on commit 7505c7d

Please sign in to comment.