grpc-gateway:传递给 http 处理程序的上下文丢失了上下文中设置的值

问题描述 投票:0回答:1

我对 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")
}

go gorilla
1个回答
0
投票

这是我们使用引导程序设置中间件层的方式的设置问题。基本上,在请求传递到 HTTP 之前,上下文已被完全替换,而不是添加和传入。一旦我们修复了中间件,上下文就保持了我们预期的值。

© www.soinside.com 2019 - 2024. All rights reserved.