watcher.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 struct {
  15. EventChan chan *Event
  16. stream bool
  17. recursive bool
  18. sinceIndex uint64
  19. removed bool
  20. remove func()
  21. }
  22. // notify function notifies the watcher. If the watcher interests in the given path,
  23. // the function will return true.
  24. func (w *Watcher) notify(e *Event, originalPath bool, deleted bool) bool {
  25. // watcher is interested the path in three cases and under one condition
  26. // the condition is that the event happens after the watcher's sinceIndex
  27. // 1. the path at which the event happens is the path the watcher is watching at.
  28. // For example if the watcher is watching at "/foo" and the event happens at "/foo",
  29. // the watcher must be interested in that event.
  30. // 2. the watcher is a recursive watcher, it interests in the event happens after
  31. // its watching path. For example if watcher A watches at "/foo" and it is a recursive
  32. // one, it will interest in the event happens at "/foo/bar".
  33. // 3. when we delete a directory, we need to force notify all the watchers who watches
  34. // at the file we need to delete.
  35. // For example a watcher is watching at "/foo/bar". And we deletes "/foo". The watcher
  36. // should get notified even if "/foo" is not the path it is watching.
  37. if (w.recursive || originalPath || deleted) && e.Index() >= w.sinceIndex {
  38. // We cannot block here if the EventChan capacity is full, otherwise
  39. // etcd will hang. EventChan capacity is full when the rate of
  40. // notifications are higher than our send rate.
  41. // If this happens, we close the channel.
  42. select {
  43. case w.EventChan <- e:
  44. default:
  45. // We have missed a notification. Close the channel to indicate this situation.
  46. close(w.EventChan)
  47. }
  48. return true
  49. }
  50. return false
  51. }
  52. // Remove removes the watcher from watcherHub
  53. // The actual remove function is guaranteed to only be executed once
  54. func (w *Watcher) Remove() {
  55. if w.remove != nil {
  56. w.remove()
  57. } else {
  58. // We attached a remove function to watcher
  59. // Other pkg cannot change it, so this should not happen
  60. panic("missing Watcher remove function")
  61. }
  62. }