watch_client_adapter.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2016 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 grpcproxy
  15. import (
  16. "errors"
  17. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. "golang.org/x/net/context"
  19. "google.golang.org/grpc"
  20. "google.golang.org/grpc/metadata"
  21. )
  22. var errAlreadySentHeader = errors.New("grpcproxy: already send header")
  23. type ws2wc struct{ wserv pb.WatchServer }
  24. func WatchServerToWatchClient(wserv pb.WatchServer) pb.WatchClient {
  25. return &ws2wc{wserv}
  26. }
  27. func (s *ws2wc) Watch(ctx context.Context, opts ...grpc.CallOption) (pb.Watch_WatchClient, error) {
  28. // ch1 is buffered so server can send error on close
  29. ch1, ch2 := make(chan interface{}, 1), make(chan interface{})
  30. headerc, trailerc := make(chan metadata.MD, 1), make(chan metadata.MD, 1)
  31. cctx, ccancel := context.WithCancel(ctx)
  32. cli := &chanStream{recvc: ch1, sendc: ch2, ctx: cctx, cancel: ccancel}
  33. wclient := &ws2wcClientStream{chanClientStream{headerc, trailerc, cli}}
  34. sctx, scancel := context.WithCancel(ctx)
  35. srv := &chanStream{recvc: ch2, sendc: ch1, ctx: sctx, cancel: scancel}
  36. wserver := &ws2wcServerStream{chanServerStream{headerc, trailerc, srv, nil}}
  37. go func() {
  38. if err := s.wserv.Watch(wserver); err != nil {
  39. select {
  40. case srv.sendc <- err:
  41. case <-sctx.Done():
  42. case <-cctx.Done():
  43. }
  44. }
  45. scancel()
  46. ccancel()
  47. }()
  48. return wclient, nil
  49. }
  50. // ws2wcClientStream implements Watch_WatchClient
  51. type ws2wcClientStream struct{ chanClientStream }
  52. // ws2wcServerStream implements Watch_WatchServer
  53. type ws2wcServerStream struct{ chanServerStream }
  54. func (s *ws2wcClientStream) Send(wr *pb.WatchRequest) error {
  55. return s.SendMsg(wr)
  56. }
  57. func (s *ws2wcClientStream) Recv() (*pb.WatchResponse, error) {
  58. var v interface{}
  59. if err := s.RecvMsg(&v); err != nil {
  60. return nil, err
  61. }
  62. return v.(*pb.WatchResponse), nil
  63. }
  64. func (s *ws2wcServerStream) Send(wr *pb.WatchResponse) error {
  65. return s.SendMsg(wr)
  66. }
  67. func (s *ws2wcServerStream) Recv() (*pb.WatchRequest, error) {
  68. var v interface{}
  69. if err := s.RecvMsg(&v); err != nil {
  70. return nil, err
  71. }
  72. return v.(*pb.WatchRequest), nil
  73. }
  74. // chanServerStream implements grpc.ServerStream with a chanStream
  75. type chanServerStream struct {
  76. headerc chan<- metadata.MD
  77. trailerc chan<- metadata.MD
  78. grpc.Stream
  79. headers []metadata.MD
  80. }
  81. func (ss *chanServerStream) SendHeader(md metadata.MD) error {
  82. if ss.headerc == nil {
  83. return errAlreadySentHeader
  84. }
  85. outmd := make(map[string][]string)
  86. for _, h := range append(ss.headers, md) {
  87. for k, v := range h {
  88. outmd[k] = v
  89. }
  90. }
  91. select {
  92. case ss.headerc <- outmd:
  93. ss.headerc = nil
  94. ss.headers = nil
  95. return nil
  96. case <-ss.Context().Done():
  97. }
  98. return ss.Context().Err()
  99. }
  100. func (ss *chanServerStream) SetHeader(md metadata.MD) error {
  101. if ss.headerc == nil {
  102. return errAlreadySentHeader
  103. }
  104. ss.headers = append(ss.headers, md)
  105. return nil
  106. }
  107. func (ss *chanServerStream) SetTrailer(md metadata.MD) {
  108. ss.trailerc <- md
  109. }
  110. // chanClientStream implements grpc.ClientStream with a chanStream
  111. type chanClientStream struct {
  112. headerc <-chan metadata.MD
  113. trailerc <-chan metadata.MD
  114. *chanStream
  115. }
  116. func (cs *chanClientStream) Header() (metadata.MD, error) {
  117. select {
  118. case md := <-cs.headerc:
  119. return md, nil
  120. case <-cs.Context().Done():
  121. }
  122. return nil, cs.Context().Err()
  123. }
  124. func (cs *chanClientStream) Trailer() metadata.MD {
  125. select {
  126. case md := <-cs.trailerc:
  127. return md
  128. case <-cs.Context().Done():
  129. return nil
  130. }
  131. }
  132. func (s *chanClientStream) CloseSend() error {
  133. close(s.chanStream.sendc)
  134. return nil
  135. }
  136. // chanStream implements grpc.Stream using channels
  137. type chanStream struct {
  138. recvc <-chan interface{}
  139. sendc chan<- interface{}
  140. ctx context.Context
  141. cancel context.CancelFunc
  142. }
  143. func (s *chanStream) Context() context.Context { return s.ctx }
  144. func (s *chanStream) SendMsg(m interface{}) error {
  145. select {
  146. case s.sendc <- m:
  147. if err, ok := m.(error); ok {
  148. return err
  149. }
  150. return nil
  151. case <-s.ctx.Done():
  152. }
  153. return s.ctx.Err()
  154. }
  155. func (s *chanStream) RecvMsg(m interface{}) error {
  156. v := m.(*interface{})
  157. select {
  158. case msg, ok := <-s.recvc:
  159. if !ok {
  160. return grpc.ErrClientConnClosing
  161. }
  162. if err, ok := msg.(error); ok {
  163. return err
  164. }
  165. *v = msg
  166. return nil
  167. case <-s.ctx.Done():
  168. }
  169. return s.ctx.Err()
  170. }