-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventloop_unix.go
313 lines (274 loc) · 8.03 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
// Copyright (c) 2019 Andy Pan
// Copyright (c) 2018 Joshua J Baker
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//go:build linux || freebsd || dragonfly || darwin
// +build linux freebsd dragonfly darwin
package gnet
import (
"context"
"errors"
"fmt"
"os"
"sync/atomic"
"time"
"unsafe"
"golang.org/x/sys/unix"
gerrors "github.com/liujiangang01/enhanced_gnet/errors"
"github.com/liujiangang01/enhanced_gnet/internal/io"
"github.com/liujiangang01/enhanced_gnet/internal/netpoll"
"github.com/liujiangang01/enhanced_gnet/logging"
)
type eventloop struct {
internalEventloop
// Prevents eventloop from false sharing by padding extra memory with the difference
// between the cache line size "s" and (eventloop mod s) for the most common CPU architectures.
_ [64 - unsafe.Sizeof(internalEventloop{})%64]byte
}
//nolint:structcheck
type internalEventloop struct {
ln *listener // listener
idx int // loop index in the server loops list
svr *server // server in loop
poller *netpoll.Poller // epoll or kqueue
buffer []byte // read packet buffer whose capacity is set by user, default value is 64KB
connCount int32 // number of active connections in event-loop
connections map[int]*conn // loop connections fd -> conn
eventHandler EventHandler // user eventHandler
}
func (el *eventloop) getLogger() logging.Logger {
return el.svr.opts.Logger
}
func (el *eventloop) addConn(delta int32) {
atomic.AddInt32(&el.connCount, delta)
}
func (el *eventloop) loadConn() int32 {
return atomic.LoadInt32(&el.connCount)
}
func (el *eventloop) closeAllConns() {
// Close loops and all outstanding connections
for _, c := range el.connections {
_ = el.loopCloseConn(c, nil)
}
}
func (el *eventloop) loopRegister(itf interface{}) error {
c := itf.(*conn)
if err := el.poller.AddRead(c.pollAttachment); err != nil {
_ = unix.Close(c.fd)
c.releaseTCP()
return nil
}
el.connections[c.fd] = c
return el.loopOpen(c)
}
func (el *eventloop) loopOpen(c *conn) error {
c.opened = true
el.addConn(1)
out, action := el.eventHandler.OnOpened(c)
if out != nil {
if err := c.open(out); err != nil {
return err
}
}
if !c.outboundBuffer.IsEmpty() {
if err := el.poller.AddWrite(c.pollAttachment); err != nil {
return err
}
}
return el.handleAction(c, action)
}
func (el *eventloop) loopRead(c *conn) error {
n, err := unix.Read(c.fd, el.buffer)
if n == 0 || err != nil {
if err == unix.EAGAIN {
return nil
}
return el.loopCloseConn(c, os.NewSyscallError("read", err))
}
c.buffer = el.buffer[:n]
for inFrame, _ := c.read(); inFrame != nil; inFrame, _ = c.read() {
out, action := el.eventHandler.React(inFrame, c)
if out != nil {
// Encode data and try to write it back to the client, this attempt is based on a fact:
// a client socket waits for the response data after sending request data to the server,
// which makes the client socket writable.
if err = c.write(out); err != nil {
return err
}
}
switch action {
case None:
case Close:
return el.loopCloseConn(c, nil)
case Shutdown:
return gerrors.ErrServerShutdown
}
// Check the status of connection every loop since it might be closed during writing data back to client due to
// some kind of system error.
if !c.opened {
return nil
}
}
_, _ = c.inboundBuffer.Write(c.buffer)
return nil
}
func (el *eventloop) loopWrite(c *conn) error {
el.eventHandler.PreWrite(c)
head, tail := c.outboundBuffer.PeekAll()
var (
n int
err error
)
if len(tail) > 0 {
n, err = io.Writev(c.fd, [][]byte{head, tail})
} else {
n, err = unix.Write(c.fd, head)
}
c.outboundBuffer.Discard(n)
switch err {
case nil, gerrors.ErrShortWritev: // do nothing, just go on
case unix.EAGAIN:
return nil
default:
return el.loopCloseConn(c, os.NewSyscallError("write", err))
}
// All data have been drained, it's no need to monitor the writable events,
// remove the writable event from poller to help the future event-loops.
if c.outboundBuffer.IsEmpty() {
_ = el.poller.ModRead(c.pollAttachment)
}
el.eventHandler.AfterWrite(c, nil)
return nil
}
func (el *eventloop) loopCloseConn(c *conn, err error) (rerr error) {
if !c.opened {
return
}
// Send residual data in buffer back to client before actually closing the connection.
if !c.outboundBuffer.IsEmpty() {
el.eventHandler.PreWrite(c)
head, tail := c.outboundBuffer.PeekAll()
if n, err := unix.Write(c.fd, head); err == nil {
if n == len(head) && tail != nil {
_, _ = unix.Write(c.fd, tail)
}
}
el.eventHandler.AfterWrite(c, nil)
}
if err0, err1 := el.poller.Delete(c.fd), unix.Close(c.fd); err0 == nil && err1 == nil {
delete(el.connections, c.fd)
el.addConn(-1)
if el.eventHandler.OnClosed(c, err) == Shutdown {
return gerrors.ErrServerShutdown
}
c.releaseTCP()
} else {
if err0 != nil {
rerr = fmt.Errorf("failed to delete fd=%d from poller in event-loop(%d): %v", c.fd, el.idx, err0)
}
if err1 != nil {
err1 = fmt.Errorf("failed to close fd=%d in event-loop(%d): %v", c.fd, el.idx, os.NewSyscallError("close", err1))
if rerr != nil {
rerr = errors.New(rerr.Error() + " & " + err1.Error())
} else {
rerr = err1
}
}
}
return
}
func (el *eventloop) loopWake(c *conn) error {
if co, ok := el.connections[c.fd]; !ok || co != c {
return nil // ignore stale wakes.
}
out, action := el.eventHandler.React(nil, c)
if out != nil {
if err := c.write(out); err != nil {
return err
}
}
return el.handleAction(c, action)
}
func (el *eventloop) loopTicker(ctx context.Context) {
if el == nil {
return
}
var (
action Action
delay time.Duration
timer *time.Timer
)
defer func() {
if timer != nil {
timer.Stop()
}
}()
for {
delay, action = el.eventHandler.Tick()
switch action {
case None:
case Shutdown:
err := el.poller.UrgentTrigger(func(_ interface{}) error { return gerrors.ErrServerShutdown }, nil)
el.getLogger().Debugf("stopping ticker in event-loop(%d) from Tick(), UrgentTrigger:%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 Server, error:%v", el.idx, ctx.Err())
return
case <-timer.C:
}
}
}
func (el *eventloop) handleAction(c *conn, action Action) error {
switch action {
case None:
return nil
case Close:
return el.loopCloseConn(c, nil)
case Shutdown:
return gerrors.ErrServerShutdown
default:
return nil
}
}
func (el *eventloop) loopReadUDP(fd int) error {
n, sa, err := unix.Recvfrom(fd, el.buffer, 0)
if err != nil {
if err == unix.EAGAIN || err == unix.EWOULDBLOCK {
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))
}
c := newUDPConn(fd, el, sa)
out, action := el.eventHandler.React(el.buffer[:n], c)
if out != nil {
_ = c.sendTo(out)
}
if action == Shutdown {
return gerrors.ErrServerShutdown
}
c.releaseUDP()
return nil
}