Use central timer for collectors and router. Add expressions to router

This commit is contained in:
Thomas Roehl
2021-12-20 17:40:28 +01:00
parent 44d8b0c979
commit b7fbd198ff
4 changed files with 152 additions and 19 deletions

View File

@@ -0,0 +1,39 @@
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
}