fix(iot): ретрай GetQueueUrl на старте + выход ≠0 при сбое подсистемы (🔴5)
- Добавлен ResolveQueueURLWithRetry (10 попыток × 2с backoff): временная ошибка GetQueueUrl больше не роняет bridge/consumer без ретрая. - main.go: при ПОСТОЯННОЙ ошибке bridge/consumer процесс завершается с кодом ≠ 0 (fatal-флаг + cancel), чтобы платформа перезапустила контейнер — раньше процесс оставался жив, а /health отдавал 200.
This commit is contained in:
@@ -28,7 +28,7 @@ func Run(ctx context.Context, cfg *config.Config, sqsClient *sqs.Client, log *sl
|
||||
log.Info("bridge: starting", "broker", cfg.MQTTBrokerURL, "client_id", cfg.MQTTClientID)
|
||||
|
||||
// URL очереди получаем один раз при старте (как старый consumer).
|
||||
queueURL, err := sqsclient.ResolveQueueURL(ctx, sqsClient, cfg.SQSQueueName)
|
||||
queueURL, err := sqsclient.ResolveQueueURLWithRetry(ctx, sqsClient, cfg.SQSQueueName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ const maxProcessBackoff = 60 * time.Second
|
||||
|
||||
// Run — бесконечный long-poll цикл потребителя.
|
||||
func Run(ctx context.Context, cfg *config.Config, sqsClient *sqs.Client, store *iotpg.IoTPostgresStore, log *slog.Logger) error {
|
||||
queueURL, err := sqsclient.ResolveQueueURL(ctx, sqsClient, cfg.SQSQueueName)
|
||||
queueURL, err := sqsclient.ResolveQueueURLWithRetry(ctx, sqsClient, cfg.SQSQueueName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ package sqsclient
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
@@ -36,3 +37,29 @@ func ResolveQueueURL(ctx context.Context, client *sqs.Client, queueName string)
|
||||
}
|
||||
return aws.ToString(out.QueueUrl), nil
|
||||
}
|
||||
|
||||
// ResolveQueueURLWithRetry — ResolveQueueURL с ретраями на старте. Временная
|
||||
// ошибка GetQueueUrl (SQS ещё поднимается, сетевой сбой) не должна фатально
|
||||
// ронять bridge/consumer без ретрая (ревью 2026-10-10, 🔴5). После исчерпания
|
||||
// попыток возвращает ошибку — вызывающий должен завершить процесс ≠ 0 (платформа
|
||||
// перезапустит контейнер).
|
||||
func ResolveQueueURLWithRetry(ctx context.Context, client *sqs.Client, queueName string) (string, error) {
|
||||
const attempts = 10
|
||||
const backoff = 2 * time.Second
|
||||
var lastErr error
|
||||
for attempt := 1; attempt <= attempts; attempt++ {
|
||||
url, err := ResolveQueueURL(ctx, client, queueName)
|
||||
if err == nil {
|
||||
return url, nil
|
||||
}
|
||||
lastErr = err
|
||||
if attempt < attempts {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", lastErr
|
||||
case <-time.After(backoff):
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", lastErr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user