2014-07-01 03:03:27 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2016-03-04 18:20:48 +01:00
|
|
|
"sync"
|
2014-07-01 03:03:27 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
// 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 {
|
2016-03-04 18:20:48 +01:00
|
|
|
// These are used to pull the error out of the function; need a mutex to
|
|
|
|
// avoid a data race.
|
|
|
|
var resultErr error
|
|
|
|
var resultErrMu sync.Mutex
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
c := &StateChangeConf{
|
2016-03-09 23:53:32 +01:00
|
|
|
Pending: []string{"retryableerror"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"success"},
|
2014-10-08 06:44:51 +02:00
|
|
|
Timeout: timeout,
|
|
|
|
MinTimeout: 500 * time.Millisecond,
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2016-03-09 23:53:32 +01:00
|
|
|
rerr := f()
|
|
|
|
if rerr == nil {
|
2016-03-10 14:41:42 +01:00
|
|
|
resultErr = nil
|
2014-10-18 03:28:03 +02:00
|
|
|
return 42, "success", nil
|
2014-07-08 00:21:54 +02:00
|
|
|
}
|
|
|
|
|
2016-03-04 18:20:48 +01:00
|
|
|
resultErrMu.Lock()
|
|
|
|
defer resultErrMu.Unlock()
|
2016-03-09 23:53:32 +01:00
|
|
|
resultErr = rerr.Err
|
2014-10-18 03:28:03 +02:00
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
if rerr.Retryable {
|
|
|
|
return 42, "retryableerror", nil
|
|
|
|
}
|
|
|
|
return nil, "quit", rerr.Err
|
2014-10-08 06:44:51 +02:00
|
|
|
},
|
2014-07-01 03:03:27 +02:00
|
|
|
}
|
2014-10-08 06:44:51 +02:00
|
|
|
|
2016-03-04 18:20:48 +01:00
|
|
|
c.WaitForState()
|
|
|
|
|
|
|
|
// Need to acquire the lock here to be able to avoid race using resultErr as
|
|
|
|
// the return value
|
|
|
|
resultErrMu.Lock()
|
|
|
|
defer resultErrMu.Unlock()
|
|
|
|
return resultErr
|
2014-07-01 03:03:27 +02:00
|
|
|
}
|
2014-10-18 03:28:03 +02:00
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
// RetryFunc is the function retried until it succeeds.
|
|
|
|
type RetryFunc func() *RetryError
|
|
|
|
|
|
|
|
// RetryError is the required return type of RetryFunc. It forces client code
|
|
|
|
// to choose whether or not a given error is retryable.
|
2014-10-18 03:28:03 +02:00
|
|
|
type RetryError struct {
|
2016-03-09 23:53:32 +01:00
|
|
|
Err error
|
|
|
|
Retryable bool
|
2014-10-18 03:28:03 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
// RetryableError is a helper to create a RetryError that's retryable from a
|
|
|
|
// given error.
|
|
|
|
func RetryableError(err error) *RetryError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &RetryError{Err: err, Retryable: true}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NonRetryableError is a helper to create a RetryError that's _not)_ retryable
|
|
|
|
// from a given error.
|
|
|
|
func NonRetryableError(err error) *RetryError {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &RetryError{Err: err, Retryable: false}
|
2014-10-18 03:28:03 +02:00
|
|
|
}
|