-
-
Notifications
You must be signed in to change notification settings - Fork 607
feat(wait): implement AnyMultiStrategy: ForAny equivalent to ForAll. #3719
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,12 @@ | ||
| # All Wait strategy | ||
|
|
||
| The All multi wait strategy holds a list of wait strategies. The execution of each strategy is first added, first executed. | ||
|
|
||
| Available Options: | ||
|
|
||
| - `WithDeadline` - the deadline for when all strategies must complete by, default is none. | ||
| - `WithStartupTimeoutDefault` - the startup timeout default to be used for each Strategy if not defined in seconds, default is 60 seconds. | ||
|
|
||
| <!--codeinclude--> | ||
| [ForAll Example](../../../wait/wait_examples_test.go) inside_block:ExampleForAll | ||
| <!--/codeinclude--> |
This file contains hidden or 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,17 @@ | ||
| # Any Wait strategy | ||
|
|
||
| The Any multi wait strategy holds a list of wait strategies. The execution of | ||
| each strategy is asynchronous: all run in their own goroutine. If any one | ||
| succeeds, the Wait will finish with success (no error) and the remaining | ||
| running wait strategies will be cancelled. If any one fails, the Wait will | ||
| finish with an error and the remaining running wait strategies will be | ||
| cancelled. | ||
|
|
||
| Available Options: | ||
|
|
||
| - `WithDeadline` - the deadline for when all strategies must complete by, default is none. | ||
| - `WithStartupTimeoutDefault` - the startup timeout default to be used for each Strategy if not defined in seconds, default is 60 seconds. | ||
|
|
||
| <!--codeinclude--> | ||
| [ForAny Example](../../../wait/wait_examples_test.go) inside_block:ExampleForAny | ||
| <!--/codeinclude--> |
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,128 @@ | ||
| package wait | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "reflect" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // Implement interface | ||
| var ( | ||
| _ Strategy = (*AnyMultiStrategy)(nil) | ||
| _ StrategyTimeout = (*AnyMultiStrategy)(nil) | ||
| ) | ||
|
|
||
| type AnyMultiStrategy struct { | ||
| // all Strategies should have a startupTimeout to avoid waiting infinitely | ||
| timeout *time.Duration | ||
| deadline *time.Duration | ||
|
|
||
| // additional properties | ||
| Strategies []Strategy | ||
| } | ||
|
|
||
| // WithStartupTimeoutDefault sets the default timeout for all inner wait strategies. | ||
| func (ms *AnyMultiStrategy) WithStartupTimeoutDefault(timeout time.Duration) *AnyMultiStrategy { | ||
| ms.timeout = &timeout | ||
| return ms | ||
| } | ||
|
|
||
| // WithDeadline sets a time.Duration which limits all wait strategies. | ||
| func (ms *AnyMultiStrategy) WithDeadline(deadline time.Duration) *AnyMultiStrategy { | ||
| ms.deadline = &deadline | ||
| return ms | ||
| } | ||
|
|
||
| // ForAny returns a WaitStrategy that waits for any of the supplied conditions | ||
| // to become true (after which it cancels the remaining ones). | ||
| // | ||
| // Failures are not permitted: any strategy which fails will have its error | ||
| // immediately returned. | ||
| func ForAny(strategies ...Strategy) *AnyMultiStrategy { | ||
| return &AnyMultiStrategy{ | ||
| Strategies: strategies, | ||
| } | ||
| } | ||
|
|
||
| func (ms *AnyMultiStrategy) Timeout() *time.Duration { | ||
| return ms.timeout | ||
| } | ||
|
|
||
| // String returns a human-readable description of the wait strategy. | ||
| func (ms *AnyMultiStrategy) String() string { | ||
| if len(ms.Strategies) == 0 { | ||
| return "any of: (none)" | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| var strategies []string | ||
| for _, strategy := range ms.Strategies { | ||
| if strategy == nil || reflect.ValueOf(strategy).IsNil() { | ||
| continue | ||
| } | ||
| if s, ok := strategy.(fmt.Stringer); ok { | ||
| strategies = append(strategies, s.String()) | ||
| } else { | ||
| strategies = append(strategies, fmt.Sprintf("%T", strategy)) | ||
| } | ||
| } | ||
|
|
||
| // Always include "any of:" prefix to make it clear this is a AnyMultiStrategy | ||
| // even when there's only one strategy after filtering out nils. | ||
| return "any of: [" + strings.Join(strategies, ", ") + "]" | ||
| } | ||
|
|
||
| func (ms *AnyMultiStrategy) WaitUntilReady(ctx context.Context, target StrategyTarget) error { | ||
| if len(ms.Strategies) == 0 { | ||
| return errors.New("no wait strategy supplied") | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(ctx) | ||
| defer cancel() // All remaining strategies will stop when this fires. | ||
|
|
||
| if ms.deadline != nil { | ||
| ctx, cancel = context.WithTimeout(ctx, *ms.deadline) | ||
| defer cancel() | ||
| } | ||
|
|
||
| resCh := make(chan error, len(ms.Strategies)) | ||
| var valid int | ||
|
|
||
| for _, strategy := range ms.Strategies { | ||
| if strategy == nil || reflect.ValueOf(strategy).IsNil() { | ||
| // A module could be appending strategies after part of the container initialization, | ||
| // and use wait.ForAny on a not initialized strategy. | ||
| // In this case, we just skip the nil strategy. | ||
| continue | ||
| } | ||
| valid++ | ||
|
|
||
| strategyCtx := ctx | ||
| // Set default Timeout when strategy implements StrategyTimeout | ||
| if st, ok := strategy.(StrategyTimeout); ok { | ||
| if ms.Timeout() != nil && st.Timeout() == nil { | ||
| strategyCtx, cancel = context.WithTimeout(ctx, *ms.Timeout()) | ||
| defer cancel() | ||
| } | ||
| } | ||
| go func() { resCh <- strategy.WaitUntilReady(strategyCtx, target) }() | ||
| } | ||
|
|
||
| if valid == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| for { | ||
| select { | ||
| case err := <-resCh: | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| case <-ctx.Done(): | ||
| return fmt.Errorf("timed out waiting for strategies: %w", ctx.Err()) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.