doForAtLeast executes the function f. It blocks for at least the passed duration but can go longer. if context is cancelled after the function is done we will bail immediately. in the current use case this means that the process is shutting down we don't force f() to cancel, we assume it also respon
(ctx context.Context, dur time.Duration, f func())
| 427 | // the function is done we will bail immediately. in the current use case this means that the process is shutting down |
| 428 | // we don't force f() to cancel, we assume it also responds to the cancelled context |
| 429 | func doForAtLeast(ctx context.Context, dur time.Duration, f func()) { |
| 430 | startTime := time.Now() |
| 431 | f() |
| 432 | elapsed := time.Since(startTime) |
| 433 | |
| 434 | if elapsed < dur { |
| 435 | ticker := time.NewTicker(dur - elapsed) |
| 436 | defer ticker.Stop() |
| 437 | |
| 438 | select { |
| 439 | case <-ticker.C: |
| 440 | case <-ctx.Done(): |
| 441 | } |
| 442 | } |
| 443 | } |