allow loop with 0 period

This commit is contained in:
Péter Garamvölgyi
2023-08-23 20:56:08 +08:00
parent d0d24e8075
commit 7488a98c83

View File

@@ -34,14 +34,25 @@ func LoopWithContext(ctx context.Context, period time.Duration, f func(ctx conte
// Loop Run the f func periodically.
func Loop(ctx context.Context, period time.Duration, f func()) {
tick := time.NewTicker(period)
defer tick.Stop()
for ; ; <-tick.C {
select {
case <-ctx.Done():
return
default:
f()
if period == 0*time.Second {
for {
select {
case <-ctx.Done():
return
default:
f()
}
}
} else {
tick := time.NewTicker(period)
defer tick.Stop()
for ; ; <-tick.C {
select {
case <-ctx.Done():
return
default:
f()
}
}
}
}