-
Notifications
You must be signed in to change notification settings - Fork 4
/
ae.go
221 lines (198 loc) · 4.24 KB
/
ae.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
package main
import (
"errors"
"golang.org/x/sys/unix"
"time"
)
type TeType int
const (
AeNormal TeType = iota + 1
AeOnce
)
type FileProc func(loop *AeLoop, fd int, extra interface{})
type TimeProc func(loop *AeLoop, id int, extra interface{})
type AeFileEvent struct {
fd int
proc FileProc
extra interface{}
}
type AeTimeEvent struct {
id int
mask TeType
when int64 // ms
interval int64 // ms
proc TimeProc
extra interface{}
next *AeTimeEvent
}
type AeLoop struct {
FileEvents map[int]*AeFileEvent
TimeEvents *AeTimeEvent
fileEventFd int
timeEventNextId int
events []*AeFileEvent // file events cache
}
func (loop *AeLoop) AddRead(fd int, proc FileProc, extra interface{}) {
err := unix.EpollCtl(loop.fileEventFd, unix.EPOLL_CTL_ADD, fd, &unix.EpollEvent{
Fd: int32(fd),
Events: unix.EPOLLIN,
})
if err != nil {
panic(err)
}
loop.FileEvents[fd] = &AeFileEvent{
fd: fd,
proc: proc,
extra: extra,
}
}
func (loop *AeLoop) ModRead(fd int, proc FileProc, extra interface{}) {
err := unix.EpollCtl(loop.fileEventFd, unix.EPOLL_CTL_MOD, fd, &unix.EpollEvent{
Fd: int32(fd),
Events: unix.EPOLLIN,
})
if err != nil {
panic(err)
}
fe := loop.FileEvents[fd]
fe.proc = proc
fe.extra = extra
}
func (loop *AeLoop) ModWrite(fd int, proc FileProc, extra interface{}) {
err := unix.EpollCtl(loop.fileEventFd, unix.EPOLL_CTL_MOD, fd, &unix.EpollEvent{
Fd: int32(fd),
Events: unix.EPOLLOUT,
})
if err != nil {
panic(err)
}
fe := loop.FileEvents[fd]
fe.proc = proc
fe.extra = extra
}
func (loop *AeLoop) ModDetach(fd int) {
err := unix.EpollCtl(loop.fileEventFd, unix.EPOLL_CTL_DEL, fd, &unix.EpollEvent{
Fd: int32(fd),
Events: unix.EPOLLIN | unix.EPOLLOUT,
})
if err != nil {
panic(err)
}
delete(loop.FileEvents, fd)
}
func GetMsTime() int64 {
return time.Now().UnixMilli()
}
func (loop *AeLoop) AddTimeEvent(mask TeType, interval int64, proc TimeProc, extra interface{}) int {
id := loop.timeEventNextId
loop.timeEventNextId++
loop.TimeEvents = &AeTimeEvent{
id: id,
mask: mask,
interval: interval,
when: GetMsTime() + interval,
proc: proc,
extra: extra,
next: loop.TimeEvents,
}
return id
}
func (loop *AeLoop) RemoveTimeEvent(id int) {
p := loop.TimeEvents
var pre *AeTimeEvent
for p != nil {
if p.id == id {
if pre == nil {
loop.TimeEvents = p.next
} else {
pre.next = p.next
}
p.next = nil
break
}
pre = p
p = p.next
}
}
func AeLoopCreate() (*AeLoop, error) {
epollFd, err := unix.EpollCreate1(0)
if err != nil {
return nil, err
}
return &AeLoop{
FileEvents: make(map[int]*AeFileEvent),
fileEventFd: epollFd,
timeEventNextId: 1,
events: make([]*AeFileEvent, 128), // pre alloc
}, nil
}
func (loop *AeLoop) nearestTime() int64 {
var nearest = GetMsTime() + 1000
p := loop.TimeEvents
for p != nil {
nearest = min(nearest, p.when)
p = p.next
}
return nearest
}
func (loop *AeLoop) AeWait() (tes []*AeTimeEvent, fes []*AeFileEvent) {
timeout := loop.nearestTime() - GetMsTime()
if timeout <= 0 {
timeout = 10 // at least wait 10ms
}
var events [128]unix.EpollEvent
retry:
n, err := unix.EpollWait(loop.fileEventFd, events[:], int(timeout))
if err != nil {
// interrupted system call
if errors.Is(err, unix.EINTR) {
goto retry
}
log.Error().Msgf("epoll wait error: %v", err)
return
}
// collect file events
fes = loop.events[:0]
for _, ev := range events[:n] {
if ev.Events&unix.EPOLLIN != 0 {
fe := loop.FileEvents[int(ev.Fd)]
if fe != nil {
fes = append(fes, fe)
}
}
if ev.Events&unix.EPOLLOUT != 0 {
fe := loop.FileEvents[int(ev.Fd)]
if fe != nil {
fes = append(fes, fe)
}
}
}
// collect time events
now := GetMsTime()
p := loop.TimeEvents
for p != nil {
if p.when <= now {
tes = append(tes, p)
}
p = p.next
}
return
}
func (loop *AeLoop) AeProcess(tes []*AeTimeEvent, fes []*AeFileEvent) {
for _, te := range tes {
te.proc(loop, te.id, te.extra)
if te.mask == AeOnce {
loop.RemoveTimeEvent(te.id)
} else {
te.when = GetMsTime() + te.interval
}
}
for _, fe := range fes {
fe.proc(loop, fe.fd, fe.extra)
}
}
func (loop *AeLoop) AeMain() {
for {
loop.AeProcess(loop.AeWait())
}
}