diff --git a/transport/transport.go b/transport/transport.go index 517168035be4..de987b94b9a6 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -341,16 +341,22 @@ func (s *Stream) Read(p []byte) (n int, err error) { // The key to save transport.Stream in the context. type streamKey struct{} +// streamCtxGoStringer implements the GoStringer interface for Stream +// so that ctx.String() is thread-safe by hiding the stream information. +type streamCtxGoStringer Stream + +func (ss *streamCtxGoStringer) GoString() string { return "" } + // newContextWithStream creates a new context from ctx and attaches stream // to it. func newContextWithStream(ctx context.Context, stream *Stream) context.Context { - return context.WithValue(ctx, streamKey{}, stream) + return context.WithValue(ctx, streamKey{}, (*streamCtxGoStringer)(stream)) } // StreamFromContext returns the stream saved in ctx. -func StreamFromContext(ctx context.Context) (s *Stream, ok bool) { - s, ok = ctx.Value(streamKey{}).(*Stream) - return +func StreamFromContext(ctx context.Context) (*Stream, bool) { + s, ok := ctx.Value(streamKey{}).(*streamCtxGoStringer) + return (*Stream)(s), ok } // state of transport