Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion commons-test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $(GOBIN)/mockery:
$(call go_install,github.com/vektra/mockery/v2@v2.53.4)

$(GOBIN)/gci:
$(call go_install,github.com/daixiang0/gci@v0.13.5)
$(call go_install,github.com/daixiang0/gci@v0.14.0)

.PHONY: install
install: $(GOBIN)/golangci-lint $(GOBIN)/gotestsum $(GOBIN)/mockery
Expand Down
12 changes: 12 additions & 0 deletions docs/features/wait/all.md
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-->
17 changes: 17 additions & 0 deletions docs/features/wait/any.md
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-->
3 changes: 2 additions & 1 deletion docs/features/wait/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ Below you can find a list of the available wait strategies that you can use:
- [HostPort](./host_port.md)
- [HTTP](./http.md)
- [Log](./log.md)
- [Multi](./multi.md)
- [SQL](./sql.md)
- [TLS](./tls.md)
- [ForAll](./all.md)
- [ForAny](./any.md)

## Startup timeout and Poll interval

Expand Down
25 changes: 0 additions & 25 deletions docs/features/wait/multi.md

This file was deleted.

3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ nav:
- HostPort: features/wait/host_port.md
- HTTP: features/wait/http.md
- Log: features/wait/log.md
- Multi: features/wait/multi.md
- SQL: features/wait/sql.md
- TLS: features/wait/tls.md
- Walk: features/wait/walk.md
- All: features/wait/all.md
- Any: features/wait/any.md
- features/files_and_mounts.md
- features/follow_logs.md
- features/garbage_collector.md
Expand Down
128 changes: 128 additions & 0 deletions wait/any.go
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
}
Comment thread
jeanbza marked this conversation as resolved.

// 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)"
}
Comment thread
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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
case <-ctx.Done():
return fmt.Errorf("timed out waiting for strategies: %w", ctx.Err())
}
}
}
Loading
Loading