// RetryWithTargetOccurrences is a basic wrapper around StateChangeConf that will retry
// a function until it returns the specified amount of target occurrences continuously.
// Adapted from the Retry function in the go SDK.
RetryWithTargetOccurrences is a basic wrapper around StateChangeConf that will retry
a function until it returns the specified amount of target occurrences continuously.
Adapted from the Retry function in the go SDK.
// RetryWithTargetOccurrences is a basic wrapper around StateChangeConf that will retry // a function until it returns the specified amount of target occurrences continuously . // Adapted from the Retry function in the go SDK .
func RetryWithTargetOccurrences(timeout time.Duration, targetOccurrences int,
	f resource.RetryFunc) error {
	var resultErr error
	var resultErrMu sync.Mutex
	c := &resource.StateChangeConf{
		Pending:                   []string{"retryableerror"},
		Target:                    []string{"success"},
		Timeout:                   timeout,
		MinTimeout:                500 * time.Millisecond,
		ContinuousTargetOccurence: targetOccurrences,
		Refresh: func() (interface{}, string, error) {
			rerr := f()
			resultErrMu.Lock()
			defer resultErrMu.Unlock()
			if rerr == nil {
				resultErr = nil
				return 42, "success", nil
			}
			resultErr = rerr.Err
			if rerr.Retryable {
				return 42, "retryableerror", nil
			}
			return nil, "quit", rerr.Err
		},
	}
	_, waitErr := c.WaitForState()
	resultErrMu.Lock()
	defer resultErrMu.Unlock()
	if resultErr == nil {
		return waitErr
	}
	return resultErr
}
func RetryWithTargetOccurrences ( timeout time . Duration , targetOccurrences int , f resource . RetryFunc ) error { var resultErr error var resultErrMu sync . Mutex c := & resource . StateChangeConf { Pending : [ ] string { " " } , Target : [ ] string { " " } , Timeout : timeout , MinTimeout : 500 * time . Millisecond , ContinuousTargetOccurence : targetOccurrences , Refresh : func ( ) ( interface { } , string , error ) { rerr := f ( ) resultErrMu . Lock ( ) defer resultErrMu . Unlock ( ) if rerr == nil { resultErr = nil return 42 , " " , nil } resultErr = rerr . Err if rerr . Retryable { return 42 , " " , nil } return nil , " " , rerr . Err } , } _ , waitErr := c . WaitForState ( ) resultErrMu . Lock ( ) defer resultErrMu . Unlock ( ) if resultErr == nil { return waitErr } return resultErr }
RetryWithTargetOccurrences is a basic wrapper around StateChangeConf that will retry
a function until it returns the specified amount of target occurrences continuously.
RetryWithTargetOccurrences is a basic wrapper around StateChangeConf that will retry a function until it returns the specified amount of target occurrences continuously .
// These are used to pull the error out of the function; need a mutex to // avoid a data race. // Need to acquire the lock here to be able to avoid race using resultErr as // the return value // resultErr may be nil because the wait timed out and resultErr was never // set; this is still an error // resultErr takes precedence over waitErr if both are set because it is // more likely to be useful