mux.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package watch
  14. import (
  15. "fmt"
  16. "sync"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. )
  20. // FullChannelBehavior controls how the Broadcaster reacts if a watcher's watch
  21. // channel is full.
  22. type FullChannelBehavior int
  23. const (
  24. WaitIfChannelFull FullChannelBehavior = iota
  25. DropIfChannelFull
  26. )
  27. // Buffer the incoming queue a little bit even though it should rarely ever accumulate
  28. // anything, just in case a few events are received in such a short window that
  29. // Broadcaster can't move them onto the watchers' queues fast enough.
  30. const incomingQueueLength = 25
  31. // Broadcaster distributes event notifications among any number of watchers. Every event
  32. // is delivered to every watcher.
  33. type Broadcaster struct {
  34. watchers map[int64]*broadcasterWatcher
  35. nextWatcher int64
  36. distributing sync.WaitGroup
  37. // incomingBlock allows us to ensure we don't race and end up sending events
  38. // to a closed channel following a broadcaster shutdown.
  39. incomingBlock sync.Mutex
  40. incoming chan Event
  41. stopped chan struct{}
  42. // How large to make watcher's channel.
  43. watchQueueLength int
  44. // If one of the watch channels is full, don't wait for it to become empty.
  45. // Instead just deliver it to the watchers that do have space in their
  46. // channels and move on to the next event.
  47. // It's more fair to do this on a per-watcher basis than to do it on the
  48. // "incoming" channel, which would allow one slow watcher to prevent all
  49. // other watchers from getting new events.
  50. fullChannelBehavior FullChannelBehavior
  51. }
  52. // NewBroadcaster creates a new Broadcaster. queueLength is the maximum number of events to queue per watcher.
  53. // It is guaranteed that events will be distributed in the order in which they occur,
  54. // but the order in which a single event is distributed among all of the watchers is unspecified.
  55. func NewBroadcaster(queueLength int, fullChannelBehavior FullChannelBehavior) *Broadcaster {
  56. m := &Broadcaster{
  57. watchers: map[int64]*broadcasterWatcher{},
  58. incoming: make(chan Event, incomingQueueLength),
  59. stopped: make(chan struct{}),
  60. watchQueueLength: queueLength,
  61. fullChannelBehavior: fullChannelBehavior,
  62. }
  63. m.distributing.Add(1)
  64. go m.loop()
  65. return m
  66. }
  67. // NewLongQueueBroadcaster functions nearly identically to NewBroadcaster,
  68. // except that the incoming queue is the same size as the outgoing queues
  69. // (specified by queueLength).
  70. func NewLongQueueBroadcaster(queueLength int, fullChannelBehavior FullChannelBehavior) *Broadcaster {
  71. m := &Broadcaster{
  72. watchers: map[int64]*broadcasterWatcher{},
  73. incoming: make(chan Event, queueLength),
  74. stopped: make(chan struct{}),
  75. watchQueueLength: queueLength,
  76. fullChannelBehavior: fullChannelBehavior,
  77. }
  78. m.distributing.Add(1)
  79. go m.loop()
  80. return m
  81. }
  82. const internalRunFunctionMarker = "internal-do-function"
  83. // a function type we can shoehorn into the queue.
  84. type functionFakeRuntimeObject func()
  85. func (obj functionFakeRuntimeObject) GetObjectKind() schema.ObjectKind {
  86. return schema.EmptyObjectKind
  87. }
  88. func (obj functionFakeRuntimeObject) DeepCopyObject() runtime.Object {
  89. if obj == nil {
  90. return nil
  91. }
  92. // funcs are immutable. Hence, just return the original func.
  93. return obj
  94. }
  95. // Execute f, blocking the incoming queue (and waiting for it to drain first).
  96. // The purpose of this terrible hack is so that watchers added after an event
  97. // won't ever see that event, and will always see any event after they are
  98. // added.
  99. func (m *Broadcaster) blockQueue(f func()) {
  100. m.incomingBlock.Lock()
  101. defer m.incomingBlock.Unlock()
  102. select {
  103. case <-m.stopped:
  104. return
  105. default:
  106. }
  107. var wg sync.WaitGroup
  108. wg.Add(1)
  109. m.incoming <- Event{
  110. Type: internalRunFunctionMarker,
  111. Object: functionFakeRuntimeObject(func() {
  112. defer wg.Done()
  113. f()
  114. }),
  115. }
  116. wg.Wait()
  117. }
  118. // Watch adds a new watcher to the list and returns an Interface for it.
  119. // Note: new watchers will only receive new events. They won't get an entire history
  120. // of previous events. It will block until the watcher is actually added to the
  121. // broadcaster.
  122. func (m *Broadcaster) Watch() (Interface, error) {
  123. var w *broadcasterWatcher
  124. m.blockQueue(func() {
  125. id := m.nextWatcher
  126. m.nextWatcher++
  127. w = &broadcasterWatcher{
  128. result: make(chan Event, m.watchQueueLength),
  129. stopped: make(chan struct{}),
  130. id: id,
  131. m: m,
  132. }
  133. m.watchers[id] = w
  134. })
  135. if w == nil {
  136. return nil, fmt.Errorf("broadcaster already stopped")
  137. }
  138. return w, nil
  139. }
  140. // WatchWithPrefix adds a new watcher to the list and returns an Interface for it. It sends
  141. // queuedEvents down the new watch before beginning to send ordinary events from Broadcaster.
  142. // The returned watch will have a queue length that is at least large enough to accommodate
  143. // all of the items in queuedEvents. It will block until the watcher is actually added to
  144. // the broadcaster.
  145. func (m *Broadcaster) WatchWithPrefix(queuedEvents []Event) (Interface, error) {
  146. var w *broadcasterWatcher
  147. m.blockQueue(func() {
  148. id := m.nextWatcher
  149. m.nextWatcher++
  150. length := m.watchQueueLength
  151. if n := len(queuedEvents) + 1; n > length {
  152. length = n
  153. }
  154. w = &broadcasterWatcher{
  155. result: make(chan Event, length),
  156. stopped: make(chan struct{}),
  157. id: id,
  158. m: m,
  159. }
  160. m.watchers[id] = w
  161. for _, e := range queuedEvents {
  162. w.result <- e
  163. }
  164. })
  165. if w == nil {
  166. return nil, fmt.Errorf("broadcaster already stopped")
  167. }
  168. return w, nil
  169. }
  170. // stopWatching stops the given watcher and removes it from the list.
  171. func (m *Broadcaster) stopWatching(id int64) {
  172. m.blockQueue(func() {
  173. w, ok := m.watchers[id]
  174. if !ok {
  175. // No need to do anything, it's already been removed from the list.
  176. return
  177. }
  178. delete(m.watchers, id)
  179. close(w.result)
  180. })
  181. }
  182. // closeAll disconnects all watchers (presumably in response to a Shutdown call).
  183. func (m *Broadcaster) closeAll() {
  184. for _, w := range m.watchers {
  185. close(w.result)
  186. }
  187. // Delete everything from the map, since presence/absence in the map is used
  188. // by stopWatching to avoid double-closing the channel.
  189. m.watchers = map[int64]*broadcasterWatcher{}
  190. }
  191. // Action distributes the given event among all watchers.
  192. func (m *Broadcaster) Action(action EventType, obj runtime.Object) error {
  193. m.incomingBlock.Lock()
  194. defer m.incomingBlock.Unlock()
  195. select {
  196. case <-m.stopped:
  197. return fmt.Errorf("broadcaster already stopped")
  198. default:
  199. }
  200. m.incoming <- Event{action, obj}
  201. return nil
  202. }
  203. // Action distributes the given event among all watchers, or drops it on the floor
  204. // if too many incoming actions are queued up. Returns true if the action was sent,
  205. // false if dropped.
  206. func (m *Broadcaster) ActionOrDrop(action EventType, obj runtime.Object) (bool, error) {
  207. m.incomingBlock.Lock()
  208. defer m.incomingBlock.Unlock()
  209. // Ensure that if the broadcaster is stopped we do not send events to it.
  210. select {
  211. case <-m.stopped:
  212. return false, fmt.Errorf("broadcaster already stopped")
  213. default:
  214. }
  215. select {
  216. case m.incoming <- Event{action, obj}:
  217. return true, nil
  218. default:
  219. return false, nil
  220. }
  221. }
  222. // Shutdown disconnects all watchers (but any queued events will still be distributed).
  223. // You must not call Action or Watch* after calling Shutdown. This call blocks
  224. // until all events have been distributed through the outbound channels. Note
  225. // that since they can be buffered, this means that the watchers might not
  226. // have received the data yet as it can remain sitting in the buffered
  227. // channel. It will block until the broadcaster stop request is actually executed
  228. func (m *Broadcaster) Shutdown() {
  229. m.blockQueue(func() {
  230. close(m.stopped)
  231. close(m.incoming)
  232. })
  233. m.distributing.Wait()
  234. }
  235. // loop receives from m.incoming and distributes to all watchers.
  236. func (m *Broadcaster) loop() {
  237. // Deliberately not catching crashes here. Yes, bring down the process if there's a
  238. // bug in watch.Broadcaster.
  239. for event := range m.incoming {
  240. if event.Type == internalRunFunctionMarker {
  241. event.Object.(functionFakeRuntimeObject)()
  242. continue
  243. }
  244. m.distribute(event)
  245. }
  246. m.closeAll()
  247. m.distributing.Done()
  248. }
  249. // distribute sends event to all watchers. Blocking.
  250. func (m *Broadcaster) distribute(event Event) {
  251. if m.fullChannelBehavior == DropIfChannelFull {
  252. for _, w := range m.watchers {
  253. select {
  254. case w.result <- event:
  255. case <-w.stopped:
  256. default: // Don't block if the event can't be queued.
  257. }
  258. }
  259. } else {
  260. for _, w := range m.watchers {
  261. select {
  262. case w.result <- event:
  263. case <-w.stopped:
  264. }
  265. }
  266. }
  267. }
  268. // broadcasterWatcher handles a single watcher of a broadcaster
  269. type broadcasterWatcher struct {
  270. result chan Event
  271. stopped chan struct{}
  272. stop sync.Once
  273. id int64
  274. m *Broadcaster
  275. }
  276. // ResultChan returns a channel to use for waiting on events.
  277. func (mw *broadcasterWatcher) ResultChan() <-chan Event {
  278. return mw.result
  279. }
  280. // Stop stops watching and removes mw from its list.
  281. // It will block until the watcher stop request is actually executed
  282. func (mw *broadcasterWatcher) Stop() {
  283. mw.stop.Do(func() {
  284. close(mw.stopped)
  285. mw.m.stopWatching(mw.id)
  286. })
  287. }