watch_client_adapter.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. }