我对 GRPC 处理程序和它调用之间的上下文发生的情况感到茫然
gwmux.ServeHTTP(w, r.WithContext(ctx)
我已经删除了很多代码来尝试只包含最基本的内容。一旦我收到 gwmux.ServeHTTP 对
myHttp
的调用,上下文中的任何值都不会包含在上下文中。我是否缺少某些内容来确保包含上下文值?
gwmux := runtime.NewServeMux()
r.PathPrefix("/route").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// This works and has the value I expect
test := ctx.Value("mykey")
gwmux.ServeHTTP(w, r.WithContext(ctx))
}
func myHttp(ctx context.Context) {
// This does not work, it is nil
test := ctx.Value("mykey")
}
这是我们使用引导程序设置中间件层的方式的设置问题。基本上,在请求传递到 HTTP 之前,上下文已被完全替换,而不是添加和传入。一旦我们修复了中间件,上下文就保持了我们预期的值。