watcher.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. Watch(key []byte, prefix bool, startRev int64) int64
  35. // Chan returns a chan. All watch response will be sent to the returned chan.
  36. Chan() <-chan WatchResponse
  37. // Cancel cancels a watcher by giving its ID. If watcher does not exist, an error will be
  38. // returned.
  39. Cancel(id int64) error
  40. // Close closes the WatchChan and release all related resources.
  41. Close()
  42. }
  43. type WatchResponse struct {
  44. // WatchID is the ID of the watcher this response sent to.
  45. WatchID int64
  46. // Events contains all the events that needs to send.
  47. Events []storagepb.Event
  48. }
  49. // watchStream contains a collection of watchers that share
  50. // one streaming chan to send out watched events and other control events.
  51. type watchStream struct {
  52. watchable watchable
  53. ch chan WatchResponse
  54. mu sync.Mutex // guards fields below it
  55. // nextID is the ID pre-allocated for next new watcher in this stream
  56. nextID int64
  57. closed bool
  58. cancels map[int64]cancelFunc
  59. }
  60. // TODO: return error if ws is closed?
  61. func (ws *watchStream) Watch(key []byte, prefix bool, startRev int64) int64 {
  62. ws.mu.Lock()
  63. defer ws.mu.Unlock()
  64. if ws.closed {
  65. return -1
  66. }
  67. id := ws.nextID
  68. ws.nextID++
  69. _, c := ws.watchable.watch(key, prefix, startRev, id, ws.ch)
  70. ws.cancels[id] = c
  71. return id
  72. }
  73. func (ws *watchStream) Chan() <-chan WatchResponse {
  74. return ws.ch
  75. }
  76. func (ws *watchStream) Cancel(id int64) error {
  77. cancel, ok := ws.cancels[id]
  78. if !ok {
  79. return ErrWatcherNotExist
  80. }
  81. cancel()
  82. delete(ws.cancels, id)
  83. return nil
  84. }
  85. func (ws *watchStream) Close() {
  86. ws.mu.Lock()
  87. defer ws.mu.Unlock()
  88. for _, cancel := range ws.cancels {
  89. cancel()
  90. }
  91. ws.closed = true
  92. close(ws.ch)
  93. watchStreamGauge.Dec()
  94. }