refactor(outbox): share domain backoff policy
This commit is contained in:
@@ -148,8 +148,8 @@ func (p *OutboxProcessor) handleFailure(ctx context.Context, event *OutboxEvent,
|
||||
}
|
||||
} else {
|
||||
// 计算下次重试时间(指数退避)
|
||||
backoffSeconds := calculateBackoff(event.RetryCount, event.MaxRetries)
|
||||
nextRetry := time.Now().Add(time.Duration(backoffSeconds) * time.Second)
|
||||
backoffSeconds := CalculateOutboxBackoff(event.RetryCount, event.MaxRetries)
|
||||
nextRetry := time.Now().Add(time.Duration(backoffSeconds) * time.Second)
|
||||
|
||||
// 在存储层更新重试状态(这里简化处理)
|
||||
if err := p.eventStore.MarkFailed(ctx, event.EventID, publishErr.Error()); err != nil {
|
||||
@@ -162,8 +162,8 @@ func (p *OutboxProcessor) handleFailure(ctx context.Context, event *OutboxEvent,
|
||||
}
|
||||
}
|
||||
|
||||
// calculateBackoff 计算指数退避时间
|
||||
func calculateBackoff(retryCount, maxRetries int) int {
|
||||
// CalculateOutboxBackoff 计算指数退避时间
|
||||
func CalculateOutboxBackoff(retryCount, maxRetries int) int {
|
||||
backoff := DefaultInitialBackoffSeconds * int(math.Pow(2, float64(retryCount-1)))
|
||||
if backoff > DefaultMaxBackoffSeconds {
|
||||
backoff = DefaultMaxBackoffSeconds
|
||||
|
||||
@@ -56,7 +56,7 @@ func (m *mockOutboxEventStore) MarkFailed(ctx context.Context, eventID string, e
|
||||
if e, ok := m.events[eventID]; ok {
|
||||
e.Status = OutboxStatusFailed
|
||||
e.ErrorMessage = errorMsg
|
||||
backoff := calculateBackoff(e.RetryCount, e.MaxRetries)
|
||||
backoff := CalculateOutboxBackoff(e.RetryCount, e.MaxRetries)
|
||||
nextRetry := time.Now().Add(time.Duration(backoff) * time.Second)
|
||||
e.NextRetryAt = &nextRetry
|
||||
m.failed = append(m.failed, e)
|
||||
@@ -269,7 +269,7 @@ func TestP006_ExponentialBackoff(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
backoff := calculateBackoff(tt.retryCount, tt.maxRetries)
|
||||
backoff := CalculateOutboxBackoff(tt.retryCount, tt.maxRetries)
|
||||
if backoff < tt.expectedMin || backoff > tt.expectedMax {
|
||||
t.Errorf("retry %d: expected backoff %d-%d, got %d",
|
||||
tt.retryCount, tt.expectedMin, tt.expectedMax, backoff)
|
||||
@@ -280,7 +280,7 @@ func TestP006_ExponentialBackoff(t *testing.T) {
|
||||
// TestP006_MaxBackoffCap 验证退避时间上限
|
||||
func TestP006_MaxBackoffCap(t *testing.T) {
|
||||
// 即使重试很多次,退避时间也不应超过60秒
|
||||
backoff := calculateBackoff(100, 100)
|
||||
backoff := CalculateOutboxBackoff(100, 100)
|
||||
if backoff > DefaultMaxBackoffSeconds {
|
||||
t.Errorf("backoff should be capped at %d, got %d", DefaultMaxBackoffSeconds, backoff)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user