-
Notifications
You must be signed in to change notification settings - Fork 64
/
event_buffer.go
49 lines (38 loc) · 1001 Bytes
/
event_buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"container/ring"
"sync"
v1 "k8s.io/api/core/v1"
)
// TODO: check if we even need this if we use NewSharedInformerFactory for
// watching events.
const bufferSize = 1000
var mu sync.RWMutex
// TODO: we could have one buffer per namespace
var eventBuffer = ring.New(bufferSize)
func addEventToBuffer(event *v1.Event) {
mu.Lock()
defer mu.Unlock()
eventBuffer.Value = event.DeepCopy()
eventBuffer = eventBuffer.Next()
}
func filterEventsFromBuffer(namespace string, objectKind string, objectName string) []*v1.Event {
mu.RLock()
defer mu.RUnlock()
res := make([]*v1.Event, 0, bufferSize/4)
// FIXME: this can be optimized if the limit is provided and we walk
// backwards.
eventBuffer.Do(func(obj any) {
event, ok := obj.(*v1.Event)
if !ok {
return
}
if event.Namespace != namespace ||
event.InvolvedObject.Kind != objectKind ||
event.InvolvedObject.Name != objectName {
return
}
res = append(res, event.DeepCopy())
})
return res
}