watcher.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. type watcher struct {
  20. key []byte
  21. prefix bool
  22. cur int64
  23. ch chan storagepb.Event
  24. mu sync.Mutex
  25. err error
  26. }
  27. func newWatcher(key []byte, prefix bool, start int64) *watcher {
  28. return &watcher{
  29. key: key,
  30. prefix: prefix,
  31. cur: start,
  32. ch: make(chan storagepb.Event, 10),
  33. }
  34. }
  35. func (w *watcher) Event() <-chan storagepb.Event { return w.ch }
  36. func (w *watcher) Err() error {
  37. w.mu.Lock()
  38. defer w.mu.Unlock()
  39. return w.err
  40. }
  41. func (w *watcher) stopWithError(err error) {
  42. if w.err != nil {
  43. return
  44. }
  45. close(w.ch)
  46. w.mu.Lock()
  47. w.err = err
  48. w.mu.Unlock()
  49. }