sampler-fork/data/poller.go

39 lines
553 B
Go
Raw Normal View History

2019-01-31 00:02:38 +00:00
package data
import (
"time"
)
type Poller struct {
consumer Consumer
2019-01-31 23:40:05 +00:00
item Item
2019-01-31 00:02:38 +00:00
}
2019-01-31 23:40:05 +00:00
func NewPoller(consumer Consumer, item Item, rateMs int) Poller {
2019-01-31 00:02:38 +00:00
ticker := time.NewTicker(time.Duration(rateMs * int(time.Millisecond)))
2019-02-01 05:07:25 +00:00
poller := Poller{consumer, item}
2019-01-31 00:02:38 +00:00
go func() {
for {
select {
case <-ticker.C:
poller.poll()
}
}
}()
return poller
}
func (self *Poller) poll() {
2019-01-31 23:40:05 +00:00
value, err := self.item.nextValue()
2019-01-31 00:02:38 +00:00
if err != nil {
2019-01-31 23:40:05 +00:00
self.consumer.ConsumeError(self.item, err)
2019-01-31 00:02:38 +00:00
}
2019-01-31 23:40:05 +00:00
self.consumer.ConsumeValue(self.item, value)
2019-01-31 00:02:38 +00:00
}