wrappers.go 945 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2016 Michal Witkowski. All Rights Reserved.
  2. // See LICENSE for licensing terms.
  3. package grpc_middleware
  4. import (
  5. "golang.org/x/net/context"
  6. "google.golang.org/grpc"
  7. )
  8. // WrappedServerStream is a thin wrapper around grpc.ServerStream that allows modifying context.
  9. type WrappedServerStream struct {
  10. grpc.ServerStream
  11. // WrappedContext is the wrapper's own Context. You can assign it.
  12. WrappedContext context.Context
  13. }
  14. // Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context()
  15. func (w *WrappedServerStream) Context() context.Context {
  16. return w.WrappedContext
  17. }
  18. // WrapServerStream returns a ServerStream that has the ability to overwrite context.
  19. func WrapServerStream(stream grpc.ServerStream) *WrappedServerStream {
  20. if existing, ok := stream.(*WrappedServerStream); ok {
  21. return existing
  22. }
  23. return &WrappedServerStream{ServerStream: stream, WrappedContext: stream.Context()}
  24. }