watcher.go 2.9 KB

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