Skip to content

Commit

Permalink
Move the unary call if-condition in TimeoutServerInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
sorra committed Jul 17, 2023
1 parent 3798f55 commit f323668
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions api/src/main/java/io/grpc/TimeoutServerInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

/**
* An optional ServerInterceptor that can interrupt server calls that are running for too long time.
* In this way, it prevents problematic code from using up all threads.
*
* <p>You can add it to your server using the ServerBuilder#intercept(ServerInterceptor) method.
* <p>How to use: you can add it to your server using the ServerBuilder#intercept(ServerInterceptor) method.
*
* <p>Limitation: it only applies the timeout to unary calls (streaming calls will run without timeout).
*/
Expand All @@ -36,8 +37,13 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> serverCall,
Metadata metadata,
ServerCallHandler<ReqT, RespT> serverCallHandler) {
return new TimeoutServerCallListener<>(
serverCallHandler.startCall(serverCall, metadata), serverCall, serverTimeoutManager);
// Only intercepts unary calls because the timeout is inapplicable to streaming calls.
if (serverCall.getMethodDescriptor().getType().clientSendsOneMessage()) {
return new TimeoutServerCallListener<>(
serverCallHandler.startCall(serverCall, metadata), serverCall, serverTimeoutManager);
} else {
return serverCallHandler.startCall(serverCall, metadata);
}
}

/** A listener that intercepts the RPC method invocation for timeout control. */
Expand All @@ -57,17 +63,12 @@ private TimeoutServerCallListener(
}

/**
* Only intercepts unary calls because the timeout is inapplicable to streaming calls.
* Intercepts onHalfClose() because the RPC method is called in it. See
* io.grpc.stub.ServerCalls.UnaryServerCallHandler.UnaryServerCallListener
*/
@Override
public void onHalfClose() {
if (serverCall.getMethodDescriptor().getType().clientSendsOneMessage()) {
serverTimeoutManager.intercept(super::onHalfClose);
} else {
super.onHalfClose();
}
serverTimeoutManager.intercept(super::onHalfClose);
}
}
}

0 comments on commit f323668

Please sign in to comment.