Debian Codegraph @

config/configgrpc/wrappedstream.go

raw
1 // Copyright The OpenTelemetry Authors
2 // Copyright 2016 Michal Witkowski. All Rights Reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 package configgrpc // import "go.opentelemetry.io/collector/config/configgrpc"
6
7 import (
8 "context"
9
10 "google.golang.org/grpc"
11 )
12
13 // this functionality was originally copied from grpc-ecosystem/go-grpc-middleware project
14
15 // wrappedServerStream is a thin wrapper around grpc.ServerStream that allows modifying context.
16 type wrappedServerStream struct {
17 grpc.ServerStream
18 // wrappedContext is the wrapper's own Context. You can assign it.
19 wrappedCtx context.Context
20 }
21
22 // Context returns the wrapper's wrappedContext, overwriting the nested grpc.ServerStream.Context()
23 func (w *wrappedServerStream) Context() context.Context {
24 return w.wrappedCtx
25 }
26
27 // wrapServerStream returns a ServerStream with the new context.
28 func wrapServerStream(wrappedCtx context.Context, stream grpc.ServerStream) *wrappedServerStream {
29 if existing, ok := stream.(*wrappedServerStream); ok {
30 existing.wrappedCtx = wrappedCtx
31 return existing
32 }
33 return &wrappedServerStream{ServerStream: stream, wrappedCtx: wrappedCtx}
34 }