Private
Public Access
1
0

Enhance Redis and NATS connection logic with retry mechanisms, exponential backoff, and improved error handling.
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 2m25s

This commit is contained in:
Sebastian Unterschütz
2026-01-04 15:44:00 +01:00
parent 43680ece16
commit 932edf74f2
2 changed files with 83 additions and 9 deletions

View File

@@ -24,13 +24,37 @@ const leaderboardKey = "leaderboard:top"
func InitLeaderboard(redisAddr string) error {
rdb := redis.NewClient(&redis.Options{
Addr: redisAddr,
DB: 0,
Addr: redisAddr,
DB: 0,
MaxRetries: 5,
MinRetryBackoff: 1 * time.Second,
MaxRetryBackoff: 5 * time.Second,
DialTimeout: 10 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
})
ctx := context.Background()
if err := rdb.Ping(ctx).Err(); err != nil {
return err
// Retry-Logik für initiales Connect
maxRetries := 10
retryDelay := 2 * time.Second
for i := 0; i < maxRetries; i++ {
if err := rdb.Ping(ctx).Err(); err != nil {
log.Printf("⚠️ Redis noch nicht erreichbar (Versuch %d/%d): %v", i+1, maxRetries, err)
if i < maxRetries-1 {
log.Printf("⏳ Warte %v vor erneutem Versuch...", retryDelay)
time.Sleep(retryDelay)
retryDelay = retryDelay * 2 // Exponential backoff
if retryDelay > 30*time.Second {
retryDelay = 30 * time.Second
}
continue
}
return fmt.Errorf("Redis nicht erreichbar nach %d Versuchen: %w", maxRetries, err)
}
break // Erfolgreich verbunden
}
GlobalLeaderboard = &Leaderboard{
@@ -38,7 +62,7 @@ func InitLeaderboard(redisAddr string) error {
ctx: ctx,
}
log.Println("📊 Redis-Leaderboard verbunden")
log.Println(" Redis-Leaderboard verbunden")
return nil
}