watch_client_adapter.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  17. "golang.org/x/net/context"
  18. "google.golang.org/grpc"
  19. "google.golang.org/grpc/metadata"
  20. )
  21. type ws2wc struct{ wserv pb.WatchServer }
  22. func WatchServerToWatchClient(wserv pb.WatchServer) pb.WatchClient {
  23. return &ws2wc{wserv}
  24. }
  25. func (s *ws2wc) Watch(ctx context.Context, opts ...grpc.CallOption) (pb.Watch_WatchClient, error) {
  26. ch1, ch2 := make(chan interface{}), make(chan interface{})
  27. headerc, trailerc := make(chan metadata.MD, 1), make(chan metadata.MD, 1)
  28. wclient := &ws2wcClientStream{chanClientStream{headerc, trailerc, &chanStream{ch1, ch2, ctx}}}
  29. wserver := &ws2wcServerStream{chanServerStream{headerc, trailerc, &chanStream{ch2, ch1, ctx}}}
  30. go func() {
  31. s.wserv.Watch(wserver)
  32. // close the server side sender
  33. close(ch1)
  34. }()
  35. return wclient, nil
  36. }
  37. // ws2wcClientStream implements Watch_WatchClient
  38. type ws2wcClientStream struct{ chanClientStream }
  39. // ws2wcServerStream implements Watch_WatchServer
  40. type ws2wcServerStream struct{ chanServerStream }
  41. func (s *ws2wcClientStream) Send(wr *pb.WatchRequest) error {
  42. return s.SendMsg(wr)
  43. }
  44. func (s *ws2wcClientStream) Recv() (*pb.WatchResponse, error) {
  45. var v interface{}
  46. if err := s.RecvMsg(&v); err != nil {
  47. return nil, err
  48. }
  49. return v.(*pb.WatchResponse), nil
  50. }
  51. func (s *ws2wcServerStream) Send(wr *pb.WatchResponse) error {
  52. return s.SendMsg(wr)
  53. }
  54. func (s *ws2wcServerStream) Recv() (*pb.WatchRequest, error) {
  55. var v interface{}
  56. if err := s.RecvMsg(&v); err != nil {
  57. return nil, err
  58. }
  59. return v.(*pb.WatchRequest), nil
  60. }
  61. // chanServerStream implements grpc.ServerStream with a chanStream
  62. type chanServerStream struct {
  63. headerc chan<- metadata.MD
  64. trailerc chan<- metadata.MD
  65. grpc.Stream
  66. }
  67. func (ss *chanServerStream) SendHeader(md metadata.MD) error {
  68. select {
  69. case ss.headerc <- md:
  70. return nil
  71. case <-ss.Context().Done():
  72. }
  73. return ss.Context().Err()
  74. }
  75. func (ss *chanServerStream) SetTrailer(md metadata.MD) {
  76. ss.trailerc <- md
  77. }
  78. // chanClientStream implements grpc.ClientStream with a chanStream
  79. type chanClientStream struct {
  80. headerc <-chan metadata.MD
  81. trailerc <-chan metadata.MD
  82. *chanStream
  83. }
  84. func (cs *chanClientStream) Header() (metadata.MD, error) {
  85. select {
  86. case md := <-cs.headerc:
  87. return md, nil
  88. case <-cs.Context().Done():
  89. }
  90. return nil, cs.Context().Err()
  91. }
  92. func (cs *chanClientStream) Trailer() metadata.MD {
  93. select {
  94. case md := <-cs.trailerc:
  95. return md
  96. case <-cs.Context().Done():
  97. return nil
  98. }
  99. }
  100. func (s *chanClientStream) CloseSend() error {
  101. close(s.chanStream.sendc)
  102. return nil
  103. }
  104. // chanStream implements grpc.Stream using channels
  105. type chanStream struct {
  106. recvc <-chan interface{}
  107. sendc chan<- interface{}
  108. ctx context.Context
  109. }
  110. func (s *chanStream) Context() context.Context { return s.ctx }
  111. func (s *chanStream) SendMsg(m interface{}) error {
  112. select {
  113. case s.sendc <- m:
  114. return nil
  115. case <-s.ctx.Done():
  116. }
  117. return s.ctx.Err()
  118. }
  119. func (s *chanStream) RecvMsg(m interface{}) error {
  120. v := m.(*interface{})
  121. select {
  122. case msg, ok := <-s.recvc:
  123. if !ok {
  124. return grpc.ErrClientConnClosing
  125. }
  126. *v = msg
  127. return nil
  128. case <-s.ctx.Done():
  129. }
  130. return s.ctx.Err()
  131. }