Skip to content

Commit 07ee78d

Browse files
authored
Merge pull request #52 from salesforce/feature/defaultdeadline
Mutable default deadline
2 parents 6d7eb68 + 63be009 commit 07ee78d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

grpc-contrib/src/main/java/com/salesforce/grpc/contrib/interceptor/DefaultDeadlineInterceptor.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.google.common.base.Preconditions;
1111
import io.grpc.*;
1212

13+
import java.time.Duration;
1314
import java.util.concurrent.TimeUnit;
1415

1516
/**
@@ -18,21 +19,38 @@
1819
* implicit deadline will be used instead.
1920
*/
2021
public class DefaultDeadlineInterceptor implements ClientInterceptor {
21-
private final long duration;
22-
private final TimeUnit timeUnit;
22+
private Duration duration;
2323

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

2828
this.duration = duration;
29-
this.timeUnit = timeUnit;
29+
}
30+
31+
/**
32+
* Get the current default deadline duration.
33+
*
34+
* @return the current default deadline duration
35+
*/
36+
public Duration getDuration() {
37+
return duration;
38+
}
39+
40+
/**
41+
* Set a new default deadline duration.
42+
*
43+
* @param duration the new default deadline duration
44+
*/
45+
public void setDuration(Duration duration) {
46+
this.duration = duration;
3047
}
3148

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

3856
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

grpc-contrib/src/test/java/com/salesforce/grpc/contrib/interceptor/DefaultDeadlineInterceptorTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@
1010
import io.grpc.*;
1111
import org.junit.Test;
1212

13+
import java.time.Duration;
1314
import java.util.concurrent.Executors;
1415
import java.util.concurrent.TimeUnit;
1516
import java.util.concurrent.atomic.AtomicBoolean;
1617

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

19-
@SuppressWarnings("ALL")
20+
@SuppressWarnings("ConstantConditions")
2021
public class DefaultDeadlineInterceptorTest {
2122
@Test
2223
public void interceptorShouldAddDeadlineWhenAbsent() {
2324
AtomicBoolean called = new AtomicBoolean(false);
2425

25-
DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(1, TimeUnit.HOURS);
26+
DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(Duration.ofHours(1));
2627

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

48-
DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(1, TimeUnit.HOURS);
49+
DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(Duration.ofHours(1));
4950

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

71-
DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(1, TimeUnit.HOURS);
72+
DefaultDeadlineInterceptor interceptor = new DefaultDeadlineInterceptor(Duration.ofHours(1));
7273

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

0 commit comments

Comments
 (0)