converge/cmd/converge/notifier.go
Erik Brakkee 52160a368c more generalization of how time is handled in the tests.
Asynchronous variant that is easier to use and multi-thread safe.
2024-08-16 21:08:44 +02:00

30 lines
858 B
Go

package main
import (
"git.wamblee.org/converge/pkg/models"
"git.wamblee.org/converge/pkg/support/throttling"
"time"
)
type StateNotifier struct {
throttler *throttling.AsyncThrottler[models.State]
webNotificationChannel chan *models.State
prometheusNotificationChannel chan *models.State
}
func NewStateNotifier(minDelay time.Duration) *StateNotifier {
notifier := StateNotifier{
webNotificationChannel: make(chan *models.State),
prometheusNotificationChannel: make(chan *models.State),
}
notifier.throttler = throttling.NewAsyncThrottler(func(state *models.State) {
notifier.webNotificationChannel <- state
notifier.prometheusNotificationChannel <- state
}, minDelay, 1*time.Second)
return &notifier
}
func (notifier StateNotifier) Publish(state *models.State) {
notifier.throttler.Notify(state)
}