32 lines
952 B
Go
32 lines
952 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"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),
|
|
}
|
|
// Runs indefinitely, context does not need to be canceled.
|
|
notifier.throttler = throttling.NewAsyncThrottler(context.Background(), func(state *models.State) {
|
|
notifier.webNotificationChannel <- state
|
|
notifier.prometheusNotificationChannel <- state
|
|
}, minDelay, 1*time.Second)
|
|
return ¬ifier
|
|
}
|
|
|
|
func (notifier StateNotifier) Publish(state *models.State) {
|
|
notifier.throttler.Notify(state)
|
|
}
|