60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package confirmnotice
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// WorkerOptions controls MQ-driven confirm IM delivery retries.
|
|
type WorkerOptions struct {
|
|
WorkerID string
|
|
BatchSize int
|
|
LockTTL time.Duration
|
|
PublishTimeout time.Duration
|
|
MaxRetryCount int
|
|
InitialBackoff time.Duration
|
|
MaxBackoff time.Duration
|
|
}
|
|
|
|
func normalizeWorkerOptions(options WorkerOptions, nodeID string) WorkerOptions {
|
|
if strings.TrimSpace(options.WorkerID) == "" {
|
|
options.WorkerID = strings.TrimSpace(nodeID)
|
|
}
|
|
if options.WorkerID == "" {
|
|
options.WorkerID = "notice-confirm-worker"
|
|
}
|
|
if options.BatchSize <= 0 {
|
|
options.BatchSize = 100
|
|
}
|
|
if options.LockTTL <= 0 {
|
|
options.LockTTL = 30 * time.Second
|
|
}
|
|
if options.PublishTimeout <= 0 {
|
|
options.PublishTimeout = 3 * time.Second
|
|
}
|
|
if options.MaxRetryCount <= 0 {
|
|
options.MaxRetryCount = 10
|
|
}
|
|
if options.InitialBackoff <= 0 {
|
|
options.InitialBackoff = 5 * time.Second
|
|
}
|
|
if options.MaxBackoff <= 0 {
|
|
options.MaxBackoff = 5 * time.Minute
|
|
}
|
|
return options
|
|
}
|
|
|
|
func backoff(retryCount int, options WorkerOptions) time.Duration {
|
|
if retryCount <= 0 {
|
|
return options.InitialBackoff
|
|
}
|
|
delay := options.InitialBackoff
|
|
for i := 1; i < retryCount; i++ {
|
|
delay *= 2
|
|
if delay >= options.MaxBackoff {
|
|
return options.MaxBackoff
|
|
}
|
|
}
|
|
return delay
|
|
}
|