Do not allow validator to be restarted

This commit is contained in:
Kevin Cotugno 2017-10-19 21:25:40 -07:00
parent e33bd3d689
commit 6c3af1fcd8

View File

@ -19,23 +19,30 @@ type Validator struct {
Logger tacitus.Logger
Products []string
ticker *time.Ticker
done chan bool
done chan struct{}
dirty bool
}
func (v *Validator) Stop() {
if v.ticker == nil || v.done == nil {
v.ticker.Stop()
v.done <- true
if v.done != nil {
select {
case v.done <- struct{}{}:
default:
}
}
}
func (v *Validator) Start(frequency time.Duration) {
v.ticker = time.NewTicker(frequency)
v.done = make(chan bool)
if v.dirty == true {
return
}
v.dirty = true
v.done = make(chan struct{})
go func() {
v.emitProducts()
ticker := time.NewTicker(frequency)
var done bool
@ -43,12 +50,12 @@ func (v *Validator) Start(frequency time.Duration) {
select {
case <-v.done:
done = true
case <-v.ticker.C:
ticker.Stop()
case <-ticker.C:
v.emitProducts()
}
}
v.ticker = nil
v.done = nil
}()
}