mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-02 14:56:38 +08:00
27 lines
447 B
Go
27 lines
447 B
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|