On Tue, Feb 16, 2021 at 6:38 AM Philippe Laflamme <phili...@hopper.com> wrote:
> > This is true for calls to the ClientCall methods, but is not for calls to > ClientCalls.Listener. The calls to the ClientCall.Listener methods are in > the reverse order as the calls to ClientCall methods. So if both a and b > above "intercept" the ClientCall.Listener, then the ordering is something > like this: > > // startCall: b called first > a.startCall(b.startCall()) > > // onMessage: a called first > b.onMessage(a.onMessage()) > This is 100% the behavior you want; anything else leads to madness. Interceptors inject themselves to the boundary because the application and the gRPC library and allow you to do "whatever you want" with the API. This produces a layering, and to preserve the layering "who calls who" *must* be the *exact* opposite for callbacks as normal method calls. It may be easier to think about it not as which is called "first" but by its placement; think about whether an interceptor is nearer the application or the network. This makes it impossible for an interceptor to do something like: > > * setup some ThreadLocal state > * invoke the next interceptor's method (e.g.: start or onMessage) > * teardown ThreadLocal state > How so? Each method call may be on a different thread, so you have to setup and teardown the ThreadLocal every method: public void onMessage(ReqT req) { setupThreadLocal(); try { super.onMessage(req); } finally { teardownThreadLocal(); } } Don't create and throw exceptions inside interceptors, but if an exception happens it is okay to propagate it. This sort of code probably doesn't want the ThreadLocal to "leak," thus the try-finally. This is a common pattern for setting up tracing or logging context or > integrating with other frameworks that use ThreadLocal. > We actually do this ourselves for the io.grpc.Context which is stored in a ThreadLocal. -- 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 grpc-io+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/CA%2B4M1oPbadUK6pMH%2B5niNk72yYE6U9tQ%3D_Y1FRr7SzgHqqv_JQ%40mail.gmail.com.
smime.p7s
Description: S/MIME Cryptographic Signature