-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller_epoll.go
330 lines (281 loc) · 7.94 KB
/
poller_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
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
//go:build linux && !channels
package asyncigo
import (
"context"
"encoding/binary"
"errors"
"io"
"net"
"os"
"runtime"
"time"
"golang.org/x/sys/unix"
)
// EpollPoller is an epoll-backed [Poller] implementation.
type EpollPoller struct {
epfd int
waker io.ReadWriteCloser
wakerBuf []byte
subscribed map[int32]*EpollAsyncFile
events []unix.EpollEvent
}
// NewPoller constructs a new EpollPoller.
// Will fail if an epoll handle could not be created.
func NewPoller() (Poller, error) {
epfd, err := unix.EpollCreate1(0)
if err != nil {
return nil, err
}
poller := &EpollPoller{
epfd: epfd,
wakerBuf: make([]byte, 8),
subscribed: make(map[int32]*EpollAsyncFile),
events: make([]unix.EpollEvent, 100),
}
// eventfd for waking up the poller from another thread
wakerFd, err := unix.Eventfd(0, unix.EFD_NONBLOCK)
if err != nil {
return nil, err
}
poller.waker, err = poller.Open(uintptr(wakerFd))
if err != nil {
return nil, err
}
return poller, nil
}
// Close implements [Poller].
func (e *EpollPoller) Close() error {
return unix.Close(e.epfd)
}
// Wait implements [Poller].
func (e *EpollPoller) Wait(timeout time.Duration) error {
n, err := unix.EpollWait(e.epfd, e.events, max(0, int(timeout.Milliseconds())))
if err != nil {
if errors.Is(err, unix.EINTR) {
err = nil
}
return err
}
for i := 0; i < n; i++ {
fd := e.events[i].Fd
if file := e.subscribed[fd]; file != nil {
file.notifyReady()
}
}
return nil
}
// WakeupThreadsafe implements [Poller].
func (e *EpollPoller) WakeupThreadsafe() error {
buf := make([]byte, 8)
binary.NativeEndian.PutUint64(buf, 1)
_, err := e.waker.Write(buf)
return err
}
// Subscribe instructs the poller to start listening for events for the given file handle.
func (e *EpollPoller) Subscribe(target *EpollAsyncFile) error {
fd := int(target.Fd())
if err := unix.SetNonblock(fd, true); err != nil {
return err
}
event := unix.EpollEvent{Events: unix.EPOLLIN | unix.EPOLLOUT | unix.EPOLLPRI | unix.EPOLLET, Fd: int32(fd)}
if err := unix.EpollCtl(e.epfd, unix.EPOLL_CTL_ADD, fd, &event); err != nil {
return err
}
e.subscribed[int32(fd)] = target
return nil
}
// Unsubscribe instructs the poller to stop listening for events for the given file handle.
func (e *EpollPoller) Unsubscribe(target *EpollAsyncFile) error {
fd := int(target.Fd())
delete(e.subscribed, int32(fd))
return unix.EpollCtl(e.epfd, unix.EPOLL_CTL_DEL, fd, nil)
}
// Open wraps the given file descriptor and subscribes to its events.
func (e *EpollPoller) Open(fd uintptr) (file AsyncReadWriteCloser, err error) {
f := NewEpollAsyncFile(e, os.NewFile(fd, ""))
if err := e.Subscribe(f); err != nil {
return nil, err
}
return f, nil
}
// Pipe implements [Poller].
func (e *EpollPoller) Pipe() (r, w AsyncReadWriteCloser, err error) {
p := make([]int, 2)
if err := unix.Pipe(p); err != nil {
return nil, nil, err
}
rf, wf := p[0], p[1]
if r, err = e.Open(uintptr(rf)); err != nil {
_ = unix.Close(rf)
_ = unix.Close(wf)
return nil, nil, err
}
if w, err = e.Open(uintptr(wf)); err != nil {
_ = unix.Close(rf)
_ = unix.Close(wf)
_ = r.Close()
return nil, nil, err
}
return r, w, nil
}
// Dial implements [Poller].
func (e *EpollPoller) Dial(ctx context.Context, network, address string) (conn AsyncReadWriteCloser, err error) {
if network != "tcp" {
return nil, errors.New("unsupported connection type")
}
// would have been nice to be able to rely on go's own intelligent address resolution,
// and then just retrieve the underlying fd and perform non-blocking operations with it,
// but there's no way to keep the connecting code from blocking,
// so just do a simple, naive getaddrinfo/socket/connect
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
portNum, err := net.DefaultResolver.LookupPort(ctx, network, port)
if err != nil {
return nil, err
}
// this is still blocking though :(
// consider getaddrinfo_a???
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)
if err != nil {
return nil, err
}
// try to connect in parallel and return the first successful connection
futs := make([]Coroutine2[*EpollAsyncFile], len(addrs))
for i, addr := range addrs {
futs[i] = func(ctx context.Context) (*EpollAsyncFile, error) {
return e.dialSingle(ctx, addr, portNum)
}
}
return GetFirstResult(ctx, futs...)
}
func (e *EpollPoller) dialSingle(ctx context.Context, addr net.IPAddr, port int) (*EpollAsyncFile, error) {
domain, sockAddr, err := e.toSockAddr(addr, port)
if err != nil {
return nil, err
}
fd, err := unix.Socket(domain, unix.SOCK_STREAM|unix.SOCK_NONBLOCK, 0)
if err != nil {
return nil, err
}
f := NewEpollAsyncFile(e, NewSocket(fd))
if err := e.Subscribe(f); err != nil {
_ = f.Close()
return nil, err
}
for {
err := unix.Connect(fd, sockAddr)
if errors.Is(err, unix.EAGAIN) || errors.Is(err, unix.EINPROGRESS) || errors.Is(err, unix.EALREADY) {
if err := f.WaitForReady(ctx); err != nil {
_ = f.Close()
return nil, err
}
continue
}
if err != nil {
_ = f.Close()
return nil, err
} else {
return f, nil
}
}
}
func (e *EpollPoller) toSockAddr(addr net.IPAddr, port int) (domain int, sockAddr unix.Sockaddr, err error) {
if ipv4 := addr.IP.To4(); len(ipv4) == net.IPv4len {
return unix.AF_INET, &unix.SockaddrInet4{Port: port, Addr: [net.IPv4len]byte(ipv4)}, nil
} else if ipv6 := addr.IP.To16(); len(ipv6) == net.IPv6len {
// handling the zone seems really complicated so no thanks
return unix.AF_INET6, &unix.SockaddrInet6{Port: port, Addr: [net.IPv6len]byte(ipv6)}, nil
} else {
return domain, nil, errors.New("could not parse IP address")
}
}
// Fder represents a file handle that has an associated file descriptor.
type Fder interface {
io.ReadWriteCloser
// Fd returns the file descriptor of this handle.
Fd() uintptr
}
// EpollAsyncFile is an implementation of [AsyncReadWriteCloser] for [EpollPoller].
type EpollAsyncFile struct {
poller *EpollPoller
f Fder
readyFut *Future[any]
}
// NewEpollAsyncFile wraps the given file handle using an [EpollAsyncFile].
func NewEpollAsyncFile(poller *EpollPoller, f Fder) *EpollAsyncFile {
eaf := &EpollAsyncFile{
f: f,
poller: poller,
}
runtime.SetFinalizer(eaf, func(e *EpollAsyncFile) { _ = e.Close() })
return eaf
}
func (eaf *EpollAsyncFile) notifyReady() {
if eaf.readyFut != nil {
readyFut := eaf.readyFut
eaf.readyFut = nil
readyFut.SetResult(nil, nil)
}
}
// WaitForReady implements [AsyncReadWriteCloser].
func (eaf *EpollAsyncFile) WaitForReady(ctx context.Context) error {
if eaf.readyFut == nil {
eaf.readyFut = NewFuture[any]()
}
_, err := eaf.readyFut.Await(ctx)
return err
}
// Read implements [io.Reader].
func (eaf *EpollAsyncFile) Read(p []byte) (n int, err error) {
return eaf.f.Read(p)
}
// Write implements [io.Writer].
func (eaf *EpollAsyncFile) Write(p []byte) (n int, err error) {
return eaf.f.Write(p)
}
// Close implements [io.Closer].
func (eaf *EpollAsyncFile) Close() error {
_ = eaf.poller.Unsubscribe(eaf)
return eaf.f.Close()
}
// Fd implements [Fder].
func (eaf *EpollAsyncFile) Fd() uintptr {
return eaf.f.Fd()
}
// EpollSocket is a wrapper for a low-level socket file descriptor.
type EpollSocket struct {
fd int
}
// Read implements [io.Reader].
func (s *EpollSocket) Read(p []byte) (n int, err error) {
n, err = unix.Read(s.fd, p)
if n == 0 && err == nil {
err = io.EOF
}
return n, err
}
// Write implements [io.Writer].
func (s *EpollSocket) Write(p []byte) (n int, err error) {
return unix.Write(s.fd, p)
}
// Close implements [io.Closer].
func (s *EpollSocket) Close() error {
if s.fd < 0 {
return unix.EBADF
}
fd := s.fd
s.fd = -1
return unix.Close(fd)
}
// Fd implements [Fder].
func (s *EpollSocket) Fd() uintptr {
return uintptr(s.fd)
}
// NewSocket wraps the given file descriptor using an [EpollSocket].
func NewSocket(fd int) *EpollSocket {
s := &EpollSocket{fd: fd}
runtime.SetFinalizer(s, func(s *EpollSocket) { _ = s.Close() })
return s
}