watcher.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "time"
  17. "github.com/coreos/etcd/clientv3"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "github.com/coreos/etcd/mvcc"
  20. "github.com/coreos/etcd/mvcc/mvccpb"
  21. )
  22. type watchRange struct {
  23. key, end string
  24. }
  25. type watcher struct {
  26. // user configuration
  27. wr watchRange
  28. filters []mvcc.FilterFunc
  29. progress bool
  30. // id is the id returned to the client on its watch stream.
  31. id int64
  32. // nextrev is the minimum expected next event revision.
  33. nextrev int64
  34. // lastHeader has the last header sent over the stream.
  35. lastHeader pb.ResponseHeader
  36. // wps is the parent.
  37. wps *watchProxyStream
  38. }
  39. // send filters out repeated events by discarding revisions older
  40. // than the last one sent over the watch channel.
  41. func (w *watcher) send(wr clientv3.WatchResponse) {
  42. if wr.IsProgressNotify() && !w.progress {
  43. return
  44. }
  45. if w.nextrev > wr.Header.Revision && len(wr.Events) > 0 {
  46. return
  47. }
  48. if w.nextrev == 0 {
  49. // current watch; expect updates following this revision
  50. w.nextrev = wr.Header.Revision + 1
  51. }
  52. events := make([]*mvccpb.Event, 0, len(wr.Events))
  53. var lastRev int64
  54. for i := range wr.Events {
  55. ev := (*mvccpb.Event)(wr.Events[i])
  56. if ev.Kv.ModRevision < w.nextrev {
  57. continue
  58. } else {
  59. // We cannot update w.rev here.
  60. // txn can have multiple events with the same rev.
  61. // If w.nextrev updates here, it would skip events in the same txn.
  62. lastRev = ev.Kv.ModRevision
  63. }
  64. filtered := false
  65. if len(w.filters) != 0 {
  66. for _, filter := range w.filters {
  67. if filter(*ev) {
  68. filtered = true
  69. break
  70. }
  71. }
  72. }
  73. if !filtered {
  74. events = append(events, ev)
  75. }
  76. }
  77. if lastRev >= w.nextrev {
  78. w.nextrev = lastRev + 1
  79. }
  80. // all events are filtered out?
  81. if !wr.IsProgressNotify() && !wr.Created && len(events) == 0 {
  82. return
  83. }
  84. w.lastHeader = wr.Header
  85. w.post(&pb.WatchResponse{
  86. Header: &wr.Header,
  87. Created: wr.Created,
  88. WatchId: w.id,
  89. Events: events,
  90. })
  91. }
  92. // post puts a watch response on the watcher's proxy stream channel
  93. func (w *watcher) post(wr *pb.WatchResponse) bool {
  94. select {
  95. case w.wps.watchCh <- wr:
  96. case <-time.After(50 * time.Millisecond):
  97. w.wps.cancel()
  98. return false
  99. }
  100. return true
  101. }