traefik/vendor/github.com/xenolf/lego/platform/wait/wait.go

34 lines
610 B
Go
Raw Normal View History

2019-01-07 18:30:06 +01:00
package wait
2018-03-26 14:12:03 +02:00
import (
"fmt"
"time"
2018-10-10 16:28:04 +02:00
"github.com/xenolf/lego/log"
2018-03-26 14:12:03 +02:00
)
2019-01-07 18:30:06 +01:00
// For polls the given function 'f', once every 'interval', up to 'timeout'.
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)
2018-10-10 16:28:04 +02:00
2018-03-26 14:12:03 +02:00
var lastErr string
2018-10-10 16:28:04 +02:00
timeUp := time.After(timeout)
2018-03-26 14:12:03 +02:00
for {
select {
2018-10-10 16:28:04 +02:00
case <-timeUp:
return fmt.Errorf("time limit exceeded: last error: %s", lastErr)
2018-03-26 14:12:03 +02:00
default:
}
stop, err := f()
if stop {
return nil
}
if err != nil {
lastErr = err.Error()
}
time.Sleep(interval)
}
}