-
Notifications
You must be signed in to change notification settings - Fork 36
/
middleware.go
49 lines (39 loc) · 1.36 KB
/
middleware.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
package workers
// JobFunc is a message processor
type JobFunc func(message *Msg) error
// MiddlewareFunc is an extra function on the processing pipeline
type MiddlewareFunc func(queue string, m *Manager, next JobFunc) JobFunc
// Middlewares contains the lists of all configured middleware functions
type Middlewares []MiddlewareFunc
// Append adds middleware to the end of the processing pipeline
func (m Middlewares) Append(mid MiddlewareFunc) Middlewares {
return append(m, mid)
}
// Prepend adds middleware to the front of the processing pipeline
func (m Middlewares) Prepend(mid MiddlewareFunc) Middlewares {
return append(Middlewares{mid}, m...)
}
func (m Middlewares) build(queue string, mgr *Manager, final JobFunc) JobFunc {
for i := len(m) - 1; i >= 0; i-- {
final = m[i](queue, mgr, final)
}
return final
}
// NewMiddlewares creates the processing pipeline given the list of middleware funcs
func NewMiddlewares(mids ...MiddlewareFunc) Middlewares {
return Middlewares(mids)
}
// This is a variable for testing reasons
var defaultMiddlewares = NewMiddlewares(
LogMiddleware,
RetryMiddleware,
StatsMiddleware,
)
// DefaultMiddlewares creates the default middleware pipeline
func DefaultMiddlewares() Middlewares {
return defaultMiddlewares
}
// NopMiddleware does nothing
func NopMiddleware(queue string, mgr *Manager, final JobFunc) JobFunc {
return final
}