forked from hibiken/asynq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretrymux.go
46 lines (38 loc) · 1.12 KB
/
retrymux.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
package asynq
import (
"time"
)
type RetryMux struct {
taskMatcher *TaskMatcher[RetryDelayHandler]
defHandler func() RetryDelayHandler
}
func NewRetryMux(defaultRetry RetryDelayHandler) *RetryMux {
def := noRetryHandler
if !IsNil(defaultRetry) {
def = func() RetryDelayHandler { return defaultRetry }
}
return &RetryMux{
taskMatcher: NewTaskMatcher[RetryDelayHandler](),
defHandler: def,
}
}
func (mux *RetryMux) RetryDelay(n int, e error, task *Task) time.Duration {
h, _ := mux.Handler(task)
return h.RetryDelay(n, e, task)
}
func (mux *RetryMux) Handler(t *Task) (h RetryDelayHandler, pattern string) {
return mux.taskMatcher.Handler(t, mux.defHandler)
}
func (mux *RetryMux) Handle(pattern string, handler RetryDelayHandler) {
mux.taskMatcher.Handle(pattern, handler)
}
func (mux *RetryMux) HandleFunc(pattern string, handler func(n int, e error, task *Task) time.Duration) {
if handler == nil {
panic("asynq: nil retry handler")
}
mux.Handle(pattern, RetryDelayFunc(handler))
}
// noRetryHandler returns the default retry delay func.
func noRetryHandler() RetryDelayHandler {
return DefaultRetryDelay
}