forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.go
46 lines (38 loc) · 916 Bytes
/
wait.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
package resource
import (
"time"
)
// RetryFunc is the function retried until it succeeds.
type RetryFunc func() error
// Retry is a basic wrapper around StateChangeConf that will just retry
// a function until it no longer returns an error.
func Retry(timeout time.Duration, f RetryFunc) error {
var err error
c := &StateChangeConf{
Pending: []string{"error"},
Target: "success",
Timeout: timeout,
MinTimeout: 500 * time.Millisecond,
Refresh: func() (interface{}, string, error) {
err = f()
if err == nil {
return 42, "success", nil
}
if rerr, ok := err.(RetryError); ok {
err = rerr.Err
return nil, "quit", err
}
return 42, "error", nil
},
}
c.WaitForState()
return err
}
// RetryError, if returned, will quit the retry immediately with the
// Err.
type RetryError struct {
Err error
}
func (e RetryError) Error() string {
return e.Err.Error()
}