Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.common.base.Preconditions;
import io.grpc.*;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -18,21 +19,38 @@
* implicit deadline will be used instead.
*/
public class DefaultDeadlineInterceptor implements ClientInterceptor {
private final long duration;
private final TimeUnit timeUnit;
private Duration duration;

public DefaultDeadlineInterceptor(long duration, TimeUnit timeUnit) {
Preconditions.checkArgument(duration > 0, "duration must be greater than zero");
Preconditions.checkNotNull(timeUnit, "timeUnit");
public DefaultDeadlineInterceptor(Duration duration) {
Preconditions.checkNotNull(duration, "duration");
Preconditions.checkArgument(!duration.isNegative(), "duration must be greater than zero");

this.duration = duration;
this.timeUnit = timeUnit;
}

/**
* Get the current default deadline duration.
*
* @return the current default deadline duration
*/
public Duration getDuration() {
return duration;
}

/**
* Set a new default deadline duration.
*
* @param duration the new default deadline duration
*/
public void setDuration(Duration duration) {
this.duration = duration;
}

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
// Only add a deadline if no other deadline has been set.
if (callOptions.getDeadline() == null && Context.current().getDeadline() == null) {
callOptions = callOptions.withDeadlineAfter(duration, timeUnit);
callOptions = callOptions.withDeadlineAfter(duration.toMillis(), TimeUnit.MILLISECONDS);
}

return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
import io.grpc.*;
import org.junit.Test;

import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings("ALL")
@SuppressWarnings("ConstantConditions")
public class DefaultDeadlineInterceptorTest {
@Test
public void interceptorShouldAddDeadlineWhenAbsent() {
AtomicBoolean called = new AtomicBoolean(false);

DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(1, TimeUnit.HOURS);
DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(Duration.ofHours(1));

interceptor.interceptCall(null, CallOptions.DEFAULT, new Channel() {
@Override
Expand All @@ -45,7 +46,7 @@ public String authority() {
public void interceptorShouldNotModifyExplicitDeadline() {
AtomicBoolean called = new AtomicBoolean(false);

DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(1, TimeUnit.HOURS);
DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(Duration.ofHours(1));

interceptor.interceptCall(null, CallOptions.DEFAULT.withDeadlineAfter(10, TimeUnit.HOURS), new Channel() {
@Override
Expand All @@ -68,7 +69,7 @@ public String authority() {
public void interceptorShouldNotModifyContextDeadline() throws Exception {
AtomicBoolean called = new AtomicBoolean(false);

DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(1, TimeUnit.HOURS);
DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(Duration.ofHours(1));

Context.current().withDeadlineAfter(10, TimeUnit.HOURS, Executors.newSingleThreadScheduledExecutor()).run(() -> {
interceptor.interceptCall(null, CallOptions.DEFAULT, new Channel() {
Expand Down