Add a util.Forever variant that ends on a stop channel
This commit is contained in:
@@ -51,7 +51,19 @@ func HandleCrash() {
|
||||
|
||||
// Forever loops forever running f every d. Catches any panics, and keeps going.
|
||||
func Forever(f func(), period time.Duration) {
|
||||
Until(f, period, nil)
|
||||
}
|
||||
|
||||
// Until loops until stop channel is closed, running f every d.
|
||||
// Catches any panics, and keeps going. f may not be invoked if
|
||||
// stop channel is already closed.
|
||||
func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
|
||||
for {
|
||||
select {
|
||||
case <-stopCh:
|
||||
return
|
||||
default:
|
||||
}
|
||||
func() {
|
||||
defer HandleCrash()
|
||||
f()
|
||||
|
Reference in New Issue
Block a user