| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package watch |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"sync" |
| 22 |
|
| 23 |
"k8s.io/apimachinery/pkg/runtime" |
| 24 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
type FullChannelBehavior int |
| 30 |
|
| 31 |
const ( |
| 32 |
WaitIfChannelFull FullChannelBehavior = iota |
| 33 |
DropIfChannelFull |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
const incomingQueueLength = 25 |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
type Broadcaster struct { |
| 44 |
watchers map[int64]*broadcasterWatcher |
| 45 |
nextWatcher int64 |
| 46 |
distributing sync.WaitGroup |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
incomingBlock sync.Mutex |
| 51 |
incoming chan Event |
| 52 |
stopped chan struct{} |
| 53 |
|
| 54 |
|
| 55 |
watchQueueLength int |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
fullChannelBehavior FullChannelBehavior |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 82 |
|
| 83 |
|
| 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 |
|
| 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 |
|
| 110 |
return obj |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 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 |
|
| 192 |
func (m *Broadcaster) stopWatching(id int64) { |
| 193 |
m.blockQueue(func() { |
| 194 |
w, ok := m.watchers[id] |
| 195 |
if !ok { |
| 196 |
|
| 197 |
return |
| 198 |
} |
| 199 |
delete(m.watchers, id) |
| 200 |
close(w.result) |
| 201 |
}) |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
func (m *Broadcaster) closeAll() { |
| 206 |
for _, w := range m.watchers { |
| 207 |
close(w.result) |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
m.watchers = map[int64]*broadcasterWatcher{} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 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 |
|
| 229 |
|
| 230 |
|
| 231 |
func (m *Broadcaster) ActionOrDrop(action EventType, obj runtime.Object) (bool, error) { |
| 232 |
m.incomingBlock.Lock() |
| 233 |
defer m.incomingBlock.Unlock() |
| 234 |
|
| 235 |
|
| 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 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 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 |
|
| 265 |
func (m *Broadcaster) loop() { |
| 266 |
|
| 267 |
|
| 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 |
|
| 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: |
| 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 |
|
| 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 |
|
| 309 |
func (mw *broadcasterWatcher) ResultChan() <-chan Event { |
| 310 |
return mw.result |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
func (mw *broadcasterWatcher) Stop() { |
| 316 |
mw.stop.Do(func() { |
| 317 |
close(mw.stopped) |
| 318 |
mw.m.stopWatching(mw.id) |
| 319 |
}) |
| 320 |
} |