My understanding is that a request and all associated interceptors are executed on the same thread, per request.
On Wed, Jul 3, 2019, 12:06 PM 'Penn (Dapeng) Zhang' via grpc.io < [email protected]> wrote: > >The execution of the service method itself may not lie on the same thread > to execute the interceptor, is that right? > Yes. > > I only see you set the principal, but don't see where/in what thread you > read the principal, so I can not give you an answer. What's not possible > for you to achieve your goal with ThreadLocal<A > <https://cs.corp.google.com/piper///depot/google3/java/com/google/net/base/GoogleIpTosParams.java?l=53&ct=xref_jump_to_def&gsn=GoogleIpTosParams&rcl=256078461> > tomicReference<Principal>> as below? > > > static final ThreadLocal<A > <https://cs.corp.google.com/piper///depot/google3/java/com/google/net/base/GoogleIpTosParams.java?l=53&ct=xref_jump_to_def&gsn=GoogleIpTosParams&rcl=256078461> > tomicReference<Principal>> threadLocal = new ThreadLocal<>(); > > public <ReqT, RespT> interceptCall( > ServerCall<ReqT, RespT> serverCall, Metadata headers, > ServerCallHandler<ReqT, RespT> next) { > Principal principal = ... // get the identify from the request > final A > <https://cs.corp.google.com/piper///depot/google3/java/com/google/net/base/GoogleIpTosParams.java?l=53&ct=xref_jump_to_def&gsn=GoogleIpTosParams&rcl=256078461> > tomicReference<Principal> principalRef = new A > <https://cs.corp.google.com/piper///depot/google3/java/com/google/net/base/GoogleIpTosParams.java?l=53&ct=xref_jump_to_def&gsn=GoogleIpTosParams&rcl=256078461> > tomicReference<Principal>(principal); > threadLoacal.set(principalRef); > return next.startCall( > new SimpleForwardingServerCall<>(call) { > public void close(Status status, Metadata trailers) { > principalRef.set(null); // clear the identity > } > }, > headers); > } > > > > > On Sunday, June 30, 2019 at 7:55:29 PM UTC-7, garlicbulb wrote: >> >> We have a gPRC service that needs to set auth/identity information in a >> ThreadLocal variable in a class for it to correctly call another service. >> The gPRC service gets the auth/identiy information from request so I am >> thinking to use interceptor. >> >> To start, I have some code looking as follows. >> >> public class ImpersonationInterceptor { >> public <ReqT, RespT> interceptCall( >> ServerCall<ReqT, RespT> serverCall, Metadata headers, >> ServerCallHandler<ReqT, RespT> next) { >> Principal principal = ... // get the identify from the request >> >> AuthContext.setPrincipal(principal); // underneath it uses a >> ThreadLocal. >> >> return next.startCall( >> new SimpleForwardingServerCall<>(call) { >> public void close(Status status, Metadata trailers) { >> AuthContext.setPrincipal(null); // clear the identity >> } >> } >> ) >> } >> } >> >> Questions. >> >> * The execution of the service method itself may not lie on the same >> thread to execute the interceptor, is that right? >> * If true, the above is not gonna work, then the question is, what is the >> canonical way to set ThreadLocal variable in gRPC world? I know gRPC has >> Context support since 0.12, but in my case, I have to use the AuthContext's >> ThreadLocal mechanism. >> >> Thanks very much in advance. >> >> Chen >> > -- > You received this message because you are subscribed to the Google Groups " > grpc.io" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/grpc-io/1eab44e6-5657-4a35-9e1c-8350daab6c8c%40googlegroups.com > <https://groups.google.com/d/msgid/grpc-io/1eab44e6-5657-4a35-9e1c-8350daab6c8c%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "grpc.io" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/CAD7-yfsG3TabHk5Vp75QdEizUX%3Dmh%2BZcW9zNiH04EMGQBzrd0A%40mail.gmail.com.
