-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
39 lines (32 loc) · 980 Bytes
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package rmq
import (
"context"
"log/slog"
"time"
"github.com/danlock/rmq/internal"
)
// Args contains common options shared by danlock/rmq classes.
type Args struct {
// AMQPTimeout sets a timeout on AMQP operations. Defaults to 1 minute.
AMQPTimeout time.Duration
// Delay returns the delay between retry attempts. Defaults to FibonacciDelay.
Delay func(attempt int) time.Duration
// Log can be left nil, set with slog.Log or wrapped around your favorite logging library
Log func(ctx context.Context, level slog.Level, msg string, args ...any)
}
func (cfg *Args) setDefaults() {
if cfg.AMQPTimeout == 0 {
cfg.AMQPTimeout = time.Minute
}
if cfg.Delay == nil {
cfg.Delay = FibonacciDelay
}
internal.WrapLogFunc(&cfg.Log)
}
func FibonacciDelay(attempt int) time.Duration {
if attempt < len(internal.FibonacciDurations) {
return internal.FibonacciDurations[attempt]
} else {
return internal.FibonacciDurations[len(internal.FibonacciDurations)-1]
}
}