2019-11-02 21:32:58 +07:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-01-21 07:10:00 +07:00
|
|
|
// CheckUntil regularly check a predicate until it's true or time out is reached.
|
2019-11-02 21:32:58 +07:00
|
|
|
func CheckUntil(interval time.Duration, timeout time.Duration, predicate func() (bool, error)) error {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(interval):
|
|
|
|
predTrue, err := predicate()
|
|
|
|
if predTrue {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case <-time.After(timeout):
|
2021-07-04 05:08:24 +07:00
|
|
|
return fmt.Errorf("timeout of %ds reached", int64(timeout/time.Second))
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|