dispatch/vendor/github.com/xenolf/lego/acme/utils.go

34 lines
594 B
Go
Raw Normal View History

package acme
import (
"fmt"
"time"
2018-11-10 07:04:36 +00:00
"github.com/xenolf/lego/log"
)
// WaitFor polls the given function 'f', once every 'interval', up to 'timeout'.
func WaitFor(timeout, interval time.Duration, f func() (bool, error)) error {
2018-11-10 07:04:36 +00:00
log.Infof("Wait [timeout: %s, interval: %s]", timeout, interval)
var lastErr string
2018-11-10 07:04:36 +00:00
timeUp := time.After(timeout)
for {
select {
2018-11-10 07:04:36 +00:00
case <-timeUp:
return fmt.Errorf("time limit exceeded: last error: %s", lastErr)
default:
}
stop, err := f()
if stop {
return nil
}
if err != nil {
lastErr = err.Error()
}
time.Sleep(interval)
}
}