mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
26 lines
496 B
Go
26 lines
496 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// CheckUntil regurly check a predicate until it's true or time out is reached
|
||
|
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):
|
||
|
return fmt.Errorf("Timeout of %ds reached", int64(timeout/time.Second))
|
||
|
}
|
||
|
}
|
||
|
}
|