Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
32 lines
599 B
Go
32 lines
599 B
Go
package ctxticker
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// Do calls fn every d until ctx canceled or fn returns with a non-nil error.
|
|
func Do(ctx context.Context, d time.Duration, fn func() error) error {
|
|
ticker := time.NewTicker(d)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
|
|
case <-ticker.C:
|
|
if err := fn(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// DoNow is same as Do except it makes +1 call to fn on start.
|
|
func DoNow(ctx context.Context, d time.Duration, fn func() error) error {
|
|
if err := fn(); err != nil {
|
|
return err
|
|
}
|
|
return Do(ctx, d, fn)
|
|
}
|