mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 14:06:36 +08:00
24 lines
378 B
Go
24 lines
378 B
Go
package heartbeat
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// RunLoop invokes fn immediately, then on each interval tick until ctx is cancelled.
|
|
func RunLoop(ctx context.Context, interval time.Duration, fn func(context.Context)) {
|
|
fn(ctx)
|
|
|
|
ticker := time.NewTicker(interval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
fn(ctx)
|
|
}
|
|
}
|
|
}
|