contextserverstream.go 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package server
  2. import (
  3. "context"
  4. "google.golang.org/grpc"
  5. "google.golang.org/grpc/metadata"
  6. )
  7. // A gRPC server stream will only let you get its context but
  8. // there is no way to set a new (augmented context) to the next
  9. // handler (like we do for a unary request). We need to wrap the grpc.ServerSteam
  10. // to be able to set a new context that will be sent to the next stream interceptor.
  11. type contextServerStream struct {
  12. ss grpc.ServerStream
  13. ctx context.Context
  14. }
  15. func (css *contextServerStream) SetHeader(md metadata.MD) error {
  16. return css.ss.SetHeader(md)
  17. }
  18. func (css *contextServerStream) SendHeader(md metadata.MD) error {
  19. return css.ss.SendHeader(md)
  20. }
  21. func (css *contextServerStream) SetTrailer(md metadata.MD) {
  22. css.ss.SetTrailer(md)
  23. }
  24. func (css *contextServerStream) Context() context.Context {
  25. return css.ctx
  26. }
  27. func (css *contextServerStream) SendMsg(m interface{}) error {
  28. return css.ss.SendMsg(m)
  29. }
  30. func (css *contextServerStream) RecvMsg(m interface{}) error {
  31. return css.ss.RecvMsg(m)
  32. }