-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #606 from weaveworks/waiters-pkg
Refactor waiter helpers into a separate package
- Loading branch information
Showing
2 changed files
with
126 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package waiters | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/kris-nova/logger" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Wait for something with a name to reach status that is expressed by acceptors using newRequest | ||
// until we hit waitTimeout, on unexpected status troubleshoot will be called with the desired | ||
// status as an argument, so that it can find what migth have gone wrong | ||
func Wait(name, msg string, acceptors []request.WaiterAcceptor, newRequest func() *request.Request, waitTimeout time.Duration, troubleshoot func(string)) error { | ||
desiredStatus := fmt.Sprintf("%v", acceptors[0].Expected) | ||
msg = fmt.Sprintf("%s to reach %q status", msg, desiredStatus) | ||
name = strings.Join([]string{"wait", name, desiredStatus}, "_") | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), waitTimeout) | ||
defer cancel() | ||
startTime := time.Now() | ||
w := makeWaiter(ctx, name, msg, acceptors, newRequest) | ||
logger.Debug("start %s", msg) | ||
if waitErr := w.WaitWithContext(ctx); waitErr != nil { | ||
if troubleshoot != nil { | ||
troubleshoot(desiredStatus) | ||
} | ||
return errors.Wrap(waitErr, msg) | ||
} | ||
logger.Debug("done after %s of %s", time.Since(startTime), msg) | ||
return nil | ||
} | ||
|
||
func makeWaiter(ctx context.Context, name, msg string, acceptors []request.WaiterAcceptor, newRequest func() *request.Request) request.Waiter { | ||
return request.Waiter{ | ||
Name: name, | ||
MaxAttempts: 1024, // we use context deadline instead | ||
Delay: makeWaiterDelay(), | ||
Acceptors: acceptors, | ||
NewRequest: func(_ []request.Option) (*request.Request, error) { | ||
logger.Debug(msg) | ||
req := newRequest() | ||
req.SetContext(ctx) | ||
return req, nil | ||
}, | ||
} | ||
} | ||
|
||
// MakeAcceptors constructs a slice of request acceptors | ||
func MakeAcceptors(statusPath string, successStatus string, failureStates []string, extraAcceptors ...request.WaiterAcceptor) []request.WaiterAcceptor { | ||
acceptors := []request.WaiterAcceptor{makeStatusAcceptor(successStatus, statusPath)} | ||
acceptors[0].State = request.SuccessWaiterState | ||
|
||
for _, s := range failureStates { | ||
acceptors = append(acceptors, makeStatusAcceptor(s, statusPath)) | ||
} | ||
|
||
acceptors = append(acceptors, extraAcceptors...) | ||
|
||
return acceptors | ||
} | ||
|
||
func makeStatusAcceptor(status string, statusPath string) request.WaiterAcceptor { | ||
return request.WaiterAcceptor{ | ||
Matcher: request.PathAllWaiterMatch, | ||
Argument: statusPath, | ||
Expected: status, | ||
State: request.FailureWaiterState, | ||
} | ||
} | ||
|
||
// makeWaiterDelay returns delay ranging between 15s and 20s | ||
func makeWaiterDelay() request.WaiterDelay { | ||
const ( | ||
base = 15 * time.Second | ||
offsetSteps = 200 | ||
offsetMax = 5000 | ||
stepMult = offsetMax / offsetSteps | ||
) | ||
|
||
offsets := rand.Perm(offsetSteps) | ||
|
||
return func(attempt int) time.Duration { | ||
s := rand.Intn(offsetSteps) | ||
d := stepMult * offsets[s] | ||
|
||
offset := time.Duration(d) * time.Millisecond | ||
|
||
return base + offset | ||
} | ||
} |