我竟然在 go 里用 epoll

  • 编译要求 go version >= 1.6 (瞎写的, 需要 syscall 包支持, 我也不知道是不是 1.6)
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
package main

import (
"net"
"runtime"
"syscall"
)

const (
evListen = uint32((syscall.EPOLLET | syscall.EPOLLIN | syscall.EPOLLRDHUP) & 0xffffffff)
evClient = uint32((syscall.EPOLLET | syscall.EPOLLIN | syscall.EPOLLOUT | syscall.EPOLLRDHUP) & 0xffffffff)
evRead = uint32((syscall.EPOLLIN | syscall.EPOLLRDHUP | syscall.EPOLLERR) & 0xffffffff)
evWrite = uint32((syscall.EPOLLOUT | syscall.EPOLLHUP | syscall.EPOLLERR) & 0xffffffff)
)

func resolveSockAddr4(netaddr string) syscall.Sockaddr {
addr, err := net.ResolveTCPAddr("tcp4", netaddr)
if err != nil {
return nil
}
ip := addr.IP
if len(ip) == 0 {
ip = net.IPv4zero
}
sa4 := &syscall.SockaddrInet4{Port: addr.Port}
copy(sa4.Addr[:], ip.To4())
return sa4
}

type server struct {
epfd int
listenfd int
}

func (s *server) bind(fd int, ev uint32) error {
var e syscall.EpollEvent
syscall.SetNonblock(fd, true)
e.Events = ev
e.Fd = int32(fd)
return syscall.EpollCtl(s.epfd, syscall.EPOLL_CTL_ADD, fd, &e)
}

func (s *server) unbind(fd int) {
var e syscall.EpollEvent
syscall.EpollCtl(s.epfd, syscall.EPOLL_CTL_DEL, fd, &e)
}

func (s *server) init() (err error) {
s.epfd, err = syscall.EpollCreate1(syscall.EPOLL_CLOEXEC)
return
}

func (s *server) close() {
syscall.Close(s.listenfd)
syscall.Close(s.epfd)
}

func (s *server) listen(addr string) error {
lfd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
if err != nil {
return err
}
ok := false
defer func() {
if !ok {
syscall.Close(lfd)
}
}()
err = syscall.Bind(lfd, resolveSockAddr4(addr))
if err != nil {
return err
}
s.bind(lfd, evListen)
err = syscall.Listen(lfd, syscall.SOMAXCONN)
if err != nil {
return err
}
s.listenfd = lfd
ok = true
return nil
}

func (s *server) poll(onEvent func(int, int)) error {
var events [64]syscall.EpollEvent
for {
n, err := syscall.EpollWait(s.epfd, events[:], -1)
if err != nil {
if err == syscall.EINTR {
continue
}
return err
}
for i := 0; i < n; i++ {
if s.listenfd == int(events[i].Fd) {
for {
clientfd, _, err := syscall.Accept(s.listenfd)
if err != nil {
if err != syscall.EAGAIN {
return err
}
break
}
s.bind(clientfd, evClient)
}
} else {
available := 0
if (events[i].Events & evRead) != 0 {
available |= 1
}
if (events[i].Events & evWrite) != 0 {
available |= 2
}
onEvent(int(events[i].Fd), available)
}
}
}
}

func main() {
s := server{}
err := s.init()
if err != nil {
panic(err)
}
defer s.close()
err = s.listen(":6126")
if err != nil {
panic(err)
}
type polldesp struct {
fd int
rw int
}
handler := func(ch <-chan polldesp) {
var buf [1024]byte
for pd := range ch {
fd := pd.fd
shouldClose := false
if (pd.rw & 1) != 0 {
for {
n, err := syscall.Read(fd, buf[:])
if n > 0 {
data := buf[:n]
n, err = syscall.Write(fd, data)
if err == nil {
err = syscall.Fsync(fd)
}
}
if n == 0 || err != nil {
if err == syscall.EAGAIN {
err = nil
} else {
shouldClose = true
}
break
}
}
}
if shouldClose {
s.unbind(fd)
syscall.Close(fd)
}
}
}
ncpu := runtime.NumCPU()
chs := make([]chan polldesp, ncpu)
err = s.poll(func(fd int, rw int) {
ch := chs[fd%ncpu]
if ch == nil {
ch = make(chan polldesp, 2)
chs[fd%ncpu] = ch
go handler(ch)
}
ch <- polldesp{fd, rw}
})
if err != nil {
panic(err)
}
}