30 lines
858 B
Go
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 ¬ifier
|
|
}
|
|
|
|
func (notifier StateNotifier) Publish(state *models.State) {
|
|
notifier.throttler.Notify(state)
|
|
}
|