watcher.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. "sync"
  17. "github.com/coreos/etcd/storage/storagepb"
  18. )
  19. // Watcher watches on the KV. It will be notified if there is an event
  20. // happened on the watched key or prefix.
  21. type Watcher interface {
  22. // Event returns a channel that receives observed event that matches the
  23. // context of watcher. When watch finishes or is canceled or aborted, the
  24. // channel is closed and returns empty event.
  25. // Successive calls to Event return the same value.
  26. Event() <-chan storagepb.Event
  27. // Err returns a non-nil error value after Event is closed. Err returns
  28. // Compacted if the history was compacted, Canceled if watch is canceled,
  29. // or EOF if watch reaches the end revision. No other values for Err are defined.
  30. // After Event is closed, successive calls to Err return the same value.
  31. Err() error
  32. }
  33. type watcher struct {
  34. key []byte
  35. prefix bool
  36. cur int64
  37. ch chan storagepb.Event
  38. mu sync.Mutex
  39. err error
  40. }
  41. func newWatcher(key []byte, prefix bool, start int64) *watcher {
  42. return &watcher{
  43. key: key,
  44. prefix: prefix,
  45. cur: start,
  46. ch: make(chan storagepb.Event, 10),
  47. }
  48. }
  49. func (w *watcher) Event() <-chan storagepb.Event { return w.ch }
  50. func (w *watcher) Err() error {
  51. w.mu.Lock()
  52. defer w.mu.Unlock()
  53. return w.err
  54. }
  55. func (w *watcher) stopWithError(err error) {
  56. if w.err != nil {
  57. return
  58. }
  59. close(w.ch)
  60. w.mu.Lock()
  61. w.err = err
  62. w.mu.Unlock()
  63. }