2 回答

TA貢獻1895條經驗 獲得超7個贊
您需要使用NewIncomingContext在流中創建當前上下文的副本。
然后,您必須創建一個wrappedStream
覆蓋Context
方法 in的類型ServerStream
以返回修改后的context
. 您需要將其傳遞wrappedStream
給handler
您在攔截器中收到的。
你可以在這里看到一個例子(它在這里覆蓋了其他方法,但想法是一樣的): https ://github.com/grpc/grpc-go/blob/master/examples/features/interceptor/server/main .go#L106-L124
希望這可以幫助。

TA貢獻1876條經驗 獲得超5個贊
您可以創建自己的 ServerStream 實現,用您自己的上下文覆蓋 Context() 方法,或者在 grpc 包中有一個結構WrappedServerStream (github.com/grpc-ecosystem/go-grpc-middleware),您可以在其中傳遞上下文和原始服務器流對象并在其中使用它handler。
例子:
// This methods get the current context, and creates a new one
newContext, err := interceptor.authorize(ss.Context(), info.FullMethod)
if err != nil {
log.Printf("authorization failed: %v", err)
return err
}
err = handler(srv, &grpc_middleware.WrappedServerStream{
ServerStream: ss,
WrappedContext: newContext,
})
- 2 回答
- 0 關注
- 157 瀏覽
添加回答
舉報