You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem is in middleware some information is unknown, those information can only be determined later.
For example, I have an instrumentation middleware, which measures the request handling time (egress - ingress), and logs the error when request handling hit any errors, obviously those kind of information is only available when request reaches the handler implements business logic. When instrumentation middleware receives the request, it has no way to know how long will processing current request take or whether current request will encounter any errors.
However, in golang the only way to pass information when process http request is using the context object, the information flows from upstream to downstream, I can wrap any values or objects into context using context.WithValue and export the key, downstream modules can use the key to extract those data out from context, but I cannot do it in reverse order, passing the information from downstream back to upstream modules like middleware.
Seems I need a mechanism to setup a callback hook in http process pipeline, when request pops up from the stack/pipeline, but I cannot find such a mechanism is supported by go-chi, does anyone have a good suggestion on this? Thanks.
The text was updated successfully, but these errors were encountered:
Each middleware that needs to bubble up information from handlers downstream needs to create their own storage in the context, e.g. a pointer to a value that will be filled downstream.
There's no other way around, as contexts get unwrapped on a way back up.
I agree it would be useful to have a common middleware pkg for this.
Each middleware that needs to bubble up information from handlers downstream needs to create their own storage in the context, e.g. a pointer to a value that will be filled downstream.
There's no other way around, as contexts get unwrapped on a way back up.
I agree it would be useful to have a common middleware pkg for this.
Thanks @VojtechVitek, are you suggesting that, I can do like this:
The problem is in middleware some information is unknown, those information can only be determined later.
For example, I have an instrumentation middleware, which measures the request handling time (egress - ingress), and logs the error when request handling hit any errors, obviously those kind of information is only available when request reaches the handler implements business logic. When instrumentation middleware receives the request, it has no way to know how long will processing current request take or whether current request will encounter any errors.
However, in golang the only way to pass information when process http request is using the context object, the information flows from upstream to downstream, I can wrap any values or objects into context using
context.WithValue
and export the key, downstream modules can use the key to extract those data out from context, but I cannot do it in reverse order, passing the information from downstream back to upstream modules like middleware.Seems I need a mechanism to setup a callback hook in http process pipeline, when request pops up from the stack/pipeline, but I cannot find such a mechanism is supported by go-chi, does anyone have a good suggestion on this? Thanks.
The text was updated successfully, but these errors were encountered: