Skip to content

Commit

Permalink
chore: genericize event refresher list
Browse files Browse the repository at this point in the history
  • Loading branch information
NedAnd1 committed Sep 1, 2023
1 parent f09b7f8 commit f4a3fac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
57 changes: 27 additions & 30 deletions pkg/controller/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1189,51 +1189,48 @@ func WatchObject(mgr manager.Manager, objKind source.Kind) error {
return err
}

// circular linked list with simple accessibility to the current event, etc.
type eventRefresherList struct {
curr *eventRefresherNode
type circularLinkedList[T any] struct {
*circularLinkedListNode[T]
}

type eventRefresherNode struct {
prev *eventRefresherNode
next *eventRefresherNode
message string
timestamp time.Time
objects []runtime.Object
type circularLinkedListNode[T any] struct {
curr T
prev *circularLinkedListNode[T]
next *circularLinkedListNode[T]
}

func (events eventRefresherList) isEmpty() bool {
return events.curr == nil
func (list circularLinkedList[T]) isEmpty() bool {
return list.circularLinkedListNode == nil
}

func (events *eventRefresherList) clear() {
events.curr = nil
func (list *circularLinkedList[T]) clear() {
list.circularLinkedListNode = nil
}

func (events *eventRefresherList) add(newEvent *eventRefresherNode) {
if events.curr == nil {
events.curr = newEvent
func (list *circularLinkedList[T]) add(newNode *circularLinkedListNode[T]) {
if list.circularLinkedListNode == nil {
list.circularLinkedListNode = newNode
} else {
events.curr.prev.next = newEvent
list.prev.next = newNode
}
newEvent.next = events.curr
events.curr.prev = newEvent
newNode.next = list.circularLinkedListNode
list.prev = newNode
}

// Moves to the next event in the list and retrieves it.
func (events *eventRefresherList) next() *eventRefresherNode {
nextEvent := events.curr.next
events.curr = nextEvent
return nextEvent
// Moves to the next node in the list and retrieves it.
func (list *circularLinkedList[T]) next() *circularLinkedListNode[T] {
nextNode := list.circularLinkedListNode.next
list.circularLinkedListNode = nextNode
return nextNode
}

func (oldEvent *eventRefresherNode) remove() {
oldEvent.prev.next = oldEvent.next
oldEvent.next.prev = oldEvent.prev
func (oldNode *circularLinkedListNode[T]) remove() {
oldNode.prev.next = oldNode.next
oldNode.next.prev = oldNode.prev
}

Check failure on line 1231 in pkg/controller/common.go

View workflow job for this annotation

GitHub Actions / Go Lint

receiver name oldNode should be consistent with previous receiver name list for invalid-type (golint)
func (oldEvent *eventRefresherNode) tryRemove() {
if oldEvent != nil {
oldEvent.remove()
func (oldNode *circularLinkedListNode[T]) tryRemove() {
if oldNode != nil {
oldNode.remove()
}
}

Check failure on line 1236 in pkg/controller/common.go

View workflow job for this annotation

GitHub Actions / Go Lint

receiver name oldNode should be consistent with previous receiver name list for invalid-type (golint)
22 changes: 15 additions & 7 deletions pkg/controller/shared_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1400,10 +1400,16 @@ func (c *SharedState) createEventQueues() {
}

func (c *SharedState) _eventRefresherRoutine() {
type eventInfo struct {
message string
timestamp time.Time
objects []runtime.Object
}

var eventTTL = time.Duration(c.config.ControllerConfig.EventTTLInSec) * time.Second
eventMap := map[string]*eventRefresherNode{}
eventMap := map[string]*circularLinkedListNode[eventInfo]{}

var events eventRefresherList
var events circularLinkedList[eventInfo]
var lastTime time.Time
var delay time.Duration // how long the alarm was last set to wait for
expLatency := eventOverlapVariance // we request the alarm to wake us up earlier by this amount, to negate latency due to other operations and timer imprecision
Expand All @@ -1416,15 +1422,17 @@ func (c *SharedState) _eventRefresherRoutine() {
select {
case newFailureInfo := <-c.eventsToPersistQueue:
// add the new failure
newEvent := &eventRefresherNode{
message: newFailureInfo.message,
objects: newFailureInfo.pods,
timestamp: newFailureInfo.timestamp.Add(eventTTL - eventOverlapDuration),
newEvent := &circularLinkedListNode[eventInfo]{
curr: eventInfo{
message: newFailureInfo.message,
objects: newFailureInfo.pods,
timestamp: newFailureInfo.timestamp.Add(eventTTL - eventOverlapDuration),
},
}

if events.isEmpty() {
lastTime = time.Now()
delay = newEvent.timestamp.Sub(lastTime) - expLatency
delay = newEvent.curr.timestamp.Sub(lastTime) - expLatency
alarm.Reset(delay)
}

Expand Down

0 comments on commit f4a3fac

Please sign in to comment.