chan_stream.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2017 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package adapter
  15. import (
  16. "golang.org/x/net/context"
  17. "google.golang.org/grpc"
  18. "google.golang.org/grpc/metadata"
  19. )
  20. // chanServerStream implements grpc.ServerStream with a chanStream
  21. type chanServerStream struct {
  22. headerc chan<- metadata.MD
  23. trailerc chan<- metadata.MD
  24. grpc.Stream
  25. headers []metadata.MD
  26. }
  27. func (ss *chanServerStream) SendHeader(md metadata.MD) error {
  28. if ss.headerc == nil {
  29. return errAlreadySentHeader
  30. }
  31. outmd := make(map[string][]string)
  32. for _, h := range append(ss.headers, md) {
  33. for k, v := range h {
  34. outmd[k] = v
  35. }
  36. }
  37. select {
  38. case ss.headerc <- outmd:
  39. ss.headerc = nil
  40. ss.headers = nil
  41. return nil
  42. case <-ss.Context().Done():
  43. }
  44. return ss.Context().Err()
  45. }
  46. func (ss *chanServerStream) SetHeader(md metadata.MD) error {
  47. if ss.headerc == nil {
  48. return errAlreadySentHeader
  49. }
  50. ss.headers = append(ss.headers, md)
  51. return nil
  52. }
  53. func (ss *chanServerStream) SetTrailer(md metadata.MD) {
  54. ss.trailerc <- md
  55. }
  56. // chanClientStream implements grpc.ClientStream with a chanStream
  57. type chanClientStream struct {
  58. headerc <-chan metadata.MD
  59. trailerc <-chan metadata.MD
  60. *chanStream
  61. }
  62. func (cs *chanClientStream) Header() (metadata.MD, error) {
  63. select {
  64. case md := <-cs.headerc:
  65. return md, nil
  66. case <-cs.Context().Done():
  67. }
  68. return nil, cs.Context().Err()
  69. }
  70. func (cs *chanClientStream) Trailer() metadata.MD {
  71. select {
  72. case md := <-cs.trailerc:
  73. return md
  74. case <-cs.Context().Done():
  75. return nil
  76. }
  77. }
  78. func (cs *chanClientStream) CloseSend() error {
  79. close(cs.chanStream.sendc)
  80. return nil
  81. }
  82. // chanStream implements grpc.Stream using channels
  83. type chanStream struct {
  84. recvc <-chan interface{}
  85. sendc chan<- interface{}
  86. ctx context.Context
  87. cancel context.CancelFunc
  88. }
  89. func (s *chanStream) Context() context.Context { return s.ctx }
  90. func (s *chanStream) SendMsg(m interface{}) error {
  91. select {
  92. case s.sendc <- m:
  93. if err, ok := m.(error); ok {
  94. return err
  95. }
  96. return nil
  97. case <-s.ctx.Done():
  98. }
  99. return s.ctx.Err()
  100. }
  101. func (s *chanStream) RecvMsg(m interface{}) error {
  102. v := m.(*interface{})
  103. for {
  104. select {
  105. case msg, ok := <-s.recvc:
  106. if !ok {
  107. return grpc.ErrClientConnClosing
  108. }
  109. if err, ok := msg.(error); ok {
  110. return err
  111. }
  112. *v = msg
  113. return nil
  114. case <-s.ctx.Done():
  115. }
  116. if len(s.recvc) == 0 {
  117. // prioritize any pending recv messages over canceled context
  118. break
  119. }
  120. }
  121. return s.ctx.Err()
  122. }
  123. func newPipeStream(ctx context.Context, ssHandler func(chanServerStream) error) chanClientStream {
  124. // ch1 is buffered so server can send error on close
  125. ch1, ch2 := make(chan interface{}, 1), make(chan interface{})
  126. headerc, trailerc := make(chan metadata.MD, 1), make(chan metadata.MD, 1)
  127. cctx, ccancel := context.WithCancel(ctx)
  128. cli := &chanStream{recvc: ch1, sendc: ch2, ctx: cctx, cancel: ccancel}
  129. cs := chanClientStream{headerc, trailerc, cli}
  130. sctx, scancel := context.WithCancel(ctx)
  131. srv := &chanStream{recvc: ch2, sendc: ch1, ctx: sctx, cancel: scancel}
  132. ss := chanServerStream{headerc, trailerc, srv, nil}
  133. go func() {
  134. if err := ssHandler(ss); err != nil {
  135. select {
  136. case srv.sendc <- err:
  137. case <-sctx.Done():
  138. case <-cctx.Done():
  139. }
  140. }
  141. scancel()
  142. ccancel()
  143. }()
  144. return cs
  145. }