-
Notifications
You must be signed in to change notification settings - Fork 230
/
middleware.go
67 lines (53 loc) · 1.56 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package manager
import (
"context"
"github.com/contribsys/faktory/client"
)
type MiddlewareFunc func(ctx context.Context, next func() error) error
type MiddlewareChain []MiddlewareFunc
type helperKey string
const (
MiddlewareHelperKey helperKey = "_mh"
)
type Context interface {
Job() *client.Job
Reservation() *Reservation
Manager() Manager
}
type Ctx struct {
job *client.Job
mgr *manager
res *Reservation
}
func (c Ctx) Reservation() *Reservation {
return c.res
}
func (c Ctx) Job() *client.Job {
return c.job
}
func (c Ctx) Manager() Manager {
return c.mgr
}
// Returning a Halt error in a middleware will stop the middleware execution
// chain. The server will return the Halt to the client. You can use "ERR"
// for the code to signal an unexpected error or use a well-defined code for
// an error case which the client might be interested in, e.g. "NOTUNIQUE".
func Halt(code string, msg string) error {
return ExpectedError(code, msg)
}
// Middleware can use this to restart the fetch process. Useful if the job
// fetched from Redis is invalid and should be discarded rather than returned
// to the worker.
func Discard(msg string) error {
return ExpectedError("DISCARD", msg)
}
// Run the given job through the given middleware chain.
// `final` is the function called if the entire chain passes the job along.
func callMiddleware(ctx context.Context, chain MiddlewareChain, final func() error) error {
if len(chain) == 0 {
return final()
}
link := chain[0]
rest := chain[1:]
return link(ctx, func() error { return callMiddleware(ctx, rest, final) })
}