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