pkg/up: stop *all* goroutines (#1676)

Stop all goroutines after we get the stop signal.
This commit is contained in:
Miek Gieben
2018-04-13 16:01:12 +01:00
committed by GitHub
parent e671e22e65
commit 662edf6607

View File

@@ -16,7 +16,7 @@ type Probe struct {
target string target string
sync.Mutex sync.Mutex
inprogress bool inprogress int
} }
// Func is used to determine if a target is alive. If so this function must return nil. // Func is used to determine if a target is alive. If so this function must return nil.
@@ -40,14 +40,17 @@ func (p *Probe) start(interval time.Duration) {
for { for {
select { select {
case <-p.stop: case <-p.stop:
p.Lock()
p.inprogress = stop
p.Unlock()
return return
case f := <-p.do: case f := <-p.do:
p.Lock() p.Lock()
if p.inprogress { if p.inprogress == active || p.inprogress == stop {
p.Unlock() p.Unlock()
continue continue
} }
p.inprogress = true p.inprogress = active
p.Unlock() p.Unlock()
// Passed the lock. Now run f for as long it returns false. If a true is returned // Passed the lock. Now run f for as long it returns false. If a true is returned
@@ -57,13 +60,25 @@ func (p *Probe) start(interval time.Duration) {
if err := f(); err == nil { if err := f(); err == nil {
break break
} }
// TODO(miek): little bit of exponential backoff here?
time.Sleep(interval) time.Sleep(interval)
}
p.Lock() p.Lock()
p.inprogress = false if p.inprogress == stop {
p.Unlock()
return
}
p.Unlock()
}
p.Lock()
p.inprogress = idle
p.Unlock() p.Unlock()
}() }()
} }
} }
} }
const (
idle = iota
active
stop
)