watcher.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2013 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. type Watcher interface {
  15. EventChan() chan *Event
  16. Remove()
  17. }
  18. type watcher struct {
  19. eventChan chan *Event
  20. stream bool
  21. recursive bool
  22. sinceIndex uint64
  23. hub *watcherHub
  24. removed bool
  25. remove func()
  26. }
  27. func (w *watcher) EventChan() chan *Event {
  28. return w.eventChan
  29. }
  30. // notify function notifies the watcher. If the watcher interests in the given path,
  31. // the function will return true.
  32. func (w *watcher) notify(e *Event, originalPath bool, deleted bool) bool {
  33. // watcher is interested the path in three cases and under one condition
  34. // the condition is that the event happens after the watcher's sinceIndex
  35. // 1. the path at which the event happens is the path the watcher is watching at.
  36. // For example if the watcher is watching at "/foo" and the event happens at "/foo",
  37. // the watcher must be interested in that event.
  38. // 2. the watcher is a recursive watcher, it interests in the event happens after
  39. // its watching path. For example if watcher A watches at "/foo" and it is a recursive
  40. // one, it will interest in the event happens at "/foo/bar".
  41. // 3. when we delete a directory, we need to force notify all the watchers who watches
  42. // at the file we need to delete.
  43. // For example a watcher is watching at "/foo/bar". And we deletes "/foo". The watcher
  44. // should get notified even if "/foo" is not the path it is watching.
  45. if (w.recursive || originalPath || deleted) && e.Index() >= w.sinceIndex {
  46. // We cannot block here if the eventChan capacity is full, otherwise
  47. // etcd will hang. eventChan capacity is full when the rate of
  48. // notifications are higher than our send rate.
  49. // If this happens, we close the channel.
  50. select {
  51. case w.eventChan <- e:
  52. default:
  53. // We have missed a notification. Remove the watcher.
  54. // Removing the watcher also closes the eventChan.
  55. w.remove()
  56. }
  57. return true
  58. }
  59. return false
  60. }
  61. // Remove removes the watcher from watcherHub
  62. // The actual remove function is guaranteed to only be executed once
  63. func (w *watcher) Remove() {
  64. w.hub.mutex.Lock()
  65. defer w.hub.mutex.Unlock()
  66. close(w.eventChan)
  67. if w.remove != nil {
  68. w.remove()
  69. }
  70. }