Skip to content

Commit

Permalink
Address comments by @be-hase
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhoon committed Jun 12, 2023
1 parent c2d1dd3 commit 89e9b1f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,12 @@ interface CoroutineServerInterceptor : AsyncServerInterceptor {
headers: Metadata,
next: ServerCallHandler<I, O>
): CompletableFuture<ServerCall.Listener<I>> {
check(call is AbstractServerCall) {
throw IllegalArgumentException(
"Cannot use ${AsyncServerInterceptor::class.java.name} with a non-Armeria gRPC server"
)
}
val executor = call.blockingExecutor() ?: call.eventLoop()

// COROUTINE_CONTEXT_KEY.get():
// It is necessary to propagate the CoroutineContext set by the previous CoroutineContextServerInterceptor.
// (The ArmeriaRequestCoroutineContext is also propagated by CoroutineContextServerInterceptor)
// GrpcContextElement.current():
// In gRPC-kotlin, the Coroutine Context is propagated using the gRPC Context.
return CoroutineScope(
executor.asCoroutineDispatcher() + ArmeriaRequestCoroutineContext(call.ctx()) +
COROUTINE_CONTEXT_KEY.get() + GrpcContextElement.current()
).future {
suspendedInterceptCall(call, headers, next)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,11 +948,6 @@ static GrpcStatusFunction toGrpcStatusFunction(
private ImmutableList.Builder<ServerInterceptor> interceptors() {
if (interceptors == null) {
interceptors = ImmutableList.builder();
if (USE_COROUTINE_CONTEXT_INTERCEPTOR) {
final ServerInterceptor coroutineContextInterceptor =
new ArmeriaCoroutineContextInterceptor(useBlockingTaskExecutor);
interceptors.add(coroutineContextInterceptor);
}
}
return interceptors;
}
Expand All @@ -966,6 +961,11 @@ private ImmutableList.Builder<ServerInterceptor> interceptors() {
*/
public GrpcService build() {
final HandlerRegistry handlerRegistry;
if (USE_COROUTINE_CONTEXT_INTERCEPTOR) {
final ServerInterceptor coroutineContextInterceptor =
new ArmeriaCoroutineContextInterceptor(useBlockingTaskExecutor);
interceptors().add(coroutineContextInterceptor);
}
if (!enableUnframedRequests && unframedGrpcErrorHandler != null) {
throw new IllegalStateException(
"'unframedGrpcErrorHandler' can only be set if unframed requests are enabled");
Expand All @@ -981,9 +981,9 @@ public GrpcService build() {
if (grpcHealthCheckService != null) {
registryBuilder.addService(grpcHealthCheckService.bindService(), null, ImmutableList.of());
}
final ImmutableList<ServerInterceptor> interceptors = interceptors().build();
if (!interceptors.isEmpty()) {
if (interceptors != null) {
final HandlerRegistry.Builder newRegistryBuilder = new HandlerRegistry.Builder();
final ImmutableList<ServerInterceptor> interceptors = this.interceptors.build();
for (Entry entry : registryBuilder.entries()) {
final MethodDescriptor<?, ?> methodDescriptor = entry.method();
final ServerServiceDefinition intercepted =
Expand Down

0 comments on commit 89e9b1f

Please sign in to comment.