watcher_single.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "golang.org/x/net/context"
  18. )
  19. type watcherSingle struct {
  20. // ch delievers events received from the etcd server
  21. ch clientv3.WatchChan
  22. // cancel is used to cancel the underlying etcd server watcher
  23. // It should also close the ch.
  24. cancel context.CancelFunc
  25. // sws is the stream this watcherSingle attached to
  26. sws *serverWatchStream
  27. w watcher
  28. lastStoreRev int64 // last seen revision of the remote mvcc store
  29. donec chan struct{}
  30. }
  31. func newWatcherSingle(wch clientv3.WatchChan, c context.CancelFunc, w watcher, sws *serverWatchStream) *watcherSingle {
  32. return &watcherSingle{
  33. sws: sws,
  34. ch: wch,
  35. cancel: c,
  36. w: w,
  37. donec: make(chan struct{}),
  38. }
  39. }
  40. func (ws watcherSingle) run() {
  41. defer close(ws.donec)
  42. for wr := range ws.ch {
  43. ws.lastStoreRev = wr.Header.Revision
  44. ws.w.send(wr)
  45. if ws.sws.maybeCoalesceWatcher(ws) {
  46. return
  47. }
  48. }
  49. }
  50. // canPromote returns true if a watcherSingle can promote itself to a watchergroup
  51. // when it already caught up with the last seen revision from the response header
  52. // of an etcd server.
  53. func (ws watcherSingle) canPromote() bool {
  54. return ws.w.rev == ws.lastStoreRev
  55. }
  56. func (ws watcherSingle) stop() {
  57. ws.cancel()
  58. }
  59. func (ws watcherSingle) stopNotify() <-chan struct{} {
  60. return ws.donec
  61. }