mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-11-10 04:27:25 +01:00
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package multiChanTicker
|
|
|
|
import (
|
|
"time"
|
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
|
)
|
|
|
|
type multiChanTicker struct {
|
|
ticker *time.Ticker
|
|
channels []chan time.Time
|
|
done chan bool
|
|
}
|
|
|
|
type MultiChanTicker interface {
|
|
Init(duration time.Duration)
|
|
AddChannel(chan time.Time)
|
|
Close()
|
|
}
|
|
|
|
func (t *multiChanTicker) Init(duration time.Duration) {
|
|
t.ticker = time.NewTicker(duration)
|
|
t.done = make(chan bool)
|
|
go func() {
|
|
done := func() {
|
|
close(t.done)
|
|
cclog.ComponentDebug("MultiChanTicker", "DONE")
|
|
}
|
|
for {
|
|
select {
|
|
case <-t.done:
|
|
done()
|
|
return
|
|
case ts := <-t.ticker.C:
|
|
cclog.ComponentDebug("MultiChanTicker", "Tick", ts)
|
|
for _, c := range t.channels {
|
|
select {
|
|
case <-t.done:
|
|
done()
|
|
return
|
|
case c <- ts:
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (t *multiChanTicker) AddChannel(channel chan time.Time) {
|
|
t.channels = append(t.channels, channel)
|
|
}
|
|
|
|
func (t *multiChanTicker) Close() {
|
|
cclog.ComponentDebug("MultiChanTicker", "CLOSE")
|
|
t.done <- true
|
|
// wait for close of channel t.done
|
|
<-t.done
|
|
}
|
|
|
|
func NewTicker(duration time.Duration) MultiChanTicker {
|
|
t := &multiChanTicker{}
|
|
t.Init(duration)
|
|
return t
|
|
}
|