Add retry logic for Redis publish failures
- Retry up to 3 times with 1 second delay on publish failure
This commit is contained in:
@@ -4,10 +4,16 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"aleesa-misc-go/internal/log"
|
"aleesa-misc-go/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
retryAttempts = 3
|
||||||
|
retryDelay = time.Second
|
||||||
|
)
|
||||||
|
|
||||||
// MsgParser горутинка, которая парсит json-чики прилетевшие из REDIS-ки.
|
// MsgParser горутинка, которая парсит json-чики прилетевшие из REDIS-ки.
|
||||||
func MsgParser(ctx context.Context, msg string) {
|
func MsgParser(ctx context.Context, msg string) {
|
||||||
var (
|
var (
|
||||||
@@ -228,12 +234,26 @@ func MsgParser(ctx context.Context, msg string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Заталкиваем наш json в редиску.
|
// Заталкиваем наш json в редиску с повторными попытками.
|
||||||
if err := RedisClient.Publish(ctx, sendTo, data).Err(); err != nil {
|
var publishErr error
|
||||||
log.Warnf("Unable to send data to redis channel %s: %s", sendTo, err)
|
|
||||||
} else {
|
for attempt := 1; attempt <= retryAttempts; attempt++ {
|
||||||
log.Debugf("Send msg to redis channel %s: %s", sendTo, string(data))
|
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: */
|
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */
|
||||||
|
|||||||
Reference in New Issue
Block a user