watcher.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 CoreOS, Inc.
  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 store
  15. type Watcher interface {
  16. EventChan() chan *Event
  17. StartIndex() uint64 // The EtcdIndex at which the Watcher was created
  18. Remove()
  19. }
  20. type watcher struct {
  21. eventChan chan *Event
  22. stream bool
  23. recursive bool
  24. sinceIndex uint64
  25. startIndex uint64
  26. hub *watcherHub
  27. removed bool
  28. remove func()
  29. }
  30. func (w *watcher) EventChan() chan *Event {
  31. return w.eventChan
  32. }
  33. func (w *watcher) StartIndex() uint64 {
  34. return w.startIndex
  35. }
  36. // notify function notifies the watcher. If the watcher interests in the given path,
  37. // the function will return true.
  38. func (w *watcher) notify(e *Event, originalPath bool, deleted bool) bool {
  39. // watcher is interested the path in three cases and under one condition
  40. // the condition is that the event happens after the watcher's sinceIndex
  41. // 1. the path at which the event happens is the path the watcher is watching at.
  42. // For example if the watcher is watching at "/foo" and the event happens at "/foo",
  43. // the watcher must be interested in that event.
  44. // 2. the watcher is a recursive watcher, it interests in the event happens after
  45. // its watching path. For example if watcher A watches at "/foo" and it is a recursive
  46. // one, it will interest in the event happens at "/foo/bar".
  47. // 3. when we delete a directory, we need to force notify all the watchers who watches
  48. // at the file we need to delete.
  49. // For example a watcher is watching at "/foo/bar". And we deletes "/foo". The watcher
  50. // should get notified even if "/foo" is not the path it is watching.
  51. if (w.recursive || originalPath || deleted) && e.Index() >= w.sinceIndex {
  52. // We cannot block here if the eventChan capacity is full, otherwise
  53. // etcd will hang. eventChan capacity is full when the rate of
  54. // notifications are higher than our send rate.
  55. // If this happens, we close the channel.
  56. select {
  57. case w.eventChan <- e:
  58. default:
  59. // We have missed a notification. Remove the watcher.
  60. // Removing the watcher also closes the eventChan.
  61. w.remove()
  62. }
  63. return true
  64. }
  65. return false
  66. }
  67. // Remove removes the watcher from watcherHub
  68. // The actual remove function is guaranteed to only be executed once
  69. func (w *watcher) Remove() {
  70. w.hub.mutex.Lock()
  71. defer w.hub.mutex.Unlock()
  72. close(w.eventChan)
  73. if w.remove != nil {
  74. w.remove()
  75. }
  76. }