summaryrefslogtreecommitdiff
path: root/internal/utils/check.go
blob: 59f0132b355545b656ae97dc602b639824f8bc19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package utils

import (
	"fmt"
	"time"
)

// CheckUntil regularly 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 {
	timeoutCh := time.After(timeout)

	for {
		select {
		case <-time.After(interval):
			predTrue, err := predicate()
			if predTrue {
				return nil
			}

			if err != nil {
				return err
			}
		case <-timeoutCh:
			return fmt.Errorf("timeout of %ds reached", int64(timeout/time.Second))
		}
	}
}