watch_client_adapter.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 s.wserv.Watch(wserver)
  31. return wclient, nil
  32. }
  33. // ws2wcClientStream implements Watch_WatchClient
  34. type ws2wcClientStream struct{ chanClientStream }
  35. // ws2wcServerStream implements Watch_WatchServer
  36. type ws2wcServerStream struct{ chanServerStream }
  37. func (s *ws2wcClientStream) Send(wr *pb.WatchRequest) error {
  38. return s.SendMsg(wr)
  39. }
  40. func (s *ws2wcClientStream) Recv() (*pb.WatchResponse, error) {
  41. var v interface{}
  42. if err := s.RecvMsg(&v); err != nil {
  43. return nil, err
  44. }
  45. return v.(*pb.WatchResponse), nil
  46. }
  47. func (s *ws2wcServerStream) Send(wr *pb.WatchResponse) error {
  48. return s.SendMsg(wr)
  49. }
  50. func (s *ws2wcServerStream) Recv() (*pb.WatchRequest, error) {
  51. var v interface{}
  52. if err := s.RecvMsg(&v); err != nil {
  53. return nil, err
  54. }
  55. return v.(*pb.WatchRequest), nil
  56. }
  57. // chanServerStream implements grpc.ServerStream with a chanStream
  58. type chanServerStream struct {
  59. headerc chan<- metadata.MD
  60. trailerc chan<- metadata.MD
  61. grpc.Stream
  62. }
  63. func (ss *chanServerStream) SendHeader(md metadata.MD) error {
  64. select {
  65. case ss.headerc <- md:
  66. return nil
  67. case <-ss.Context().Done():
  68. }
  69. return ss.Context().Err()
  70. }
  71. func (ss *chanServerStream) SetTrailer(md metadata.MD) {
  72. ss.trailerc <- md
  73. }
  74. // chanClientStream implements grpc.ClientStream with a chanStream
  75. type chanClientStream struct {
  76. headerc <-chan metadata.MD
  77. trailerc <-chan metadata.MD
  78. grpc.Stream
  79. }
  80. func (cs *chanClientStream) Header() (metadata.MD, error) {
  81. select {
  82. case md := <-cs.headerc:
  83. return md, nil
  84. case <-cs.Context().Done():
  85. }
  86. return nil, cs.Context().Err()
  87. }
  88. func (cs *chanClientStream) Trailer() metadata.MD {
  89. select {
  90. case md := <-cs.trailerc:
  91. return md
  92. case <-cs.Context().Done():
  93. return nil
  94. }
  95. }
  96. func (s *chanClientStream) CloseSend() error { return nil }
  97. // chanStream implements grpc.Stream using channels
  98. type chanStream struct {
  99. recvc <-chan interface{}
  100. sendc chan<- interface{}
  101. ctx context.Context
  102. }
  103. func (s *chanStream) Context() context.Context { return s.Context() }
  104. func (s *chanStream) SendMsg(m interface{}) error {
  105. select {
  106. case s.sendc <- m:
  107. return nil
  108. case <-s.ctx.Done():
  109. }
  110. return s.ctx.Err()
  111. }
  112. func (s *chanStream) RecvMsg(m interface{}) error {
  113. v := m.(*interface{})
  114. select {
  115. case m = <-s.recvc:
  116. *v = m
  117. return nil
  118. case <-s.ctx.Done():
  119. }
  120. return s.ctx.Err()
  121. }