-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
epoll_default_poller.go
229 lines (206 loc) 路 7.56 KB
/
epoll_default_poller.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
// Copyright (c) 2019 Andy Pan
// Copyright (c) 2017 Joshua J Baker
//
// 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 && !poll_opt
// +build linux,!poll_opt
package netpoll
import (
"os"
"runtime"
"sync/atomic"
"unsafe"
"golang.org/x/sys/unix"
"github.com/panjf2000/gnet/v2/internal/queue"
"github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
)
// Poller represents a poller which is in charge of monitoring file-descriptors.
type Poller struct {
fd int // epoll fd
wfd int // wake fd
wfdBuf []byte // wfd buffer to read packet
netpollWakeSig int32
asyncTaskQueue queue.AsyncTaskQueue // queue with low priority
priorAsyncTaskQueue queue.AsyncTaskQueue // queue with high priority
}
// OpenPoller instantiates a poller.
func OpenPoller() (poller *Poller, err error) {
poller = new(Poller)
if poller.fd, err = unix.EpollCreate1(unix.EPOLL_CLOEXEC); err != nil {
poller = nil
err = os.NewSyscallError("epoll_create1", err)
return
}
if poller.wfd, err = unix.Eventfd(0, unix.EFD_NONBLOCK|unix.EFD_CLOEXEC); err != nil {
_ = poller.Close()
poller = nil
err = os.NewSyscallError("eventfd", err)
return
}
poller.wfdBuf = make([]byte, 8)
if err = poller.AddRead(&PollAttachment{FD: poller.wfd}); err != nil {
_ = poller.Close()
poller = nil
return
}
poller.asyncTaskQueue = queue.NewLockFreeQueue()
poller.priorAsyncTaskQueue = queue.NewLockFreeQueue()
return
}
// Close closes the poller.
func (p *Poller) Close() error {
if err := os.NewSyscallError("close", unix.Close(p.fd)); err != nil {
return err
}
return os.NewSyscallError("close", unix.Close(p.wfd))
}
// Make the endianness of bytes compatible with more linux OSs under different processor-architectures,
// according to http://man7.org/linux/man-pages/man2/eventfd.2.html.
var (
u uint64 = 1
b = (*(*[8]byte)(unsafe.Pointer(&u)))[:]
)
// UrgentTrigger puts task into priorAsyncTaskQueue and wakes up the poller which is waiting for network-events,
// then the poller will get tasks from priorAsyncTaskQueue and run them.
//
// Note that priorAsyncTaskQueue is a queue with high-priority and its size is expected to be small,
// so only those urgent tasks should be put into this queue.
func (p *Poller) UrgentTrigger(fn queue.TaskFunc, arg interface{}) (err error) {
task := queue.GetTask()
task.Run, task.Arg = fn, arg
p.priorAsyncTaskQueue.Enqueue(task)
if atomic.CompareAndSwapInt32(&p.netpollWakeSig, 0, 1) {
for _, err = unix.Write(p.wfd, b); err == unix.EINTR || err == unix.EAGAIN; _, err = unix.Write(p.wfd, b) {
}
}
return os.NewSyscallError("write", err)
}
// Trigger is like UrgentTrigger but it puts task into asyncTaskQueue,
// call this method when the task is not so urgent, for instance writing data back to the peer.
//
// Note that asyncTaskQueue is a queue with low-priority whose size may grow large and tasks in it may backlog.
func (p *Poller) Trigger(fn queue.TaskFunc, arg interface{}) (err error) {
task := queue.GetTask()
task.Run, task.Arg = fn, arg
p.asyncTaskQueue.Enqueue(task)
if atomic.CompareAndSwapInt32(&p.netpollWakeSig, 0, 1) {
for _, err = unix.Write(p.wfd, b); err == unix.EINTR || err == unix.EAGAIN; _, err = unix.Write(p.wfd, b) {
}
}
return os.NewSyscallError("write", err)
}
// Polling blocks the current goroutine, waiting for network-events.
func (p *Poller) Polling(callback func(fd int, ev uint32) error) error {
el := newEventList(InitPollEventsCap)
var wakenUp bool
msec := -1
for {
n, err := unix.EpollWait(p.fd, el.events, msec)
if n == 0 || (n < 0 && err == unix.EINTR) {
msec = -1
runtime.Gosched()
continue
} else if err != nil {
logging.Errorf("error occurs in epoll: %v", os.NewSyscallError("epoll_wait", err))
return err
}
msec = 0
for i := 0; i < n; i++ {
ev := &el.events[i]
if fd := int(ev.Fd); fd != p.wfd {
switch err = callback(fd, ev.Events); err {
case nil:
case errors.ErrAcceptSocket, errors.ErrEngineShutdown:
return err
default:
logging.Warnf("error occurs in event-loop: %v", err)
}
} else { // poller is awaken to run tasks in queues.
wakenUp = true
_, _ = unix.Read(p.wfd, p.wfdBuf)
}
}
if wakenUp {
wakenUp = false
task := p.priorAsyncTaskQueue.Dequeue()
for ; task != nil; task = p.priorAsyncTaskQueue.Dequeue() {
switch err = task.Run(task.Arg); err {
case nil:
case errors.ErrEngineShutdown:
return err
default:
logging.Warnf("error occurs in user-defined function, %v", err)
}
queue.PutTask(task)
}
for i := 0; i < MaxAsyncTasksAtOneTime; i++ {
if task = p.asyncTaskQueue.Dequeue(); task == nil {
break
}
switch err = task.Run(task.Arg); err {
case nil:
case errors.ErrEngineShutdown:
return err
default:
logging.Warnf("error occurs in user-defined function, %v", err)
}
queue.PutTask(task)
}
atomic.StoreInt32(&p.netpollWakeSig, 0)
if (!p.asyncTaskQueue.IsEmpty() || !p.priorAsyncTaskQueue.IsEmpty()) && atomic.CompareAndSwapInt32(&p.netpollWakeSig, 0, 1) {
for _, err = unix.Write(p.wfd, b); err == unix.EINTR || err == unix.EAGAIN; _, err = unix.Write(p.wfd, b) {
}
}
}
if n == el.size {
el.expand()
} else if n < el.size>>1 {
el.shrink()
}
}
}
const (
readEvents = unix.EPOLLPRI | unix.EPOLLIN
writeEvents = unix.EPOLLOUT
readWriteEvents = readEvents | writeEvents
)
// AddReadWrite registers the given file-descriptor with readable and writable events to the poller.
func (p *Poller) AddReadWrite(pa *PollAttachment) error {
return os.NewSyscallError("epoll_ctl add",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_ADD, pa.FD, &unix.EpollEvent{Fd: int32(pa.FD), Events: readWriteEvents}))
}
// AddRead registers the given file-descriptor with readable event to the poller.
func (p *Poller) AddRead(pa *PollAttachment) error {
return os.NewSyscallError("epoll_ctl add",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_ADD, pa.FD, &unix.EpollEvent{Fd: int32(pa.FD), Events: readEvents}))
}
// AddWrite registers the given file-descriptor with writable event to the poller.
func (p *Poller) AddWrite(pa *PollAttachment) error {
return os.NewSyscallError("epoll_ctl add",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_ADD, pa.FD, &unix.EpollEvent{Fd: int32(pa.FD), Events: writeEvents}))
}
// ModRead renews the given file-descriptor with readable event in the poller.
func (p *Poller) ModRead(pa *PollAttachment) error {
return os.NewSyscallError("epoll_ctl mod",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_MOD, pa.FD, &unix.EpollEvent{Fd: int32(pa.FD), Events: readEvents}))
}
// ModReadWrite renews the given file-descriptor with readable and writable events in the poller.
func (p *Poller) ModReadWrite(pa *PollAttachment) error {
return os.NewSyscallError("epoll_ctl mod",
unix.EpollCtl(p.fd, unix.EPOLL_CTL_MOD, pa.FD, &unix.EpollEvent{Fd: int32(pa.FD), Events: readWriteEvents}))
}
// Delete removes the given file-descriptor from the poller.
func (p *Poller) Delete(fd int) error {
return os.NewSyscallError("epoll_ctl del", unix.EpollCtl(p.fd, unix.EPOLL_CTL_DEL, fd, nil))
}