watcher.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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"
  19. "github.com/coreos/etcd/mvcc/mvccpb"
  20. )
  21. type watchRange struct {
  22. key, end string
  23. }
  24. type watcher struct {
  25. id int64
  26. wr watchRange
  27. filters []mvcc.FilterFunc
  28. progress bool
  29. ch chan<- *pb.WatchResponse
  30. }
  31. func (w *watcher) send(wr clientv3.WatchResponse) {
  32. if wr.IsProgressNotify() && !w.progress {
  33. return
  34. }
  35. // todo: filter out the events that this watcher already seen.
  36. events := make([]*mvccpb.Event, 0, len(wr.Events))
  37. for i := range wr.Events {
  38. filtered := false
  39. ev := (*mvccpb.Event)(wr.Events[i])
  40. if len(w.filters) != 0 {
  41. for _, filter := range w.filters {
  42. if filter(*ev) {
  43. filtered = true
  44. break
  45. }
  46. }
  47. }
  48. if !filtered {
  49. events = append(events, ev)
  50. }
  51. }
  52. // all events are filtered out?
  53. if !wr.IsProgressNotify() && len(events) == 0 {
  54. return
  55. }
  56. pbwr := &pb.WatchResponse{
  57. Header: &wr.Header,
  58. WatchId: w.id,
  59. Events: events,
  60. }
  61. select {
  62. case w.ch <- pbwr:
  63. default:
  64. panic("handle this")
  65. }
  66. }