gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-grpc-middleware/wrappers.go (about) 1 // Copyright 2016 Michal Witkowski. All Rights Reserved. 2 // See LICENSE for licensing terms. 3 4 package grpc_middleware 5 6 import ( 7 "gitee.com/ks-custle/core-gm/grpc" 8 "gitee.com/ks-custle/core-gm/net/context" 9 ) 10 11 // WrappedServerStream is a thin wrapper around grpc.ServerStream that allows modifying context. 12 type WrappedServerStream struct { 13 grpc.ServerStream 14 // WrappedContext is the wrapper's own Context. You can assign it. 15 WrappedContext context.Context 16 } 17 18 // Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context() 19 func (w *WrappedServerStream) Context() context.Context { 20 return w.WrappedContext 21 } 22 23 // WrapServerStream returns a ServerStream that has the ability to overwrite context. 24 func WrapServerStream(stream grpc.ServerStream) *WrappedServerStream { 25 if existing, ok := stream.(*WrappedServerStream); ok { 26 return existing 27 } 28 return &WrappedServerStream{ServerStream: stream, WrappedContext: stream.Context()} 29 }