go-grpc-middleware icon indicating copy to clipboard operation
go-grpc-middleware copied to clipboard

grpc_logrus multithreading concerns

Open pkartner opened this issue 8 years ago • 3 comments

In the sample code it is encouraged to create a log entry, store it in the context and use it in the rest of the request to log. As far as I can tell this is not thread safe. What is thought process behind this example? I would like to use this issue as a place to have a discussion on logging in this manner.

pkartner avatar Sep 19 '17 07:09 pkartner

The log entry is then cloned every time on new request and the clone is put inside the Context variable of the request. That request is then handled by the dispatching goroutine. Most request handling is done in a single synchronous block, and unless someone dispatches more goroutines from it, it is safe.

Any concerns around that?

mwitkow avatar Sep 24 '17 09:09 mwitkow

Has this been a conscious decision? It is not uncommon to open new goroutines in a single request. Like you said most request handling is done in a single synchronous block. Logging in this way would force you to adopt two logging approaches, one for in the main request thread and one for the child threads.

pkartner avatar Oct 09 '17 13:10 pkartner

Yes, this was a concious decision. In our codebase, whenever we fan out to sub to go routines (rare) we make sure that we clone the relevant objects and pass them explicitly.

Would you like to contribute documentation clarification around this?

mwitkow avatar Oct 20 '17 06:10 mwitkow

This is pretty old and we know have v2 code with different structure. Let us know if this is still important, we can reopen.

bwplotka avatar Mar 19 '23 01:03 bwplotka