Skip to content

Commit c525538

Browse files
committed
Improve retry logic for load balancing: ensure unique backends are tried, add max attempts safety limit
1 parent c265367 commit c525538

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

main.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,32 +1170,46 @@ func (s *ProxyService) HandleProxy(w http.ResponseWriter, r *http.Request) {
11701170
}
11711171

11721172
if retryPolicy == "retry-all" && len(backends) > 1 {
1173-
// Try all backends until success (2xx) or all exhausted
1173+
triedURLs := make(map[string]bool) // Track which backends we've tried
11741174
attemptCount := 0
1175-
for i := 0; i < len(backends); i++ {
1175+
maxAttempts := len(backends) * 2 // Safety limit to prevent infinite loop
1176+
1177+
for attemptCount < maxAttempts {
11761178
rule := lb.Next()
11771179

1178-
success, _, shouldReturn := s.tryBackend(w, r, subdomain, rule, start, host, clientIP, i == len(backends)-1)
1180+
// Skip if already tried this backend
1181+
if triedURLs[rule.ProxyTo] {
1182+
attemptCount++
1183+
continue
1184+
}
1185+
triedURLs[rule.ProxyTo] = true
1186+
1187+
isLastAttempt := len(triedURLs) >= len(backends) // Check if we've tried all unique backends
1188+
success, _, shouldReturn := s.tryBackend(w, r, subdomain, rule, start, host, clientIP, isLastAttempt)
11791189
attemptCount++
11801190

11811191
if success {
11821192
// Track retry outcome - successfully after trying multiple backends
1183-
if attemptCount > 1 {
1193+
if len(triedURLs) > 1 {
11841194
proxyRetryAttemptsTotal.WithLabelValues(subdomain, "success").Inc()
11851195
}
11861196
return
11871197
}
11881198

11891199
if shouldReturn {
11901200
// This was the last attempt and it failed
1191-
if attemptCount > 1 {
1201+
if len(triedURLs) > 1 {
11921202
proxyRetryAttemptsTotal.WithLabelValues(subdomain, "all_failed").Inc()
11931203
}
11941204
return
11951205
}
1206+
11961207
// Continue to the next backend
11971208
}
1198-
// All backends exhausted without response (shouldn't reach here)
1209+
// All unique backends exhausted
1210+
if len(triedURLs) > 1 {
1211+
proxyRetryAttemptsTotal.WithLabelValues(subdomain, "all_failed").Inc()
1212+
}
11991213
return
12001214
}
12011215

0 commit comments

Comments
 (0)