From ca68419ba23c193c863d1f4983266960118215ad Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Fri, 16 Aug 2024 19:29:50 +0200 Subject: [PATCH] moved the throttler to its own package. --- cmd/converge/notifier.go | 9 +++++---- .../support/throttling/throtller_test.go | 18 +++++++++--------- .../support/throttling}/throttler.go | 8 ++++---- 3 files changed, 18 insertions(+), 17 deletions(-) rename cmd/converge/throttler_test.go => pkg/support/throttling/throtller_test.go (71%) rename {cmd/converge => pkg/support/throttling}/throttler.go (86%) diff --git a/cmd/converge/notifier.go b/cmd/converge/notifier.go index 72106a6..869db50 100644 --- a/cmd/converge/notifier.go +++ b/cmd/converge/notifier.go @@ -2,11 +2,12 @@ package main import ( "git.wamblee.org/converge/pkg/models" + "git.wamblee.org/converge/pkg/support/throttling" "time" ) type StateNotifier struct { - throttler Throttler[models.State] + throttler throttling.Throttler[models.State] eventChannel chan *models.State webNotificationChannel chan *models.State prometheusNotificationChannel chan *models.State @@ -18,7 +19,7 @@ func NewStateNotifier(minDelaySeconds float64) *StateNotifier { webNotificationChannel: make(chan *models.State), prometheusNotificationChannel: make(chan *models.State), } - notifier.throttler = NewThrottler(func(state *models.State) { + notifier.throttler = throttling.NewThrottler(func(state *models.State) { notifier.webNotificationChannel <- state notifier.prometheusNotificationChannel <- state }, minDelaySeconds) @@ -28,9 +29,9 @@ func NewStateNotifier(minDelaySeconds float64) *StateNotifier { for { select { case <-ticker.C: - notifier.throttler.ping(time.Now()) + notifier.throttler.Ping(time.Now()) case state := <-notifier.eventChannel: - notifier.throttler.notify(time.Now(), state) + notifier.throttler.Notify(time.Now(), state) } } }() diff --git a/cmd/converge/throttler_test.go b/pkg/support/throttling/throtller_test.go similarity index 71% rename from cmd/converge/throttler_test.go rename to pkg/support/throttling/throtller_test.go index 57abc73..9c5fba7 100644 --- a/cmd/converge/throttler_test.go +++ b/pkg/support/throttling/throtller_test.go @@ -1,4 +1,4 @@ -package main +package throttling import ( "github.com/stretchr/testify/assert" @@ -14,11 +14,11 @@ func Test_throttlerImmediateNotificationAfterInitialized(t *testing.T) { t0 := time.Now() v := 1 - throttler.notify(t0, &v) + throttler.Notify(t0, &v) assert.Equal(t, v, value) value = 0 // subsequent ping will not lead to a notification - throttler.ping(t0.Add(10 * time.Second)) + throttler.Ping(t0.Add(10 * time.Second)) assert.Equal(t, 0, value) } @@ -34,17 +34,17 @@ func Test_TwoNotificationsInSHortSucessionSecondOneIsDeliverdWithDelay(t *testin // v2 will not be delivered, the last value in the time interval will be v2 := 2 v3 := 3 - throttler.notify(t0, &v1) + throttler.Notify(t0, &v1) assert.Equal(t, v1, value) - throttler.notify(t0, &v2) - throttler.notify(t0, &v3) + throttler.Notify(t0, &v2) + throttler.Notify(t0, &v3) assert.Equal(t, v1, value) - throttler.ping(t0.Add(time.Duration(delayMs-1) * time.Millisecond)) + throttler.Ping(t0.Add(time.Duration(delayMs-1) * time.Millisecond)) assert.Equal(t, v1, value) - throttler.ping(t0.Add(time.Duration(delayMs) * time.Millisecond)) + throttler.Ping(t0.Add(time.Duration(delayMs) * time.Millisecond)) assert.Equal(t, v3, value) value = 0 // another ping won' deliver the same value again. - throttler.ping(t0.Add(time.Duration(delayMs*2) * time.Millisecond)) + throttler.Ping(t0.Add(time.Duration(delayMs*2) * time.Millisecond)) assert.Equal(t, 0, value) } diff --git a/cmd/converge/throttler.go b/pkg/support/throttling/throttler.go similarity index 86% rename from cmd/converge/throttler.go rename to pkg/support/throttling/throttler.go index 275085d..345f582 100644 --- a/cmd/converge/throttler.go +++ b/pkg/support/throttling/throttler.go @@ -1,4 +1,4 @@ -package main +package throttling import "time" @@ -26,7 +26,7 @@ func NewThrottler[T any](notifier func(t *T), minDelaySeconds float64) Throttler // for the last notification to be sent. If not, it is stored as a pending event to // be sent later. New events that come in before a notification is sent override the // pending event. -func (throttler *Throttler[T]) notify(time time.Time, value *T) { +func (throttler *Throttler[T]) Notify(time time.Time, value *T) { if time.Sub(throttler.lastReportedTime).Seconds() >= throttler.minDelaySeconds { throttler.notifier(value) throttler.lastReportedTime = time @@ -38,8 +38,8 @@ func (throttler *Throttler[T]) notify(time time.Time, value *T) { // To be called periodically. It sends out any pending events if the time the last // notification was sent is long enough ago. -func (throttler *Throttler[T]) ping(time time.Time) { +func (throttler *Throttler[T]) Ping(time time.Time) { if throttler.pendingValue != nil { - throttler.notify(time, throttler.pendingValue) + throttler.Notify(time, throttler.pendingValue) } }