fix: context-aware sleep in TCP and HTTP probe retry loops
- commit
- 9faaf179d80de282e73ff1c3cd86474252dff3fd
- author
- brice · · 2026-03-04 08:59 UTC
- parent
- 935f0d1
- signature
- unsigned
1 file · +24 -2
M internal/usecase/container/readiness.go +24 -2
| @@ -46,9 +46,20 @@ func tcpProbe(ctx context.Context, addr string, timeout time.Duration) error { | |||
| 46 | 46 | log.Debug().Err(err).Str("addr", addr).Int("attempt", attempts).Msg("TCP probe attempt failed") | |
| 47 | 47 | } | |
| 48 | 48 | ||
| 49 | + | sleepInterval := 500 * time.Millisecond | |
| 50 | + | if deadline, ok := ctx.Deadline(); ok { | |
| 51 | + | if rem := time.Until(deadline); rem < sleepInterval { | |
| 52 | + | sleepInterval = rem | |
| 53 | + | } | |
| 54 | + | } | |
| 55 | + | if sleepInterval <= 0 { | |
| 56 | + | return ctx.Err() | |
| 57 | + | } | |
| 58 | + | t := time.NewTimer(sleepInterval) | |
| 49 | 59 | select { | |
| 50 | - | case <-time.After(500 * time.Millisecond): | |
| 60 | + | case <-t.C: | |
| 51 | 61 | case <-ctx.Done(): | |
| 62 | + | t.Stop() | |
| 52 | 63 | return ctx.Err() | |
| 53 | 64 | } | |
| 54 | 65 | } | |
| @@ -95,9 +106,20 @@ func httpProbe(ctx context.Context, url string, timeout time.Duration) error { | |||
| 95 | 106 | } | |
| 96 | 107 | } | |
| 97 | 108 | ||
| 109 | + | sleepInterval := time.Second | |
| 110 | + | if deadline, ok := ctx.Deadline(); ok { | |
| 111 | + | if rem := time.Until(deadline); rem < sleepInterval { | |
| 112 | + | sleepInterval = rem | |
| 113 | + | } | |
| 114 | + | } | |
| 115 | + | if sleepInterval <= 0 { | |
| 116 | + | return ctx.Err() | |
| 117 | + | } | |
| 118 | + | t := time.NewTimer(sleepInterval) | |
| 98 | 119 | select { | |
| 99 | - | case <-time.After(time.Second): | |
| 120 | + | case <-t.C: | |
| 100 | 121 | case <-ctx.Done(): | |
| 122 | + | t.Stop() | |
| 101 | 123 | return ctx.Err() | |
| 102 | 124 | } | |
| 103 | 125 | } | |
| @@ -46,9 +46,20 @@ func tcpProbe(ctx context.Context, addr string, timeout time.Duration) error { | |||||
| 46 | log.Debug().Err(err).Str("addr", addr).Int("attempt", attempts).Msg("TCP probe attempt failed") | 46 | log.Debug().Err(err).Str("addr", addr).Int("attempt", attempts).Msg("TCP probe attempt failed") | ||
| 47 | } | 47 | } | ||
| 48 | 48 | ||||
| 49 | + | sleepInterval := 500 * time.Millisecond | |||
| 50 | + | if deadline, ok := ctx.Deadline(); ok { | |||
| 51 | + | if rem := time.Until(deadline); rem < sleepInterval { | |||
| 52 | + | sleepInterval = rem | |||
| 53 | + | } | |||
| 54 | + | } | |||
| 55 | + | if sleepInterval <= 0 { | |||
| 56 | + | return ctx.Err() | |||
| 57 | + | } | |||
| 58 | + | t := time.NewTimer(sleepInterval) | |||
| 49 | select { | 59 | select { | ||
| 50 | - | case <-time.After(500 * time.Millisecond): | 60 | + | case <-t.C: |
| 51 | case <-ctx.Done(): | 61 | case <-ctx.Done(): | ||
| 62 | + | t.Stop() | |||
| 52 | return ctx.Err() | 63 | return ctx.Err() | ||
| 53 | } | 64 | } | ||
| 54 | } | 65 | } | ||
| @@ -95,9 +106,20 @@ func httpProbe(ctx context.Context, url string, timeout time.Duration) error { | |||||
| 95 | } | 106 | } | ||
| 96 | } | 107 | } | ||
| 97 | 108 | ||||
| 109 | + | sleepInterval := time.Second | |||
| 110 | + | if deadline, ok := ctx.Deadline(); ok { | |||
| 111 | + | if rem := time.Until(deadline); rem < sleepInterval { | |||
| 112 | + | sleepInterval = rem | |||
| 113 | + | } | |||
| 114 | + | } | |||
| 115 | + | if sleepInterval <= 0 { | |||
| 116 | + | return ctx.Err() | |||
| 117 | + | } | |||
| 118 | + | t := time.NewTimer(sleepInterval) | |||
| 98 | select { | 119 | select { | ||
| 99 | - | case <-time.After(time.Second): | 120 | + | case <-t.C: |
| 100 | case <-ctx.Done(): | 121 | case <-ctx.Done(): | ||
| 122 | + | t.Stop() | |||
| 101 | return ctx.Err() | 123 | return ctx.Err() | ||
| 102 | } | 124 | } | ||
| 103 | } | 125 | } | ||