forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
eventloop_unix.go
467 lines (414 loc) · 12 KB
/
eventloop_unix.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
// Copyright (c) 2019 The Gnet Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd
package gnet
import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
"golang.org/x/sys/unix"
gio "github.com/panjf2000/gnet/v2/internal/io"
"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/queue"
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
)
type eventloop struct {
listeners map[int]*listener // listeners
idx int // loop index in the engine loops list
engine *engine // engine in loop
poller *netpoll.Poller // epoll or kqueue
buffer []byte // read packet buffer whose capacity is set by user, default value is 64KB
connections connMatrix // loop connections storage
eventHandler EventHandler // user eventHandler
}
func (el *eventloop) getLogger() logging.Logger {
return el.engine.opts.Logger
}
func (el *eventloop) countConn() int32 {
return el.connections.loadCount()
}
func (el *eventloop) closeConns() {
// Close loops and all outstanding connections
el.connections.iterate(func(c *conn) bool {
_ = el.close(c, nil)
return true
})
}
type connWithCallback struct {
c *conn
cb func()
}
func (el *eventloop) register(a any) error {
c, ok := a.(*conn)
if !ok {
ccb := a.(*connWithCallback)
c = ccb.c
defer ccb.cb()
}
return el.register0(c)
}
func (el *eventloop) register0(c *conn) error {
addEvents := el.poller.AddRead
if el.engine.opts.EdgeTriggeredIO {
addEvents = el.poller.AddReadWrite
}
if err := addEvents(&c.pollAttachment, el.engine.opts.EdgeTriggeredIO); err != nil {
_ = unix.Close(c.fd)
c.release()
return err
}
el.connections.addConn(c, el.idx)
if c.isDatagram && c.remote != nil {
return nil
}
return el.open(c)
}
func (el *eventloop) open(c *conn) error {
c.opened = true
out, action := el.eventHandler.OnOpen(c)
if out != nil {
if err := c.open(out); err != nil {
return err
}
}
if !c.outboundBuffer.IsEmpty() && !el.engine.opts.EdgeTriggeredIO {
if err := el.poller.ModReadWrite(&c.pollAttachment, false); err != nil {
return err
}
}
return el.handleAction(c, action)
}
func (el *eventloop) readTLS(c *conn) error {
// Todo: align this method with func (el *eventloop) read(c *conn) error
// Since the el.Buffer may contain multiple TLS record,
// we process one TLS record in each iteration until no more
// TLS records are available
for {
if err := c.tlsconn.ReadFrame(); err != nil {
// Receive error unix.EAGAIN, wait for the next round
if err == unix.EAGAIN {
c.tlsconn.DataDone()
return nil
}
// If err is io.EOF, it can either the data is drained,
// receives a close notify from the client.
return el.close(c, os.NewSyscallError("TLS read", err))
}
// load all decrypted data and make it ready for gnet to use
c.buffer = c.tlsconn.Data()
action := el.eventHandler.OnTraffic(c)
switch action {
case None:
case Close:
// tls data will be cleaned up in el.closeConn()
return el.close(c, nil)
case Shutdown:
c.tlsconn.DataDone()
return errorx.ErrEngineShutdown
}
_, _ = c.inboundBuffer.Write(c.buffer)
c.buffer = c.buffer[:0]
// all available TLS records are processed
if !c.tlsconn.IsRecordCompleted(c.tlsconn.RawInputData()) {
c.tlsconn.DataDone()
return nil
}
}
}
func (el *eventloop) read0(a any) error {
return el.read(a.(*conn))
}
func (el *eventloop) read(c *conn) error {
if !c.opened {
return nil
}
// detected whether kernel TLS RX is enabled
// This only happens after TLS handshake is completed.
// Therefore, no need to call c.tlsconn.HandshakeComplete().
if c.tlsconn != nil && c.tlsconn.IsKTLSRXEnabled() {
// attach the gnet eventloop.buffer to tlsconn.rawInput.
// So, KTLS can decrypt the data directly to the buffer without memory allocation.
// Since data is read through KTLS, there is no need to call unix.read(c.fd, el.buffer)
c.tlsconn.RawInputSet(el.buffer) //nolint:errcheck
return el.readTLS(c)
}
var recv int
isET := el.engine.opts.EdgeTriggeredIO
chunk := el.engine.opts.EdgeTriggeredIOChunk
loop:
n, err := unix.Read(c.fd, el.buffer)
if err != nil || n == 0 {
if err == unix.EAGAIN {
return nil
}
if n == 0 {
err = io.EOF
}
return el.close(c, os.NewSyscallError("read", err))
}
recv += n
if c.tlsconn != nil {
// attach the gnet eventloop.buffer to tlsconn.rawInput.
c.tlsconn.RawInputSet(el.buffer[:n]) //nolint:errcheck
if !c.tlsconn.HandshakeComplete() {
// check whether the buffer data is sufficient for a complete TLS record
data := c.tlsconn.RawInputData()
if !c.tlsconn.IsRecordCompleted(data) {
c.tlsconn.DataDone()
return nil
}
if err = c.tlsconn.Handshake(); err != nil {
// close will cleanup the TLS data at the end,
// so no need to call tlsconn.DataDone()
return el.close(c, os.NewSyscallError("TLS handshake", err))
}
if !c.tlsconn.HandshakeComplete() || len(c.tlsconn.RawInputData()) == 0 { // 握手没成功,或者握手成功,但是没有数据黏包了
c.tlsconn.DataDone()
return nil
}
}
return el.readTLS(c)
}
c.buffer = el.buffer[:n]
action := el.eventHandler.OnTraffic(c)
switch action {
case None:
case Close:
return el.close(c, nil)
case Shutdown:
return errorx.ErrEngineShutdown
}
_, _ = c.inboundBuffer.Write(c.buffer)
c.buffer = c.buffer[:0]
if c.isEOF || (isET && recv < chunk) {
goto loop
}
// To prevent infinite reading in ET mode and starving other events,
// we need to set up threshold for the maximum read bytes per connection
// on each event-loop. If the threshold is reached and there are still
// unread data in the socket buffer, we must issue another read event manually.
if isET && n == len(el.buffer) {
return el.poller.Trigger(queue.LowPriority, el.read0, c)
}
return nil
}
func (el *eventloop) write0(a any) error {
return el.write(a.(*conn))
}
// The default value of UIO_MAXIOV/IOV_MAX is 1024 on Linux and most BSD-like OSs.
const iovMax = 1024
func (el *eventloop) write(c *conn) error {
if c.outboundBuffer.IsEmpty() {
return nil
}
isET := el.engine.opts.EdgeTriggeredIO
chunk := el.engine.opts.EdgeTriggeredIOChunk
var (
n int
sent int
err error
)
loop:
iov, _ := c.outboundBuffer.Peek(-1)
if len(iov) > 1 {
if len(iov) > iovMax {
iov = iov[:iovMax]
}
n, err = gio.Writev(c.fd, iov)
} else {
n, err = unix.Write(c.fd, iov[0])
}
_, _ = c.outboundBuffer.Discard(n)
switch err {
case nil:
case unix.EAGAIN:
return nil
default:
return el.close(c, os.NewSyscallError("write", err))
}
sent += n
if isET && !c.outboundBuffer.IsEmpty() && sent < chunk {
goto loop
}
// All data have been sent, it's no need to monitor the writable events for LT mode,
// remove the writable event from poller to help the future event-loops if necessary.
if !isET && c.outboundBuffer.IsEmpty() {
return el.poller.ModRead(&c.pollAttachment, false)
}
// To prevent infinite writing in ET mode and starving other events,
// we need to set up threshold for the maximum write bytes per connection
// on each event-loop. If the threshold is reached and there are still
// pending data to write, we must issue another write event manually.
if isET && !c.outboundBuffer.IsEmpty() {
return el.poller.Trigger(queue.HighPriority, el.write0, c)
}
return nil
}
func (el *eventloop) close(c *conn, err error) error {
if !c.opened || el.connections.getConn(c.fd) == nil {
return nil // ignore stale connections
}
el.connections.delConn(c)
action := el.eventHandler.OnClose(c, err)
// close the TLS connection by sending the alert
if c.tlsconn != nil {
// close the TLS connection, which will send a close notify to the client
c.tlsconn.Close()
// Make sure all memory requested from the pool is returned.
c.tlsconn.DataCleanUpAfterClose()
c.tlsconn = nil
// TODO: create a sync.pool to manage the TLS connection
}
// Send residual data in buffer back to the remote before actually closing the connection.
for !c.outboundBuffer.IsEmpty() {
iov, _ := c.outboundBuffer.Peek(0)
if len(iov) > iovMax {
iov = iov[:iovMax]
}
if n, e := gio.Writev(c.fd, iov); e != nil {
break
} else { //nolint:revive
_, _ = c.outboundBuffer.Discard(n)
}
}
c.release()
var errStr strings.Builder
err0, err1 := el.poller.Delete(c.fd), unix.Close(c.fd)
if err0 != nil {
err0 = fmt.Errorf("failed to delete fd=%d from poller in event-loop(%d): %v",
c.fd, el.idx, os.NewSyscallError("delete", err0))
errStr.WriteString(err0.Error())
errStr.WriteString(" | ")
}
if err1 != nil {
err1 = fmt.Errorf("failed to close fd=%d in event-loop(%d): %v",
c.fd, el.idx, os.NewSyscallError("close", err1))
errStr.WriteString(err1.Error())
}
if errStr.Len() > 0 {
return errors.New(strings.TrimSuffix(errStr.String(), " | "))
}
return el.handleAction(c, action)
}
func (el *eventloop) wake(c *conn) error {
if !c.opened || el.connections.getConn(c.fd) == nil {
return nil // ignore stale connections
}
action := el.eventHandler.OnTraffic(c)
return el.handleAction(c, action)
}
func (el *eventloop) ticker(ctx context.Context) {
var (
action Action
delay time.Duration
timer *time.Timer
)
defer func() {
if timer != nil {
timer.Stop()
}
}()
for {
delay, action = el.eventHandler.OnTick()
switch action {
case None, Close:
case Shutdown:
// It seems reasonable to mark this as low-priority, waiting for some tasks like asynchronous writes
// to finish up before shutting down the service.
err := el.poller.Trigger(queue.LowPriority, func(_ any) error { return errorx.ErrEngineShutdown }, nil)
el.getLogger().Debugf("failed to enqueue shutdown signal of high-priority for event-loop(%d): %v", el.idx, err)
}
if timer == nil {
timer = time.NewTimer(delay)
} else {
timer.Reset(delay)
}
select {
case <-ctx.Done():
el.getLogger().Debugf("stopping ticker in event-loop(%d) from Engine, error:%v", el.idx, ctx.Err())
return
case <-timer.C:
}
}
}
func (el *eventloop) readUDP(fd int, _ netpoll.IOEvent, _ netpoll.IOFlags) error {
n, sa, err := unix.Recvfrom(fd, el.buffer, 0)
if err != nil {
if err == unix.EAGAIN {
return nil
}
return fmt.Errorf("failed to read UDP packet from fd=%d in event-loop(%d), %v",
fd, el.idx, os.NewSyscallError("recvfrom", err))
}
var c *conn
if ln, ok := el.listeners[fd]; ok {
c = newUDPConn(fd, el, ln.addr, sa, false)
} else {
c = el.connections.getConn(fd)
}
c.buffer = el.buffer[:n]
action := el.eventHandler.OnTraffic(c)
if c.remote != nil {
c.release()
}
if action == Shutdown {
return errorx.ErrEngineShutdown
}
return nil
}
func (el *eventloop) handleAction(c *conn, action Action) error {
switch action {
case None:
return nil
case Close:
return el.close(c, nil)
case Shutdown:
return errorx.ErrEngineShutdown
default:
return nil
}
}
/*
func (el *eventloop) execCmd(a any) (err error) {
cmd := a.(*asyncCmd)
c := el.connections.getConnByGFD(cmd.fd)
if c == nil || c.gfd != cmd.fd {
return errorx.ErrInvalidConn
}
defer func() {
if cmd.cb != nil {
_ = cmd.cb(c, err)
}
}()
switch cmd.typ {
case asyncCmdClose:
return el.close(c, nil)
case asyncCmdWake:
return el.wake(c)
case asyncCmdWrite:
_, err = c.Write(cmd.param.([]byte))
case asyncCmdWritev:
_, err = c.Writev(cmd.param.([][]byte))
default:
return errorx.ErrUnsupportedOp
}
return
}
*/