watcher.go 3.0 KB

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