diff --git a/grpc-client-utils/src/main/java/org/hypertrace/core/grpcutils/client/DefaultTimeoutClientInterceptor.java b/grpc-client-utils/src/main/java/org/hypertrace/core/grpcutils/client/DefaultTimeoutClientInterceptor.java new file mode 100644 index 0000000..ab65d71 --- /dev/null +++ b/grpc-client-utils/src/main/java/org/hypertrace/core/grpcutils/client/DefaultTimeoutClientInterceptor.java @@ -0,0 +1,32 @@ +package org.hypertrace.core.grpcutils.client; + +import static java.util.Objects.isNull; + +import io.grpc.CallOptions; +import io.grpc.Channel; +import io.grpc.ClientCall; +import io.grpc.ClientInterceptor; +import io.grpc.Context; +import io.grpc.MethodDescriptor; +import java.time.Duration; +import java.util.concurrent.TimeUnit; +import javax.annotation.Nonnull; +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class DefaultTimeoutClientInterceptor implements ClientInterceptor { + private final @Nonnull Duration defaultTimeout; + + @Override + public ClientCall interceptCall( + MethodDescriptor methodDescriptor, CallOptions providedOptions, Channel next) { + + if (isNull(providedOptions.getDeadline()) && isNull(Context.current().getDeadline())) { + return next.newCall( + methodDescriptor, + providedOptions.withDeadlineAfter(defaultTimeout.toMillis(), TimeUnit.MILLISECONDS)); + } + + return next.newCall(methodDescriptor, providedOptions); + } +}