Skip to content

Commit

Permalink
Default timeout interceptor (#66)
Browse files Browse the repository at this point in the history
* chore: add default timeout interceptor

* refactor: remove unused slf4j
  • Loading branch information
aaron-steinfeld authored Dec 6, 2024
1 parent bc147b5 commit e8afcbc
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> 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);
}
}

0 comments on commit e8afcbc

Please sign in to comment.