Skip to content

Commit

Permalink
Merge pull request quarkusio#18804 from michalszynkiewicz/request-con…
Browse files Browse the repository at this point in the history
…text-interceptor-global

gRPC request context: activate before all the interceptors are invoked
  • Loading branch information
michalszynkiewicz authored Jul 19, 2021
2 parents 408a29d + fab4b5c commit 178d501
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import io.quarkus.grpc.runtime.config.GrpcServerBuildTimeConfig;
import io.quarkus.grpc.runtime.health.GrpcHealthEndpoint;
import io.quarkus.grpc.runtime.health.GrpcHealthStorage;
import io.quarkus.grpc.runtime.supports.context.GrpcRequestContextGrpcInterceptor;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.netty.deployment.MinNettyAllocatorMaxOrderBuildItem;
import io.quarkus.runtime.LaunchMode;
Expand Down Expand Up @@ -253,7 +254,7 @@ void validateBindableServices(ValidationPhaseBuildItem validationPhase,
Type mutinyBeanType = Type.create(GrpcDotNames.MUTINY_BEAN, org.jboss.jandex.Type.Kind.CLASS);
Type mutinyServiceType = Type.create(GrpcDotNames.MUTINY_SERVICE, org.jboss.jandex.Type.Kind.CLASS);
Type bindableServiceType = Type.create(GrpcDotNames.BINDABLE_SERVICE, org.jboss.jandex.Type.Kind.CLASS);
Predicate<Set<Type>> predicate = new Predicate<Set<Type>>() {
Predicate<Set<Type>> predicate = new Predicate<>() {
@Override
public boolean test(Set<Type> types) {
return types.contains(bindableServiceType) || types.contains(mutinyServiceType);
Expand Down Expand Up @@ -300,6 +301,10 @@ void registerBeans(BuildProducer<AdditionalBeanBuildItem> beans,

if (!bindables.isEmpty() || LaunchMode.current() == LaunchMode.DEVELOPMENT) {
beans.produce(AdditionalBeanBuildItem.unremovableOf(GrpcContainer.class));

// this makes GrpcRequestContextGrpcInterceptor registered as a global gRPC interceptor.
// Global interceptors are invoked before any of the per-service interceptors
beans.produce(AdditionalBeanBuildItem.unremovableOf(GrpcRequestContextGrpcInterceptor.class));
features.produce(new FeatureBuildItem(GRPC_SERVER));
} else {
logger.debug("Unable to find beans exposing the `BindableService` interface - not starting the gRPC server");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ List<ServerInterceptor> getSortedInterceptors() {
return Collections.emptyList();
}

return interceptors.stream().sorted(new Comparator<ServerInterceptor>() { // NOSONAR
return interceptors.stream().sorted(new Comparator<>() { // NOSONAR
@Override
public int compare(ServerInterceptor si1, ServerInterceptor si2) {
int p1 = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import io.quarkus.grpc.runtime.reflection.ReflectionService;
import io.quarkus.grpc.runtime.supports.CompressionInterceptor;
import io.quarkus.grpc.runtime.supports.blocking.BlockingServerInterceptor;
import io.quarkus.grpc.runtime.supports.context.GrpcRequestContextGrpcInterceptor;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.ShutdownContext;
Expand Down Expand Up @@ -427,8 +426,6 @@ private ServerServiceDefinition serviceWithInterceptors(Vertx vertx, Compression
interceptors.add(new BlockingServerInterceptor(vertx, list, devMode));
}
}
// Order matters! Request scope must be called first (on the event loop) and so should be last in the list...
interceptors.add(new GrpcRequestContextGrpcInterceptor());
return ServerInterceptors.intercept(service.definition, interceptors);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.quarkus.grpc.runtime.supports.context;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.spi.Prioritized;

import org.jboss.logging.Logger;

import io.grpc.ForwardingServerCallListener;
Expand All @@ -13,7 +16,8 @@
import io.vertx.core.Context;
import io.vertx.core.Vertx;

public class GrpcRequestContextGrpcInterceptor implements ServerInterceptor {
@ApplicationScoped
public class GrpcRequestContextGrpcInterceptor implements ServerInterceptor, Prioritized {
private static final Logger log = Logger.getLogger(GrpcRequestContextGrpcInterceptor.class.getName());

private final ManagedContext reqContext;
Expand Down Expand Up @@ -132,4 +136,9 @@ private boolean activateContext() {
return next.startCall(call, headers);
}
}

@Override
public int getPriority() {
return Integer.MAX_VALUE;
}
}

0 comments on commit 178d501

Please sign in to comment.