watcher.go 1.3 KB

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