forked from tidwall/evio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shiny_kqueue.go
294 lines (283 loc) · 6.74 KB
/
shiny_kqueue.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//+build darwin freebsd
package shiny
import (
"fmt"
"net"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"time"
"unsafe"
)
type conn struct {
fd int
id int
addr string
err error
}
func eventServe(net_, addr string,
handle func(id int, data []byte, ctx interface{}) (send []byte, keepopen bool),
accept func(id int, addr string, wake func(), ctx interface{}) (send []byte, keepopen bool),
closed func(id int, err error, ctx interface{}),
ticker func(ctx interface{}) (keepopen bool),
ctx interface{}) error {
lna, err := net.Listen(net_, addr)
if err != nil {
return err
}
defer lna.Close()
ln := lna.(*net.TCPListener)
f, err := ln.File()
if err != nil {
return err
}
defer f.Close()
ln.Close()
sfd := int(f.Fd())
q, err := syscall.Kqueue()
if err != nil {
return err
}
defer syscall.Close(q)
ev := []syscall.Kevent_t{{
Ident: uint64(sfd),
Flags: syscall.EV_ADD,
Filter: syscall.EVFILT_READ,
}}
if _, err := syscall.Kevent(q, ev, nil, nil); err != nil {
return err
}
var conns = make(map[int]*conn)
defer func() {
for _, conn := range conns {
syscall.Close(conn.fd)
closed(conn.id, conn.err, ctx)
}
}()
var lastwriteorwake time.Time
write := func(nfd int, send []byte) (err error) {
res1, res2, errn := syscall.Syscall(syscall.SYS_WRITE, uintptr(nfd),
uintptr(unsafe.Pointer(&send[0])), uintptr(len(send)))
if errn != 0 {
_, _ = res1, res2
if errn == syscall.EAGAIN {
// This should not happen because we are running the
// server in blocking mode and withoug socket timeouts.
panic("EAGAIN")
}
return errn
}
lastwriteorwake = time.Now()
return nil
}
// add wake event
var wakemu sync.Mutex
var wakeable = true
var wakers = make(map[int]int) // FD->ID map
var wakersarr []int
if accept != nil {
defer func() {
wakemu.Lock()
wakeable = false
wakemu.Unlock()
}()
ev = []syscall.Kevent_t{{
Ident: 0,
Flags: syscall.EV_ADD,
Filter: syscall.EVFILT_USER,
}}
if _, err := syscall.Kevent(q, ev, nil, nil); err != nil {
return err
}
}
shandle := func(c *conn, data []byte) {
send, keepalive := handle(c.id, data, ctx)
if len(send) > 0 {
if err := write(c.fd, send); err != nil {
c.err = err
syscall.Close(c.fd)
return
}
}
if !keepalive {
syscall.Close(c.fd)
}
}
var lastts time.Time
if ticker != nil {
if !ticker(ctx) {
syscall.Close(q)
return nil
}
lastts = time.Now()
}
var id int
var packet [65535]byte
var evs [32]syscall.Kevent_t
for {
var ts syscall.Timespec
if time.Since(lastwriteorwake) < time.Second {
ts = syscall.Timespec{Sec: 0, Nsec: int64(time.Second / 20)}
} else {
ts = syscall.Timespec{Sec: 1, Nsec: 0}
}
n, err := syscall.Kevent(q, nil, evs[:], &ts)
if err != nil {
if err == syscall.EINTR {
continue
}
break
}
for i := 0; ; i++ {
now := time.Now()
if now.Sub(lastts) >= time.Second/20 {
if !ticker(ctx) {
syscall.Close(q)
break
}
lastts = now
}
if i >= n {
break
}
if evs[i].Flags&syscall.EV_EOF != 0 {
c := conns[int(evs[i].Ident)]
syscall.Close(int(evs[i].Ident))
delete(conns, int(evs[i].Ident))
if c != nil {
closed(c.id, c.err, ctx)
}
} else if evs[i].Ident == uint64(sfd) {
nfd, sa, err := syscall.Accept(sfd)
if err != nil {
continue
}
var addr string
var port int
switch sa := sa.(type) {
default:
case *syscall.SockaddrInet4:
addr = net.IP(sa.Addr[:]).String()
port = sa.Port
case *syscall.SockaddrInet6:
addr = net.IP(sa.Addr[:]).String()
port = sa.Port
}
var res []byte
if strings.Contains(addr, ":") {
res = append(append(append(res, '['), addr...), ']', ':')
} else {
res = append(append(res, addr...), ':')
}
addr = string(strconv.AppendInt(res, int64(port), 10))
id++
var send []byte
if accept != nil {
wake := func(nfd, id int) func() {
return func() {
// NOTE: This is the one and only entrypoint that is
// not thread-safe. Use a mutex.
wakemu.Lock()
ev := []syscall.Kevent_t{{
Ident: 0,
Flags: syscall.EV_ENABLE,
Filter: syscall.EVFILT_USER,
Fflags: syscall.NOTE_TRIGGER,
}}
if wakeable {
wakers[nfd] = id
syscall.Kevent(q, ev, nil, nil)
}
wakemu.Unlock()
}
}(nfd, id)
var keepalive bool
send, keepalive = accept(id, addr, wake, ctx)
if !keepalive {
syscall.Close(nfd)
continue
}
}
// 500 second keepalive
var kerr1, kerr2, kerr3 error
if runtime.GOOS == "darwin" {
kerr1 = syscall.SetsockoptInt(nfd, syscall.SOL_SOCKET, 0x8, 1)
kerr2 = syscall.SetsockoptInt(nfd, syscall.IPPROTO_TCP, 0x101, 500)
kerr3 = syscall.SetsockoptInt(nfd, syscall.IPPROTO_TCP, 0x10, 500)
} else {
// freebsd
kerr1 = syscall.SetsockoptInt(nfd, syscall.SOL_SOCKET, 0x8, 1)
kerr2 = syscall.SetsockoptInt(nfd, syscall.IPPROTO_TCP, 0x200, 500)
kerr3 = syscall.SetsockoptInt(nfd, syscall.IPPROTO_TCP, 0x100, 500)
}
if kerr1 != nil || kerr2 != nil || kerr3 != nil {
fmt.Printf("%v %v %v\n", kerr1, kerr2, kerr3)
}
// add read
ev := []syscall.Kevent_t{{
Ident: uint64(nfd),
Flags: syscall.EV_ADD,
Filter: syscall.EVFILT_READ,
}}
if _, err := syscall.Kevent(q, ev, nil, nil); err != nil {
syscall.Close(nfd)
continue
}
c := &conn{fd: nfd, addr: addr, id: id}
conns[nfd] = c
if len(send) > 0 {
if err := write(c.fd, send); err != nil {
c.err = err
syscall.Close(c.fd)
continue
}
}
} else if evs[i].Ident == 0 {
// NOTE: This is a wakeup call. Use a mutex when accessing
// the `wakers` field.
wakersarr = wakersarr[:0]
wakemu.Lock()
for nfd, id := range wakers {
wakersarr = append(wakersarr, nfd, id)
}
wakers = make(map[int]int)
ev := []syscall.Kevent_t{{
Ident: 0,
Flags: syscall.EV_DISABLE,
Filter: syscall.EVFILT_USER,
Fflags: syscall.NOTE_TRIGGER,
}}
_, err := syscall.Kevent(q, ev, nil, nil)
wakemu.Unlock()
// exit the lock and read from the array
if err != nil {
return err
}
for i := 0; i < len(wakersarr); i += 2 {
nfd := wakersarr[i]
id := wakersarr[i+1]
c := conns[nfd]
if c != nil && c.id == id {
shandle(c, nil)
}
}
lastwriteorwake = time.Now()
} else {
c := conns[int(evs[i].Ident)]
res, _, errn := syscall.Syscall(syscall.SYS_READ, uintptr(c.fd),
uintptr(unsafe.Pointer(&packet[0])), uintptr(len(packet)))
if errn != 0 || res == 0 {
if errn != 0 {
c.err = errn
}
syscall.Close(c.fd)
continue
}
shandle(c, packet[:res])
}
}
}
return nil
}