Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/resource: restore retval of resource.Retry on timeout #5460

Merged
merged 1 commit into from
Mar 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions helper/resource/wait.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resource

import (
"sync"
"time"
)

Expand All @@ -10,6 +11,11 @@ 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 {
// 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

c := &StateChangeConf{
Pending: []string{"error"},
Target: []string{"success"},
Expand All @@ -21,17 +27,25 @@ func Retry(timeout time.Duration, f RetryFunc) error {
return 42, "success", nil
}

resultErrMu.Lock()
defer resultErrMu.Unlock()
resultErr = err
if rerr, ok := err.(RetryError); ok {
err = rerr.Err
return nil, "quit", err
resultErr = rerr.Err
return nil, "quit", rerr.Err
}

return 42, "error", nil
},
}

_, err := c.WaitForState()
return err
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
}

// RetryError, if returned, will quit the retry immediately with the
Expand Down