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
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 2m25s
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user