1
0
mirror of https://github.com/go-acme/lego.git synced 2025-01-05 16:11:31 +02:00
lego/platform/wait/wait.go

37 lines
696 B
Go
Raw Normal View History

2019-03-11 18:56:48 +02:00
package wait
import (
"fmt"
"time"
2020-09-02 03:20:01 +02:00
"github.com/go-acme/lego/v4/log"
)
// For polls the given function 'f', once every 'interval', up to 'timeout'.
2018-12-22 01:53:05 +02:00
func For(msg string, timeout, interval time.Duration, f func() (bool, error)) error {
log.Infof("Wait for %s [timeout: %s, interval: %s]", msg, timeout, interval)
2020-02-27 20:14:46 +02:00
var lastErr error
2018-10-09 19:03:07 +02:00
timeUp := time.After(timeout)
for {
select {
2018-10-09 19:03:07 +02:00
case <-timeUp:
2021-05-14 17:37:45 +02:00
if lastErr == nil {
2023-05-05 09:49:38 +02:00
return fmt.Errorf("%s: time limit exceeded", msg)
2021-05-14 17:37:45 +02:00
}
2023-05-05 09:49:38 +02:00
return fmt.Errorf("%s: time limit exceeded: last error: %w", msg, lastErr)
default:
}
stop, err := f()
if stop {
return nil
}
if err != nil {
2020-02-27 20:14:46 +02:00
lastErr = err
}
2016-03-11 05:51:02 +02:00
time.Sleep(interval)
}
}