-
Notifications
You must be signed in to change notification settings - Fork 21
/
delay.go
52 lines (41 loc) · 1.14 KB
/
delay.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
40
41
42
43
44
45
46
47
48
49
50
51
52
package nats
import (
"time"
)
// TermSignal if this duration was returned, event will be term`ed
const TermSignal = time.Duration(-1)
type Delay interface {
// WaitTime return time.Duration that we need to wait.
// retryNum is how many times WaitTime was called for
// specific message
WaitTime(retryNum uint64) time.Duration
}
var _ Delay = (*StaticDelay)(nil)
// StaticDelay delay that always return the same time.Duration
type StaticDelay struct {
Delay time.Duration
}
func NewStaticDelay(delay time.Duration) StaticDelay {
return StaticDelay{Delay: delay}
}
func (s StaticDelay) WaitTime(retryNum uint64) time.Duration {
return s.Delay
}
var _ Delay = (*MaxRetryDelay)(nil)
// MaxRetryDelay delay that returns the same time.Duration up to a maximum before sending term
type MaxRetryDelay struct {
StaticDelay
maxRetries uint64
}
func NewMaxRetryDelay(delay time.Duration, retryLimit uint64) MaxRetryDelay {
return MaxRetryDelay{
StaticDelay: NewStaticDelay(delay),
maxRetries: retryLimit,
}
}
func (s MaxRetryDelay) WaitTime(retryNum uint64) time.Duration {
if retryNum >= s.maxRetries {
return TermSignal
}
return s.Delay
}