-
Notifications
You must be signed in to change notification settings - Fork 38
/
waiter.go
53 lines (43 loc) · 1.09 KB
/
waiter.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package utils
import (
"errors"
"time"
"github.com/ucloud/ucloud-sdk-go/ucloud/log"
)
// Waiter to wait sth until it completed.
type Waiter interface {
WaitForCompletion() error
Cancel() error
}
// FuncWaiter used for waiting any condition function.
type FuncWaiter struct {
Interval time.Duration
MaxAttempts int
Checker func() (bool, error)
IgnoreError bool
cancel chan struct{}
}
// WaitForCompletion will wait until the state of consdition is available.
// It will call the condition function to ensure state with interval.
func (w *FuncWaiter) WaitForCompletion() error {
for i := 0; ; i++ {
log.Infof("Waiting for completion ... attempted %v times, %v total", i, w.MaxAttempts)
if i >= w.MaxAttempts {
return errors.New("maximum attempts are reached")
}
if ok, err := w.Checker(); ok || (!w.IgnoreError && err != nil) {
return err
}
select {
case <-time.After(w.Interval):
continue
case <-w.cancel:
break
}
}
}
// Cancel will stop all of WaitForCompletion function call.
func (w *FuncWaiter) Cancel() error {
w.cancel <- struct{}{}
return nil
}