-
Notifications
You must be signed in to change notification settings - Fork 157
/
poller_epoll.go
514 lines (460 loc) · 9.7 KB
/
poller_epoll.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Copyright 2020 lesismal. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//go:build linux
// +build linux
package nbio
import (
"errors"
"fmt"
"io"
"net"
"os"
"runtime"
"syscall"
"time"
"unsafe"
"github.com/lesismal/nbio/logging"
)
const (
// EPOLLLT .
EPOLLLT = 0
// EPOLLET .
EPOLLET = 0x80000000
// EPOLLONESHOT .
EPOLLONESHOT = syscall.EPOLLONESHOT
)
const (
epollEventsRead = syscall.EPOLLPRI | syscall.EPOLLIN
epollEventsWrite = syscall.EPOLLOUT
epollEventsError = syscall.EPOLLERR | syscall.EPOLLHUP | syscall.EPOLLRDHUP
)
const (
IPPROTO_TCP = syscall.IPPROTO_TCP
TCP_KEEPINTVL = syscall.TCP_KEEPINTVL
TCP_KEEPIDLE = syscall.TCP_KEEPIDLE
)
type poller struct {
g *Engine // parent engine
epfd int // epoll fd
evtfd int // event fd for trigger
index int // poller index in engine
pollType string // listener or io poller
shutdown bool // state
// whether poller is used for listener.
isListener bool
// listener.
listener net.Listener
// if poller is used as UnixConn listener,
// store the addr and remove it when exit.
unixSockAddr string
ReadBuffer []byte // default reading buffer
}
// add the connection to poller and handle its io events.
//
//go:norace
func (p *poller) addConn(c *Conn) error {
fd := c.fd
if fd >= len(p.g.connsUnix) {
err := fmt.Errorf("too many open files, fd[%d] >= MaxOpenFiles[%d]",
fd,
len(p.g.connsUnix),
)
c.closeWithError(err)
return err
}
c.p = p
if c.typ != ConnTypeUDPServer {
p.g.onOpen(c)
} else {
p.g.onUDPListen(c)
}
p.g.connsUnix[fd] = c
err := p.addRead(fd)
if err != nil {
p.g.connsUnix[fd] = nil
c.closeWithError(err)
}
return err
}
// add the connection to poller and handle its io events.
//
//go:norace
func (p *poller) addDialer(c *Conn) error {
fd := c.fd
if fd >= len(p.g.connsUnix) {
err := fmt.Errorf("too many open files, fd[%d] >= MaxOpenFiles[%d]",
fd,
len(p.g.connsUnix),
)
c.closeWithError(err)
return err
}
c.p = p
p.g.connsUnix[fd] = c
c.isWAdded = true
err := p.addReadWrite(fd)
if err != nil {
p.g.connsUnix[fd] = nil
c.closeWithError(err)
}
return err
}
//go:norace
func (p *poller) getConn(fd int) *Conn {
return p.g.connsUnix[fd]
}
//go:norace
func (p *poller) deleteConn(c *Conn) {
if c == nil {
return
}
fd := c.fd
if c.typ != ConnTypeUDPClientFromRead {
if c == p.g.connsUnix[fd] {
p.g.connsUnix[fd] = nil
}
// p.deleteEvent(fd)
}
if c.typ != ConnTypeUDPServer {
p.g.onClose(c, c.closeErr)
}
}
//go:norace
func (p *poller) start() {
defer p.g.Done()
logging.Debug("NBIO[%v][%v_%v] start", p.g.Name, p.pollType, p.index)
defer logging.Debug("NBIO[%v][%v_%v] stopped", p.g.Name, p.pollType, p.index)
if p.isListener {
p.acceptorLoop()
} else {
defer func() {
syscall.Close(p.epfd)
syscall.Close(p.evtfd)
}()
p.readWriteLoop()
}
}
//go:norace
func (p *poller) acceptorLoop() {
if p.g.LockListener {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
}
p.shutdown = false
for !p.shutdown {
conn, err := p.listener.Accept()
if err == nil {
var c *Conn
c, err = NBConn(conn)
if err != nil {
conn.Close()
continue
}
err = p.g.pollers[c.Hash()%len(p.g.pollers)].addConn(c)
if err != nil {
logging.Error("NBIO[%v][%v_%v] addConn [fd: %v] failed: %v",
p.g.Name,
p.pollType,
p.index,
c.fd,
err,
)
}
} else {
var ne net.Error
if ok := errors.As(err, &ne); ok && ne.Timeout() {
logging.Error("NBIO[%v][%v_%v] Accept failed: timeout error, retrying...",
p.g.Name,
p.pollType,
p.index,
)
time.Sleep(time.Second / 20)
} else {
if !p.shutdown {
logging.Error("NBIO[%v][%v_%v] Accept failed: %v, exit...",
p.g.Name,
p.pollType,
p.index,
err,
)
}
break
}
}
}
}
//go:norace
func (p *poller) readWriteLoop() {
if p.g.LockPoller {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
}
msec := -1
events := make([]syscall.EpollEvent, 1024)
if p.g.onRead == nil && p.g.EpollMod == EPOLLET {
p.g.MaxConnReadTimesPerEventLoop = 1<<31 - 1
}
g := p.g
p.shutdown = false
isOneshot := g.isOneshot
asyncReadEnabled := g.AsyncReadInPoller && (g.EpollMod == EPOLLET)
for !p.shutdown {
n, err := syscall.EpollWait(p.epfd, events, msec)
if err != nil && !errors.Is(err, syscall.EINTR) {
logging.Error("NBIO[%v][%v_%v] EpollWait failed: %v, exit...",
p.g.Name,
p.pollType,
p.index,
err,
)
return
}
if n <= 0 {
msec = -1
// runtime.Gosched()
continue
}
msec = 20
for _, ev := range events[:n] {
fd := int(ev.Fd)
switch fd {
case p.evtfd: // triggered by stop, exit event loop
default: // for socket connections
c := p.getConn(fd)
if c != nil {
if ev.Events&epollEventsWrite != 0 {
if c.onConnected == nil {
c.flush()
} else {
c.onConnected(c, nil)
c.onConnected = nil
c.resetRead()
}
}
if ev.Events&epollEventsRead != 0 {
if g.onRead == nil {
if asyncReadEnabled {
c.AsyncRead()
} else {
for i := 0; i < g.MaxConnReadTimesPerEventLoop; i++ {
buffer := g.borrow(c)
rc, n, err := c.ReadAndGetConn(buffer)
if n > 0 {
g.onData(rc, buffer[:n])
}
g.payback(c, buffer)
if errors.Is(err, syscall.EINTR) {
continue
}
if errors.Is(err, syscall.EAGAIN) {
break
}
if err != nil {
c.closeWithError(err)
break
}
if n < len(buffer) {
break
}
}
if isOneshot {
c.ResetPollerEvent()
}
}
} else {
g.onRead(c)
}
}
if ev.Events&epollEventsError != 0 {
c.closeWithError(io.EOF)
continue
}
}
}
}
}
}
//go:norace
func (p *poller) stop() {
logging.Debug("NBIO[%v][%v_%v] stop...", p.g.Name, p.pollType, p.index)
p.shutdown = true
if p.listener != nil {
p.listener.Close()
if p.unixSockAddr != "" {
os.Remove(p.unixSockAddr)
}
} else {
n := uint64(1)
syscall.Write(p.evtfd, (*(*[8]byte)(unsafe.Pointer(&n)))[:])
}
}
//go:norace
func (p *poller) addRead(fd int) error {
return p.setRead(syscall.EPOLL_CTL_ADD, fd)
}
//go:norace
func (p *poller) resetRead(fd int) error {
return p.setRead(syscall.EPOLL_CTL_MOD, fd)
}
//go:norace
func (p *poller) setRead(op int, fd int) error {
switch p.g.EpollMod {
case EPOLLET:
events := syscall.EPOLLERR |
syscall.EPOLLHUP |
syscall.EPOLLRDHUP |
syscall.EPOLLPRI |
syscall.EPOLLIN |
EPOLLET |
p.g.EPOLLONESHOT
if p.g.EPOLLONESHOT != EPOLLONESHOT {
if op == syscall.EPOLL_CTL_ADD {
return syscall.EpollCtl(p.epfd, op, fd, &syscall.EpollEvent{
Fd: int32(fd),
Events: events | syscall.EPOLLOUT,
})
}
return nil
}
return syscall.EpollCtl(p.epfd, op, fd, &syscall.EpollEvent{
Fd: int32(fd),
Events: events,
})
default:
return syscall.EpollCtl(
p.epfd,
op,
fd,
&syscall.EpollEvent{
Fd: int32(fd),
Events: syscall.EPOLLERR |
syscall.EPOLLHUP |
syscall.EPOLLRDHUP |
syscall.EPOLLPRI |
syscall.EPOLLIN,
},
)
}
}
//go:norace
func (p *poller) modWrite(fd int) error {
return p.setReadWrite(syscall.EPOLL_CTL_MOD, fd)
}
//go:norace
func (p *poller) addReadWrite(fd int) error {
return p.setReadWrite(syscall.EPOLL_CTL_ADD, fd)
}
//go:norace
func (p *poller) setReadWrite(op int, fd int) error {
switch p.g.EpollMod {
case EPOLLET:
events := syscall.EPOLLERR |
syscall.EPOLLHUP |
syscall.EPOLLRDHUP |
syscall.EPOLLPRI |
syscall.EPOLLIN |
syscall.EPOLLOUT |
EPOLLET |
p.g.EPOLLONESHOT
if p.g.EPOLLONESHOT != EPOLLONESHOT {
if op == syscall.EPOLL_CTL_ADD {
return syscall.EpollCtl(p.epfd, op, fd, &syscall.EpollEvent{
Fd: int32(fd),
Events: events,
})
}
return nil
}
return syscall.EpollCtl(p.epfd, op, fd, &syscall.EpollEvent{
Fd: int32(fd),
Events: events,
})
default:
return syscall.EpollCtl(
p.epfd, op, fd,
&syscall.EpollEvent{
Fd: int32(fd),
Events: syscall.EPOLLERR |
syscall.EPOLLHUP |
syscall.EPOLLRDHUP |
syscall.EPOLLPRI |
syscall.EPOLLIN |
syscall.EPOLLOUT,
},
)
}
}
// func (p *poller) deleteEvent(fd int) error {
// return syscall.EpollCtl(
// p.epfd,
// syscall.EPOLL_CTL_DEL,
// fd,
// &syscall.EpollEvent{Fd: int32(fd)},
// )
// }
//go:norace
func newPoller(g *Engine, isListener bool, index int) (*poller, error) {
if isListener {
if len(g.Addrs) == 0 {
panic("invalid listener num")
}
addr := g.Addrs[index%len(g.Addrs)]
ln, err := g.Listen(g.Network, addr)
if err != nil {
return nil, err
}
p := &poller{
g: g,
index: index,
listener: ln,
isListener: isListener,
pollType: "LISTENER",
}
if g.Network == "unix" {
p.unixSockAddr = addr
}
return p, nil
}
fd, err := syscall.EpollCreate1(0)
if err != nil {
return nil, err
}
r0, _, e0 := syscall.Syscall(syscall.SYS_EVENTFD2, 0, syscall.O_NONBLOCK, 0)
if e0 != 0 {
syscall.Close(fd)
return nil, e0
}
err = syscall.EpollCtl(fd, syscall.EPOLL_CTL_ADD, int(r0),
&syscall.EpollEvent{Fd: int32(r0),
Events: syscall.EPOLLIN,
},
)
if err != nil {
syscall.Close(fd)
syscall.Close(int(r0))
return nil, err
}
p := &poller{
g: g,
epfd: fd,
evtfd: int(r0),
index: index,
isListener: isListener,
pollType: "POLLER",
}
return p, nil
}
//go:norace
func (c *Conn) ResetPollerEvent() {
p := c.p
g := p.g
fd := c.fd
if g.isOneshot && !c.closed {
if len(c.writeList) == 0 {
p.resetRead(fd)
} else {
p.modWrite(fd)
}
}
}