Girish Kumar | 93e9174 | 2020-07-27 16:43:19 +0000 | [diff] [blame] | 1 | // Copyright 2016 Michal Witkowski. All Rights Reserved. |
| 2 | // See LICENSE for licensing terms. |
| 3 | |
| 4 | package grpc_middleware |
| 5 | |
| 6 | import ( |
khenaidoo | 106c61a | 2021-08-11 18:05:46 -0400 | [diff] [blame] | 7 | "context" |
| 8 | |
Girish Kumar | 93e9174 | 2020-07-27 16:43:19 +0000 | [diff] [blame] | 9 | "google.golang.org/grpc" |
| 10 | ) |
| 11 | |
| 12 | // WrappedServerStream is a thin wrapper around grpc.ServerStream that allows modifying context. |
| 13 | type WrappedServerStream struct { |
| 14 | grpc.ServerStream |
| 15 | // WrappedContext is the wrapper's own Context. You can assign it. |
| 16 | WrappedContext context.Context |
| 17 | } |
| 18 | |
| 19 | // Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context() |
| 20 | func (w *WrappedServerStream) Context() context.Context { |
| 21 | return w.WrappedContext |
| 22 | } |
| 23 | |
| 24 | // WrapServerStream returns a ServerStream that has the ability to overwrite context. |
| 25 | func WrapServerStream(stream grpc.ServerStream) *WrappedServerStream { |
| 26 | if existing, ok := stream.(*WrappedServerStream); ok { |
| 27 | return existing |
| 28 | } |
| 29 | return &WrappedServerStream{ServerStream: stream, WrappedContext: stream.Context()} |
| 30 | } |