43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"git.wamblee.org/converge/pkg/models"
|
|
"time"
|
|
)
|
|
|
|
type StateNotifier struct {
|
|
throttler Throttler[models.State]
|
|
eventChannel chan *models.State
|
|
webNotificationChannel chan *models.State
|
|
prometheusNotificationChannel chan *models.State
|
|
}
|
|
|
|
func NewStateNotifier(minDelaySeconds float64) *StateNotifier {
|
|
notifier := StateNotifier{
|
|
eventChannel: make(chan *models.State),
|
|
webNotificationChannel: make(chan *models.State),
|
|
prometheusNotificationChannel: make(chan *models.State),
|
|
}
|
|
notifier.throttler = NewThrottler(func(state *models.State) {
|
|
notifier.webNotificationChannel <- state
|
|
notifier.prometheusNotificationChannel <- state
|
|
}, minDelaySeconds)
|
|
|
|
ticker := time.NewTicker(1 * time.Second)
|
|
go func() {
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
notifier.throttler.ping(time.Now())
|
|
case state := <-notifier.eventChannel:
|
|
notifier.throttler.notify(time.Now(), state)
|
|
}
|
|
}
|
|
}()
|
|
return ¬ifier
|
|
}
|
|
|
|
func (notifier StateNotifier) Publish(state *models.State) {
|
|
notifier.eventChannel <- state
|
|
}
|