From 6c8e735d04ab66cef7da7fe7e0844dd462401047 Mon Sep 17 00:00:00 2001 From: eleksir Date: Mon, 30 Mar 2026 16:15:31 +0300 Subject: [PATCH] Add retry logic for Redis publish failures - Retry up to 3 times with 1 second delay on publish failure --- internal/misc/msgParser.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/internal/misc/msgParser.go b/internal/misc/msgParser.go index 0ef19d3..80a36bc 100644 --- a/internal/misc/msgParser.go +++ b/internal/misc/msgParser.go @@ -4,10 +4,16 @@ import ( "context" "encoding/json" "strings" + "time" "aleesa-misc-go/internal/log" ) +const ( + retryAttempts = 3 + retryDelay = time.Second +) + // MsgParser горутинка, которая парсит json-чики прилетевшие из REDIS-ки. func MsgParser(ctx context.Context, msg string) { var ( @@ -228,12 +234,26 @@ func MsgParser(ctx context.Context, msg string) { return } - // Заталкиваем наш json в редиску. - if err := RedisClient.Publish(ctx, sendTo, data).Err(); err != nil { - log.Warnf("Unable to send data to redis channel %s: %s", sendTo, err) - } else { - log.Debugf("Send msg to redis channel %s: %s", sendTo, string(data)) + // Заталкиваем наш json в редиску с повторными попытками. + var publishErr error + + for attempt := 1; attempt <= retryAttempts; attempt++ { + if publishErr = RedisClient.Publish(ctx, sendTo, data).Err(); publishErr == nil { + log.Debugf("Send msg to redis channel %s: %s", sendTo, string(data)) + + return + } + + log.Warnf("Unable to send data to redis channel %s (attempt %d/%d): %s", + sendTo, attempt, retryAttempts, publishErr) + + if attempt < retryAttempts { + time.Sleep(retryDelay) + } } + + log.Errorf("Failed to send data to redis channel %s after %d attempts: %s", + sendTo, retryAttempts, publishErr) } /* vim: set ft=go noet ai ts=4 sw=4 sts=4: */