forked from jrallison/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 10
/
middleware_retry.go
128 lines (107 loc) · 2.77 KB
/
middleware_retry.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package workers
import (
"encoding/json"
"fmt"
"math"
"math/rand"
"time"
)
const (
DEFAULT_MAX_RETRY = 25
LAYOUT = "2006-01-02 15:04:05 MST"
)
type MiddlewareRetry struct{}
func (r *MiddlewareRetry) Call(queue string, message *Msg, next func() bool) (acknowledge bool) {
defer func() {
if e := recover(); e != nil {
conn := Config.Pool.Get()
defer conn.Close()
if retry(message) {
message.Set("queue", queue)
message.Set("error_message", fmt.Sprintf("%v", e))
retryCount := incrementRetry(message)
retryOptions, _ := message.Get("retry_options").Map()
waitDuration := durationToSecondsWithNanoPrecision(
time.Duration(
secondsToDelay(retryCount, retryOptions),
) * time.Second,
)
_, err := conn.Do(
"zadd",
Config.Namespace+RETRY_KEY,
nowToSecondsWithNanoPrecision()+waitDuration,
message.ToJson(),
)
// If we can't add the job to the retry queue,
// then we shouldn't acknowledge the job, otherwise
// it'll disappear into the void.
if err != nil {
acknowledge = false
}
}
panic(e)
}
}()
acknowledge = next()
return
}
func retry(message *Msg) bool {
retry := false
max := DEFAULT_MAX_RETRY
if param, err := message.Get("retry").Bool(); err == nil {
retry = param
} else if param, err := message.Get("retry").Int(); err == nil { // compatible with sidekiq
max = param
retry = true
}
if param, err := message.Get("retry_max").Int(); err == nil {
max = param
}
count, _ := message.Get("retry_count").Int()
return retry && count < max
}
func incrementRetry(message *Msg) (retryCount int) {
retryCount = 0
if count, err := message.Get("retry_count").Int(); err != nil {
message.Set("failed_at", time.Now().UTC().Format(LAYOUT))
} else {
message.Set("retried_at", time.Now().UTC().Format(LAYOUT))
retryCount = count + 1
}
message.Set("retry_count", retryCount)
return
}
func secondsToDelay(count int, retryOptions map[string]interface{}) int {
exp := float64(4)
minDelay := float64(15)
maxDelay := math.Inf(1)
maxRand := float64(30)
if retryOptions != nil {
if v, ok := retryOptions["exp"].(json.Number); ok {
if v2, err := v.Float64(); err == nil {
exp = v2
}
}
if v, ok := retryOptions["min_delay"].(json.Number); ok {
if v2, err := v.Float64(); err == nil {
minDelay = v2
}
}
if v, ok := retryOptions["max_delay"].(json.Number); ok {
if v2, err := v.Float64(); err == nil {
maxDelay = v2
}
}
if v, ok := retryOptions["max_rand"].(json.Number); ok {
if v2, err := v.Float64(); err == nil {
maxRand = v2
}
}
}
power := math.Pow(float64(count), exp)
randN := 0
if maxRand > 0 {
randN = rand.Intn(int(maxRand))
}
return int(math.Min(power+minDelay+float64(randN*(count+1)), maxDelay))
}