-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add default timeout interceptor * refactor: remove unused slf4j
- Loading branch information
1 parent
bc147b5
commit e8afcbc
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...s/src/main/java/org/hypertrace/core/grpcutils/client/DefaultTimeoutClientInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |