github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/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  	"github.com/hxx258456/ccgo/grpc"
     8  	"github.com/hxx258456/ccgo/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  }