forked from vmihailenco/taskq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
166 lines (137 loc) · 3.12 KB
/
handler.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package taskq
import (
"bytes"
"context"
"errors"
"fmt"
"reflect"
"github.com/vmihailenco/msgpack/v5"
)
var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
var messageType = reflect.TypeOf((*Message)(nil))
var errorType = reflect.TypeOf((*error)(nil)).Elem()
// Handler is an interface for processing messages.
type Handler interface {
HandleMessage(msg *Message) error
}
type HandlerFunc func(*Message) error
func (fn HandlerFunc) HandleMessage(msg *Message) error {
return fn(msg)
}
type reflectFunc struct {
fv reflect.Value // Kind() == reflect.Func
ft reflect.Type
acceptsContext bool
returnsError bool
}
var _ Handler = (*reflectFunc)(nil)
func NewHandler(fn interface{}) Handler {
if fn == nil {
panic(errors.New("taskq: handler func is nil"))
}
if h, ok := fn.(Handler); ok {
return h
}
h := reflectFunc{
fv: reflect.ValueOf(fn),
}
h.ft = h.fv.Type()
if h.ft.Kind() != reflect.Func {
panic(fmt.Sprintf("taskq: got %s, wanted %s", h.ft.Kind(), reflect.Func))
}
h.returnsError = returnsError(h.ft)
if acceptsMessage(h.ft) {
if h.returnsError {
return HandlerFunc(fn.(func(*Message) error))
}
if h.ft.NumOut() == 0 {
theFn := fn.(func(*Message))
return HandlerFunc(func(msg *Message) error {
theFn(msg)
return nil
})
}
}
h.acceptsContext = acceptsContext(h.ft)
return &h
}
func (h *reflectFunc) HandleMessage(msg *Message) error {
in, err := h.fnArgs(msg)
if err != nil {
return err
}
out := h.fv.Call(in)
if h.returnsError {
errv := out[h.ft.NumOut()-1]
if !errv.IsNil() {
return errv.Interface().(error)
}
}
return nil
}
func (h *reflectFunc) fnArgs(msg *Message) ([]reflect.Value, error) {
in := make([]reflect.Value, h.ft.NumIn())
inSaved := in
var inStart int
if h.acceptsContext {
inStart = 1
in[0] = reflect.ValueOf(msg.Ctx)
in = in[1:]
}
if len(msg.Args) == len(in) {
var hasWrongType bool
for i, arg := range msg.Args {
v := reflect.ValueOf(arg)
inType := h.ft.In(inStart + i)
if inType.Kind() == reflect.Interface {
if !v.Type().Implements(inType) {
hasWrongType = true
break
}
} else if v.Type() != inType {
hasWrongType = true
break
}
in[i] = v
}
if !hasWrongType {
return inSaved, nil
}
}
b, err := msg.MarshalArgs()
if err != nil {
return nil, err
}
dec := msgpack.NewDecoder(bytes.NewBuffer(b))
n, err := dec.DecodeArrayLen()
if err != nil {
return nil, err
}
if n == -1 {
n = 0
}
if n != len(in) {
return nil, fmt.Errorf("taskq: got %d args, wanted %d", n, len(in))
}
for i := 0; i < len(in); i++ {
arg := reflect.New(h.ft.In(inStart + i)).Elem()
err = dec.DecodeValue(arg)
if err != nil {
err = fmt.Errorf(
"taskq: decoding arg=%d failed (data=%.100x): %s", i, b, err)
return nil, err
}
in[i] = arg
}
return inSaved, nil
}
func acceptsMessage(typ reflect.Type) bool {
return typ.NumIn() == 1 && typ.In(0) == messageType
}
func acceptsContext(typ reflect.Type) bool {
return typ.NumIn() > 0 && typ.In(0).Implements(contextType)
}
func returnsError(typ reflect.Type) bool {
n := typ.NumOut()
return n > 0 && typ.Out(n-1) == errorType
}