contextserverstream.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package server
  14. import (
  15. "context"
  16. "google.golang.org/grpc"
  17. "google.golang.org/grpc/metadata"
  18. )
  19. // A gRPC server stream will only let you get its context but
  20. // there is no way to set a new (augmented context) to the next
  21. // handler (like we do for a unary request). We need to wrap the grpc.ServerSteam
  22. // to be able to set a new context that will be sent to the next stream interceptor.
  23. type contextServerStream struct {
  24. ss grpc.ServerStream
  25. ctx context.Context
  26. }
  27. func (css *contextServerStream) SetHeader(md metadata.MD) error {
  28. return css.ss.SetHeader(md)
  29. }
  30. func (css *contextServerStream) SendHeader(md metadata.MD) error {
  31. return css.ss.SendHeader(md)
  32. }
  33. func (css *contextServerStream) SetTrailer(md metadata.MD) {
  34. css.ss.SetTrailer(md)
  35. }
  36. func (css *contextServerStream) Context() context.Context {
  37. return css.ctx
  38. }
  39. func (css *contextServerStream) SendMsg(m interface{}) error {
  40. return css.ss.SendMsg(m)
  41. }
  42. func (css *contextServerStream) RecvMsg(m interface{}) error {
  43. return css.ss.RecvMsg(m)
  44. }