watch_client_adapter.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 adapter
  15. import (
  16. "context"
  17. "errors"
  18. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  19. "google.golang.org/grpc"
  20. )
  21. var errAlreadySentHeader = errors.New("adapter: already sent header")
  22. type ws2wc struct{ wserv pb.WatchServer }
  23. func WatchServerToWatchClient(wserv pb.WatchServer) pb.WatchClient {
  24. return &ws2wc{wserv}
  25. }
  26. func (s *ws2wc) Watch(ctx context.Context, opts ...grpc.CallOption) (pb.Watch_WatchClient, error) {
  27. cs := newPipeStream(ctx, func(ss chanServerStream) error {
  28. return s.wserv.Watch(&ws2wcServerStream{ss})
  29. })
  30. return &ws2wcClientStream{cs}, nil
  31. }
  32. // ws2wcClientStream implements Watch_WatchClient
  33. type ws2wcClientStream struct{ chanClientStream }
  34. // ws2wcServerStream implements Watch_WatchServer
  35. type ws2wcServerStream struct{ chanServerStream }
  36. func (s *ws2wcClientStream) Send(wr *pb.WatchRequest) error {
  37. return s.SendMsg(wr)
  38. }
  39. func (s *ws2wcClientStream) Recv() (*pb.WatchResponse, error) {
  40. var v interface{}
  41. if err := s.RecvMsg(&v); err != nil {
  42. return nil, err
  43. }
  44. return v.(*pb.WatchResponse), nil
  45. }
  46. func (s *ws2wcServerStream) Send(wr *pb.WatchResponse) error {
  47. return s.SendMsg(wr)
  48. }
  49. func (s *ws2wcServerStream) Recv() (*pb.WatchRequest, error) {
  50. var v interface{}
  51. if err := s.RecvMsg(&v); err != nil {
  52. return nil, err
  53. }
  54. return v.(*pb.WatchRequest), nil
  55. }