mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-27 07:39:05 +01:00
40 lines
668 B
Go
40 lines
668 B
Go
|
package multiChanTicker
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type multiChanTicker struct {
|
||
|
ticker *time.Ticker
|
||
|
channels []chan time.Time
|
||
|
}
|
||
|
|
||
|
type MultiChanTicker interface {
|
||
|
Init(duration time.Duration)
|
||
|
AddChannel(chan time.Time)
|
||
|
}
|
||
|
|
||
|
func (t *multiChanTicker) Init(duration time.Duration) {
|
||
|
t.ticker = time.NewTicker(duration)
|
||
|
go func() {
|
||
|
for {
|
||
|
select {
|
||
|
case ts := <-t.ticker.C:
|
||
|
for _, c := range t.channels {
|
||
|
c <- ts
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
func (t *multiChanTicker) AddChannel(channel chan time.Time) {
|
||
|
t.channels = append(t.channels, channel)
|
||
|
}
|
||
|
|
||
|
func NewTicker(duration time.Duration) MultiChanTicker {
|
||
|
t := &multiChanTicker{}
|
||
|
t.Init(duration)
|
||
|
return t
|
||
|
}
|