template-monorepo/internal/model/notification/usecase/backoff.go

33 lines
669 B
Go
Raw Permalink Normal View History

package usecase
import (
"time"
notifconfig "gateway/internal/model/notification/config"
)
const defaultMaxRetry = 5
func effectiveMaxRetry(cfg notifconfig.AsyncConfig) int {
if cfg.MaxRetry > 0 {
return cfg.MaxRetry
}
return defaultMaxRetry
}
// retryDelay returns wait duration before attempt number `nextAttempt` (1-based).
func retryDelay(cfg notifconfig.AsyncConfig, nextAttempt int) time.Duration {
if nextAttempt < 1 {
nextAttempt = 1
}
secs := cfg.BackoffSeconds
if len(secs) == 0 {
secs = []int{1, 5, 30, 300, 1800}
}
idx := nextAttempt - 1
if idx >= len(secs) {
idx = len(secs) - 1
}
return time.Duration(secs[idx]) * time.Second
}