watcher.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. "github.com/coreos/etcd/clientv3"
  17. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. "github.com/coreos/etcd/mvcc/mvccpb"
  19. )
  20. type watchRange struct {
  21. key, end string
  22. }
  23. type watcher struct {
  24. id int64
  25. wr watchRange
  26. // TODO: support filter
  27. progress bool
  28. ch chan<- *pb.WatchResponse
  29. }
  30. func (w *watcher) send(wr clientv3.WatchResponse) {
  31. if wr.IsProgressNotify() && !w.progress {
  32. return
  33. }
  34. // todo: filter out the events that this watcher already seen.
  35. evs := wr.Events
  36. events := make([]*mvccpb.Event, len(evs))
  37. for i := range evs {
  38. events[i] = (*mvccpb.Event)(evs[i])
  39. }
  40. pbwr := &pb.WatchResponse{
  41. Header: &wr.Header,
  42. WatchId: w.id,
  43. Events: events,
  44. }
  45. select {
  46. case w.ch <- pbwr:
  47. default:
  48. panic("handle this")
  49. }
  50. }