-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_epoll.go
242 lines (207 loc) · 5.89 KB
/
api_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
// Copyright 2023-2024 antlabs. 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 linux
// +build linux
package greatws
import (
"errors"
"io"
"time"
"golang.org/x/sys/unix"
)
const (
// 垂直触发
// 来自man 手册
// When used as an edge-triggered interface, for performance reasons,
// it is possible to add the file descriptor inside the epoll interface (EPOLL_CTL_ADD) once by specifying (EPOLLIN|EPOLLOUT).
// This allows you to avoid con‐
// tinuously switching between EPOLLIN and EPOLLOUT calling epoll_ctl(2) with EPOLL_CTL_MOD.
etRead = uint32(unix.EPOLLERR | unix.EPOLLHUP | unix.EPOLLRDHUP | unix.EPOLLPRI | unix.EPOLLIN | unix.EPOLLOUT | unix.EPOLLET)
etWrite = uint32(0)
etDelWrite = uint32(0)
etResetRead = uint32(0)
// 一次性触发, TODO: 这里要看下是否需要,还是垂直触发+overflow fd记录,目前没有使用
etReadOneShot = uint32(unix.EPOLLERR | unix.EPOLLHUP | unix.EPOLLRDHUP | unix.EPOLLPRI | unix.EPOLLIN | unix.EPOLLOUT | unix.EPOLLET | unix.EPOLLONESHOT)
etWriteOneShot = uint32(etReadOneShot)
etDelWriteOneShot = uint32(0)
etResetReadOneShot = uint32(etReadOneShot)
processWrite = uint32(unix.EPOLLOUT)
processRead = uint32(unix.EPOLLIN | unix.EPOLLRDHUP | unix.EPOLLHUP | unix.EPOLLERR)
)
type epollState struct {
epfd int
events []unix.EpollEvent
et bool
parent *EventLoop
rev uint32
wev uint32
dwEv uint32 // delete write event
resetEv uint32
}
func (e *epollState) getMultiEventLoop() *MultiEventLoop {
return e.parent.parent
}
func getReadWriteDeleteReset(oneShot bool, et bool) (uint32, uint32, uint32, uint32) {
if oneShot {
return etReadOneShot, etWriteOneShot, etDelWriteOneShot, etResetReadOneShot
}
if et {
return etRead, etWrite, etDelWrite, etResetRead
}
// if lt {
// return ltRead, ltWrite, ltDelWrite, ltResetRead
// }
return 0, 0, 0, 0
}
// 创建epoll handler
func apiEpollCreate(parent *EventLoop) (la linuxApi, err error) {
var e epollState
e.epfd, err = unix.EpollCreate1(0)
if err != nil {
return nil, err
}
e.events = make([]unix.EpollEvent, 128)
e.parent = parent
e.rev, e.wev, e.dwEv, e.resetEv = getReadWriteDeleteReset(false, true)
return &e, nil
}
// 释放
func (e *epollState) apiFree() {
unix.Close(e.epfd)
}
// 新加读事件
func (e *epollState) addRead(c *Conn) error {
if e.rev > 0 {
fd := int(c.getFd())
return unix.EpollCtl(e.epfd, unix.EPOLL_CTL_ADD, fd, &unix.EpollEvent{
Fd: int32(fd),
Events: e.rev,
})
}
return nil
}
// 新加写事件
func (e *epollState) addWrite(c *Conn) error {
if e.wev > 0 {
fd := int(c.getFd())
return unix.EpollCtl(e.epfd, unix.EPOLL_CTL_MOD, fd, &unix.EpollEvent{
Fd: int32(fd),
Events: e.wev,
})
}
return nil
}
// 删除写事件
func (e *epollState) delWrite(c *Conn) error {
if e.dwEv > 0 {
fd := int(c.getFd())
return unix.EpollCtl(e.epfd, unix.EPOLL_CTL_MOD, fd, &unix.EpollEvent{
Fd: int32(fd),
Events: e.dwEv,
})
}
return nil
}
// 重装添加读事件
func (e *epollState) resetRead(c *Conn) error {
fd := c.getFd()
if e.resetEv > 0 {
return unix.EpollCtl(e.epfd, unix.EPOLL_CTL_MOD, int(fd), &unix.EpollEvent{
Fd: int32(fd),
Events: e.resetEv,
})
}
return nil
}
// 删除事件
func (e *epollState) del(fd int) error {
return unix.EpollCtl(e.epfd, unix.EPOLL_CTL_DEL, fd, &unix.EpollEvent{Fd: int32(fd)})
}
// 事件循环
func (e *epollState) apiPoll(tv time.Duration) (retVal int, err error) {
msec := -1
if tv > 0 {
msec = int(tv) / int(time.Millisecond)
}
retVal, err = unix.EpollWait(e.epfd, e.events, msec)
e.getMultiEventLoop().addPollEvNum()
if err != nil {
if errors.Is(err, unix.EINTR) {
return 0, nil
}
return 0, err
}
numEvents := 0
if retVal > 0 {
numEvents = retVal
for i := 0; i < numEvents; i++ {
ev := &e.events[i]
conn := e.parent.getConn(int(ev.Fd))
if conn == nil {
unix.Close(int(ev.Fd))
continue
}
// 如果是关闭事件,直接关闭
if ev.Events&unix.EPOLLRDHUP > 0 {
conn.closeWithLock(io.EOF)
continue
}
if ev.Events&(unix.EPOLLERR|unix.EPOLLHUP|unix.EPOLLRDHUP) > 0 {
conn.closeWithLock(io.EOF)
continue
}
// 默认是io线程只做事件的分发,websocket包的读取和解析在parse loop里面做
if *e.getMultiEventLoop().parseInParseLoop {
isRead := ev.Events&processRead > 0
isWrite := ev.Events&processWrite > 0
e.getMultiEventLoop().parseLoop.addTask(int(ev.Fd), func() bool {
// 如果这里直接贴逻辑go test -race有时候会报错,使用函数则没大这个问题
return e.process(conn, isRead, isWrite)
})
continue
}
if ev.Events&processRead > 0 {
e.getMultiEventLoop().addReadEvNum()
// 读取数据,这里要发行下websocket的解析变成流式解析
err = conn.processWebsocketFrame()
if err != nil {
conn.closeWithLock(err)
}
}
if ev.Events&processWrite > 0 {
e.getMultiEventLoop().addWriteEvNum()
// 刷新下直接写入失败的数据
conn.flushOrClose()
}
}
}
return numEvents, nil
}
func (e *epollState) process(conn *Conn, isRead, isWrite bool) bool {
if isRead {
err := conn.processWebsocketFrame()
if err != nil {
conn.closeWithLock(err)
return true
}
}
if isWrite {
// 刷新下直接写入失败的数据
conn.flushOrClose()
}
return true
}
func (e *epollState) apiName() string {
return "epoll"
}