watcher.go 3.0 KB

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