mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-11-25 11:06:36 +00:00
fix(metadata): retry discovery HTTP errors and recover in the background (#169)
A provider behind the same Traefik answers 404 while its router is not loaded, and 502 or 503 while it restarts. GetProviderMetadata returned these statuses as a plain error, so the startup retries stopped at the first attempt. After that, nothing retried until a request arrived, and that request got the 503 page (#147). - GetProviderMetadata returns a non-200 status as *HTTPError, so the startup retries apply to 429 and 5xx. - RetryConfig gets RetryableStatusCodes. MetadataFetchRetryConfig sets it to 404, so only discovery retries a 404. - If startup discovery fails, the init goroutine retries in the background (5 s, doubling up to 60 s, with jitter). It stops when metadata is adopted or when Close cancels the instance context. - GetMetadataWithRecovery takes a context, so Close also ends the startup retries. - Background attempts log at ERROR for the first three and every tenth, at DEBUG otherwise. A document with no usable issuer does not end recovery. A Close during the startup retries logs at DEBUG only. - The 503 served while metadata is unavailable or initialization times out carries Retry-After: 10, and the startup error log names the middleware. TestMetadataRecoveryOnProviderFailure and the issue152 cold-start test now serve 400 for a failed discovery, because startup retries 500 and 503 and these tests need init to fail at once.
This commit is contained in:
+16
-2
@@ -9,6 +9,7 @@ import (
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -476,6 +477,9 @@ func (cb *CircuitBreaker) GetMetrics() map[string]interface{} {
|
||||
type RetryConfig struct {
|
||||
// RetryableErrors defines error patterns that should trigger retries
|
||||
RetryableErrors []string `json:"retryable_errors"`
|
||||
// RetryableStatusCodes lists HTTP statuses retried even though they are
|
||||
// below 500 and not 429
|
||||
RetryableStatusCodes []int `json:"retryable_status_codes"`
|
||||
// MaxAttempts is the maximum number of retry attempts
|
||||
MaxAttempts int `json:"max_attempts"`
|
||||
// InitialDelay is the delay before the first retry
|
||||
@@ -528,6 +532,9 @@ func MetadataFetchRetryConfig() RetryConfig {
|
||||
"x509",
|
||||
"tls",
|
||||
},
|
||||
// Traefik answers 404 for a provider router that is not loaded yet
|
||||
// (IdP behind the same Traefik, #147).
|
||||
RetryableStatusCodes: []int{http.StatusNotFound},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -766,8 +773,15 @@ func (re *RetryExecutor) isRetryableError(err error) bool {
|
||||
// "EOF" (isEOFError), or a generic "timeout" — would be reclassified
|
||||
// retryable and retried to MaxAttempts on a permanent error, repeating
|
||||
// the failing request (R123, R157).
|
||||
if statusHTTP, ok := asHTTPError(err); ok && statusHTTP.StatusCode != 0 && statusHTTP.StatusCode < 500 && statusHTTP.StatusCode != 429 {
|
||||
return false
|
||||
if statusHTTP, ok := asHTTPError(err); ok && statusHTTP.StatusCode != 0 {
|
||||
for _, code := range re.config.RetryableStatusCodes {
|
||||
if statusHTTP.StatusCode == code {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if statusHTTP.StatusCode < 500 && statusHTTP.StatusCode != 429 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Check for Traefik default certificate error (startup race condition)
|
||||
|
||||
Reference in New Issue
Block a user