fix: non-blocking loader with retry backoff, 30s timeout
This commit is contained in:
@@ -54,7 +54,7 @@ func NewLoader(registry *Registry, namespace string) (*Loader, error) {
|
||||
token: string(tokenBytes),
|
||||
baseURL: "https://kubernetes.default.svc",
|
||||
client: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
Timeout: 30 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{RootCAs: pool},
|
||||
},
|
||||
@@ -63,19 +63,22 @@ func NewLoader(registry *Registry, namespace string) (*Loader, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Start loads ServiceAdapters immediately, then polls every interval.
|
||||
func (l *Loader) Start(interval time.Duration) error {
|
||||
if err := l.load(); err != nil {
|
||||
// Retry once after 2s — handles transient "storage reinitializing" (429)
|
||||
log.Printf("serviceadapter loader: first attempt failed (%v), retrying in 2s", err)
|
||||
time.Sleep(2 * time.Second)
|
||||
if err := l.load(); err != nil {
|
||||
return fmt.Errorf("serviceadapter loader: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Background poll for changes
|
||||
// Start begins loading ServiceAdapters in the background. Non-blocking.
|
||||
// Retries on failure so the gateway can start serving immediately.
|
||||
func (l *Loader) Start(interval time.Duration) {
|
||||
go func() {
|
||||
// Initial load with retries (API server may be slow on startup)
|
||||
for attempt := 1; ; attempt++ {
|
||||
if err := l.load(); err != nil {
|
||||
backoff := time.Duration(min(attempt*5, 30)) * time.Second
|
||||
log.Printf("serviceadapter loader: attempt %d failed (%v), retry in %s", attempt, err, backoff)
|
||||
time.Sleep(backoff)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// Poll for changes
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
@@ -89,8 +92,6 @@ func (l *Loader) Start(interval time.Duration) error {
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop stops the background poll.
|
||||
|
||||
Reference in New Issue
Block a user