From f4a3fac4da12c63fc3d48c5aea3a4a2efe72d39f Mon Sep 17 00:00:00 2001 From: Ned Anderson Date: Thu, 31 Aug 2023 21:42:39 -0400 Subject: [PATCH] chore: genericize event refresher list --- pkg/controller/common.go | 57 ++++++++++++++++------------------ pkg/controller/shared_state.go | 22 ++++++++----- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/pkg/controller/common.go b/pkg/controller/common.go index d0bd08630f..481ed1911f 100644 --- a/pkg/controller/common.go +++ b/pkg/controller/common.go @@ -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 } -func (oldEvent *eventRefresherNode) tryRemove() { - if oldEvent != nil { - oldEvent.remove() +func (oldNode *circularLinkedListNode[T]) tryRemove() { + if oldNode != nil { + oldNode.remove() } } diff --git a/pkg/controller/shared_state.go b/pkg/controller/shared_state.go index fbfbcde9e4..6bcb547a12 100644 --- a/pkg/controller/shared_state.go +++ b/pkg/controller/shared_state.go @@ -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 @@ -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) }