watcher.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 storage
  15. import (
  16. "errors"
  17. "sync"
  18. "github.com/coreos/etcd/storage/storagepb"
  19. )
  20. var (
  21. ErrWatcherNotExist = errors.New("storage: watcher does not exist")
  22. )
  23. type WatchStream interface {
  24. // Watch creates a watcher. The watcher watches the events happening or
  25. // happened on the given key or key prefix from the given startRev.
  26. //
  27. // The whole event history can be watched unless compacted.
  28. // If `prefix` is true, watch observes all events whose key prefix could be the given `key`.
  29. // If `startRev` <=0, watch observes events after currentRev.
  30. //
  31. // The returned `id` is the ID of this watcher. It appears as WatchID
  32. // in events that are sent to the created watcher through stream channel.
  33. //
  34. // TODO: remove the returned CancelFunc. Always use Cancel.
  35. Watch(key []byte, prefix bool, startRev int64) (id int64, cancel CancelFunc)
  36. // Chan returns a chan. All watched events will be sent to the returned chan.
  37. Chan() <-chan []storagepb.Event
  38. // Cancel cancels a watcher by giving its ID. If watcher does not exist, an error will be
  39. // returned.
  40. Cancel(id int64) error
  41. // Close closes the WatchChan and release all related resources.
  42. Close()
  43. }
  44. // watchStream contains a collection of watchers that share
  45. // one streaming chan to send out watched events and other control events.
  46. type watchStream struct {
  47. watchable watchable
  48. ch chan []storagepb.Event
  49. mu sync.Mutex // guards fields below it
  50. // nextID is the ID pre-allocated for next new watcher in this stream
  51. nextID int64
  52. closed bool
  53. cancels map[int64]CancelFunc
  54. }
  55. // TODO: return error if ws is closed?
  56. func (ws *watchStream) Watch(key []byte, prefix bool, startRev int64) (id int64, cancel CancelFunc) {
  57. ws.mu.Lock()
  58. defer ws.mu.Unlock()
  59. if ws.closed {
  60. return -1, nil
  61. }
  62. id = ws.nextID
  63. ws.nextID++
  64. _, c := ws.watchable.watch(key, prefix, startRev, id, ws.ch)
  65. ws.cancels[id] = c
  66. return id, c
  67. }
  68. func (ws *watchStream) Chan() <-chan []storagepb.Event {
  69. return ws.ch
  70. }
  71. func (ws *watchStream) Cancel(id int64) error {
  72. cancel, ok := ws.cancels[id]
  73. if !ok {
  74. return ErrWatcherNotExist
  75. }
  76. cancel()
  77. delete(ws.cancels, id)
  78. return nil
  79. }
  80. func (ws *watchStream) Close() {
  81. ws.mu.Lock()
  82. defer ws.mu.Unlock()
  83. for _, cancel := range ws.cancels {
  84. cancel()
  85. }
  86. ws.closed = true
  87. close(ws.ch)
  88. watchStreamGauge.Dec()
  89. }