-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhandler.go
198 lines (166 loc) · 3.83 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package sio
import (
"fmt"
"reflect"
"time"
"github.com/karagenc/socket.io-go/internal/sync"
)
type eventHandler struct {
rv reflect.Value
inputArgs []reflect.Type
}
func newEventHandler(f any) (*eventHandler, error) {
rv := reflect.ValueOf(f)
rt := rv.Type()
if f == nil || rv.Kind() != reflect.Func {
return nil, fmt.Errorf("sio: function expected")
}
inputArgs := make([]reflect.Type, rt.NumIn())
for i := range inputArgs {
inputArgs[i] = rt.In(i)
}
if rt.NumOut() > 0 {
return nil, fmt.Errorf("sio: an event handler cannot have return values")
}
s := &eventHandler{
rv: rv,
inputArgs: inputArgs,
}
_, err := s.ack()
return s, err
}
func (s *eventHandler) ack() (ok bool, err error) {
if len(s.inputArgs) > 0 && s.inputArgs[len(s.inputArgs)-1].Kind() == reflect.Func {
if s.inputArgs[len(s.inputArgs)-1].NumOut() > 0 {
err = fmt.Errorf("sio: an acknowledgement function of an event handler cannot have return values")
return
}
ok = true
}
return
}
func (f *eventHandler) call(args ...reflect.Value) (ret []reflect.Value, err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
err = fmt.Errorf("sio: handler error: %v", r)
}
}
}()
ret = f.rv.Call(args)
return
}
type ackHandler struct {
rv reflect.Value
inputArgs []reflect.Type
hasError bool
called bool
timedOut bool
mu sync.Mutex
}
func newAckHandler(f any, hasError bool) (*ackHandler, error) {
rv := reflect.ValueOf(f)
rt := rv.Type()
if f == nil || rv.Kind() != reflect.Func {
return nil, fmt.Errorf("sio: function expected")
}
inputArgs := make([]reflect.Type, rt.NumIn())
for i := range inputArgs {
inputArgs[i] = rt.In(i)
}
err := checkAckFunc(f, hasError)
if err != nil {
return nil, err
}
return &ackHandler{
rv: rv,
inputArgs: inputArgs,
hasError: hasError,
}, nil
}
func newAckHandlerWithTimeout(f any, timeout time.Duration, timeoutFunc func()) (*ackHandler, error) {
h, err := newAckHandler(f, true)
if err != nil {
return nil, err
}
go func() {
time.Sleep(timeout)
h.mu.Lock()
if h.called {
h.mu.Unlock()
return
}
h.timedOut = true
h.mu.Unlock()
defer func() {
_ = recover()
}()
timeoutFunc()
args := make([]reflect.Value, len(h.inputArgs))
args[0] = reflect.ValueOf(ErrAckTimeout)
for i := 1; i < len(args); i++ {
args[i] = reflect.New(h.inputArgs[i]).Elem()
}
h.rv.Call(args)
}()
return h, nil
}
func (f *ackHandler) call(args ...reflect.Value) (err error) {
f.mu.Lock()
if f.timedOut {
f.mu.Unlock()
return nil
}
f.called = true
f.mu.Unlock()
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
err = fmt.Errorf("sio: ack handler error: %v", r)
}
}
}()
if f.hasError {
var (
nilError error = nil
pointerToNilError interface{} = &nilError
)
args = append([]reflect.Value{reflect.ValueOf(pointerToNilError).Elem()}, args...)
}
f.rv.Call(args)
return
}
func dismantleAckFunc(rt reflect.Type) (in []reflect.Type, variadic bool) {
in = make([]reflect.Type, rt.NumIn())
for i := range in {
in[i] = rt.In(i)
}
variadic = rt.IsVariadic()
return
}
var (
_emptyError error
reflectError = reflect.TypeOf(&_emptyError).Elem()
)
func checkAckFunc(f any, mustHaveError bool) error {
rt := reflect.TypeOf(f)
if f == nil || rt.Kind() != reflect.Func {
return fmt.Errorf("sio: function expected")
}
if rt.NumOut() != 0 {
return fmt.Errorf("sio: ack handler must not have a return value")
}
if mustHaveError {
if rt.NumIn() == 0 {
return fmt.Errorf("sio: ack handler must have error as its 1st parameter")
}
if rt.In(0).Kind() != reflect.Interface || !rt.In(0).Implements(reflectError) {
return fmt.Errorf("sio: ack handler must have error as its 1st parameter")
}
}
return nil
}