问题陈述
我想将HTTP请求的生存期与在Web应用程序范围之外创建的上下文联系起来。因此,我编写了以下中间件(使用
github.com/go-chi/chi
):
func BindContext(c context.Context) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r.WithContext(c))
})
}
}
中间件用于以下最小测试用例:
package main
import (
"context"
"net/http"
"github.com/SentimensRG/ctx"
"github.com/SentimensRG/ctx/sigctx"
"github.com/go-chi/chi"
)
func greet(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func BindContext(c context.Context) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r.WithContext(c))
})
}
}
func main() {
r := chi.NewMux()
r.Use(BindContext(ctx.AsContext(sigctx.New())))
r.Get("/", greet)
http.ListenAndServe(":8080", r)
}
处理程序因以下错误而崩溃:
2018/07/25 14:58:57 http: panic serving [::1]:56527: interface conversion: interface {} is nil, not *chi.Context
goroutine 35 [running]:
net/http.(*conn).serve.func1(0xc42014a0a0)
/usr/local/go/src/net/http/server.go:1726 +0xd0
panic(0x12749c0, 0xc42014c200)
/usr/local/go/src/runtime/panic.go:502 +0x229
github.com/go-chi/chi.(*Mux).routeHTTP(0xc4201180c0, 0x12fcf00, 0xc420166000, 0xc420160200)
/Users/lthibault/go/src/github.com/go-chi/chi/mux.go:400 +0x2f3
github.com/go-chi/chi.(*Mux).(github.com/go-chi/chi.routeHTTP)-fm(0x12fcf00, 0xc420166000, 0xc420160200)
/Users/lthibault/go/src/github.com/go-chi/chi/mux.go:368 +0x48
net/http.HandlerFunc.ServeHTTP(0xc420142010, 0x12fcf00, 0xc420166000, 0xc420160200)
/usr/local/go/src/net/http/server.go:1947 +0x44
main.fail.func1.1(0x12fcf00, 0xc420166000, 0xc420160100)
/Users/lthibault/go/src/github.com/lthibault/mesh/cmd/scratch/main.go:22 +0x77
net/http.HandlerFunc.ServeHTTP(0xc420148000, 0x12fcf00, 0xc420166000, 0xc420160100)
/usr/local/go/src/net/http/server.go:1947 +0x44
github.com/go-chi/chi.(*Mux).ServeHTTP(0xc4201180c0, 0x12fcf00, 0xc420166000, 0xc420160000)
/Users/lthibault/go/src/github.com/go-chi/chi/mux.go:81 +0x221
net/http.serverHandler.ServeHTTP(0xc420150000, 0x12fcf00, 0xc420166000, 0xc420160000)
/usr/local/go/src/net/http/server.go:2694 +0xbc
net/http.(*conn).serve(0xc42014a0a0, 0x12fd1c0, 0xc42014c080)
/usr/local/go/src/net/http/server.go:1830 +0x651
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2795 +0x27b
不雅解
问题似乎来自
Mux.routeHTTP
,尝试恢复
*chi.Context
从
r.Context()
. 似乎
r.WithContext
不将存储在请求上下文中的值传输到新上下文。
最明显(尽管很难看)的解决办法是:
func BindContext(c context.Context) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rctx := r.Context().Value(chi.RouteCtxKey).(*chi.Context)
c = context.WithValue(c, chi.RouteCtxKey, rctx)
h.ServeHTTP(w, r.WithContext(c))
})
}
}
这行得通,但让我感到不安。我真的需要手动将每个相关值从
R.()
传递到上下文中
r.WithContext()
?
这里有几个失败案例:
-
当有许多不同的值要传输时会发生什么?
-
如果不导出上下文键(如Go中建议的那样),会发生什么?
-
如果原始上下文在我传入的上下文之前终止,会发生什么?
(几句话:没什么好东西!)
我的问题是
是否有标准的“合并”上下文传递给
小精灵
中的现有上下文
r.Context
?