New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Google context #90
Comments
it seems there is already exist one. gorilla context. |
that's a global context. Doesn't scale as well. |
Are you talking about passing a context through the middlewares? I like that negroni currently works with |
Well, negroni works with http.HandlerFunc & http.Handler by wrapping them How about this?
Just as now, Wrap calls an http.Handler and then calls next itself, it could receive (and ignore) ctx, call http.Handler and the call next itself. Then:
And so forth. |
(as an aside, I agree with @pjebs: Google contexts seem to be the most idiomatic way to do this now. They work, they work under load, they work while avoiding global state, and they work well. |
I forked negroni and httprouter to pass down a Google Context, check my repo. All up to date and tests passing. I haven't updated the readme though. |
Just to follow up on my previous comment, I created a standard for myself for HTTP handlers to include a Google Context. Then I forked negroni and httprouter to follow my "standard". If anyone is interested on how they work together I can post some code. In the meantime here's my proposed new standard (to stop this nonsense of absolutely trying to be net/http Handler compliant when it's trivial to write wrappers to switch from one type of Handler to another) Anyway here it is: https://github.com/mikegleasonjr/go/blob/master/net/http/ctx/ctx.go |
Don't see the need to fork negroni to use Google context. |
I'm currently doing the same with my own glue code, but it'd be nice if negroni supported this natively. |
It looks like Google Context will be officially part of standard package starting Go1.7: https://tip.golang.org/doc/go1.7#context |
Indeed it is, but appears that it will be directly accessible on the |
I wonder if gorilla mux will load url routing values into the context rather than using their own |
One solution for it, wrap around
This is not good solution, and read it value.
|
I ripped out gorilla/context recently and now use Context to pass around parameters taken from the router library I use (httprouter). It's very easy in middleware/adapters to stuff values in the request context and then pull them back out again in a handler or middleware. |
@yanfali do you happen to have a gist of this? i'm trying to accomplish the same thing |
@catc this only works in go1.7, so if you are stuck on an earlier version of go, this won't help. Basically your middleware uses the following pattern: func ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
ctxt := context.Background()
// ...middleware does stuff here...
ctxt = context.WithValue(ctxt, "key", "some new value I want downstream")
if next != nil {
next(w, r.WithContext(ctxt))
}
} Downstream of the middleware, like in a handler or other middleware, you can use |
@catc specifically you can use this pattern to stuff the Param value from httprouter into request context and then extract parameters from the router downstream in handlers or other middleware. |
@yanfali I am using 1.7, so that worked perfectly. Thanks! |
@yanfali I think that when creating the new context you're supposed to extend from the one already on the request. Because the parent of your replacement context is func ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
- ctx := context.Background()
+ ctx := r.Context()
// ...middleware does stuff here...
ctx = context.WithValue(ctx, "key", "some new value I want downstream")
if next != nil {
next(w, r.WithContext(ctx))
}
} |
Agreed. Sorry if the example was misleading On Thu, Nov 17, 2016, 16:52 Alex Guerra notifications@github.com wrote:
|
I think @jszwedko you could probably close this issue as it's not really an issue as of go1.7 |
Are you thinking of updating negroni (I.e. v2) to utilize Google context.
That seems to be idiomatic now. Everyone seems to using it. Or should I attempt to create a fork?
http://godoc.org/golang.org/x/net/context
https://blog.golang.org/context
The text was updated successfully, but these errors were encountered: