forked from shaovie/goev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptor.go
209 lines (188 loc) · 5.3 KB
/
acceptor.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
package goev
import (
"errors"
"net"
"os"
"strconv"
"strings"
"syscall"
"golang.org/x/sys/unix"
)
// Acceptor is a wrapper for socket listener, automatically creating a service
// and registering it with the reactor.
// Newly received file descriptors can be registered with the specified reactor.
type Acceptor struct {
IOHandle
reuseAddr bool // SO_REUSEADDR
reusePort bool // SO_REUSEPORT
fd int
sockRcvBufSize int // ignore equal 0
listenBacklog int
loopAcceptTimes int
newEvHanlderFunc func() EvHandler
reactor *Reactor
}
// NewAcceptor return an acceptor
//
// New socket has been set to non-blocking
func NewAcceptor(acceptorBindReactor *Reactor, newEvHanlderFunc func() EvHandler,
addr string, opts ...Option) (*Acceptor, error) {
evOptions := setOptions(opts...)
a := &Acceptor{
fd: -1,
reactor: acceptorBindReactor,
newEvHanlderFunc: newEvHanlderFunc,
listenBacklog: evOptions.listenBacklog,
sockRcvBufSize: evOptions.sockRcvBufSize,
reuseAddr: evOptions.reuseAddr,
reusePort: evOptions.reusePort,
}
a.loopAcceptTimes = a.listenBacklog / 2
if a.loopAcceptTimes < 1 {
a.loopAcceptTimes = 1
}
if err := a.open(addr); err != nil {
return nil, err
}
return a, nil
}
// open create a listen fd
// The addr format 192.168.0.1:8080 or :8080 or unix:/tmp/xxxx.sock
func (a *Acceptor) open(addr string) error {
p := strings.Index(addr, ":")
if p < 0 || p >= (len(addr)-1) {
return errors.New("Accetor open param:addr invalid")
}
if len(addr) > 5 {
s := addr[0:5]
if s == "unix:" {
return a.udsListen(addr[5:])
}
}
return a.tcpListen(addr)
}
// The addr format 192.168.0.1:8080 or :8080
func (a *Acceptor) tcpListen(addr string) error {
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
return errors.New("Socket in Acceptor.open: " + err.Error())
}
if a.reuseAddr == true {
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
syscall.Close(fd)
return errors.New("Set SO_REUSEADDR in Acceptor.open: " + err.Error())
}
}
if a.reusePort == true {
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
syscall.Close(fd)
return errors.New("Set SO_REUSEPORT in Acceptor.open: " + err.Error())
}
}
syscall.SetNonblock(fd, true)
if a.sockRcvBufSize > 0 {
// `sysctl -a | grep net.ipv4.tcp_rmem` 返回 min default max
// 默认 内核会在 min max 之间动态调整, default是初始值, 如果设置了SO_RCVBUF, 缓冲区大小不变成固定值,
// 内核也不会进行动态调整了
// 必须在listen/connect之前调用
// must < `sysctl -a | grep net.core.rmem_max`
err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, a.sockRcvBufSize)
if err != nil {
syscall.Close(fd)
return errors.New("Set SO_RCVBUF: " + err.Error())
}
}
ip := "0.0.0.0"
var port int64
ipp := strings.Split(addr, ":")
if len(ipp) != 2 {
syscall.Close(fd)
return errors.New("address is invalid! 192.168.1.1:80 or :80")
}
if len(ipp[0]) > 0 {
ip = ipp[0]
}
ip4 := net.ParseIP(ip)
if ip4 == nil {
syscall.Close(fd)
return errors.New("address is invalid! 192.168.1.1:80 or :80")
}
port, _ = strconv.ParseInt(ipp[1], 10, 64)
if port < 1 || port > 65535 {
syscall.Close(fd)
return errors.New("port must in (0, 65536)")
}
sa := syscall.SockaddrInet4{Port: int(port)}
copy(sa.Addr[:], ip4.To4())
if err := a.listen(fd, &sa); err != nil {
syscall.Close(fd)
return err
}
return nil
}
// The addr format /tmp/xxx.sock
func (a *Acceptor) udsListen(addr string) error {
os.RemoveAll(addr)
fd, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
return errors.New("Socket in Acceptor.open: " + err.Error())
}
syscall.SetNonblock(fd, true)
// SO_RCVBUF is invalid for unix sock
rsu := syscall.SockaddrUnix{Name: addr}
if err = a.listen(fd, &rsu); err != nil {
os.RemoveAll(addr)
syscall.Close(fd)
return err
}
return nil
}
func (a *Acceptor) listen(fd int, sa syscall.Sockaddr) error {
if err := syscall.Bind(fd, sa); err != nil {
return errors.New("syscall bind: " + err.Error())
}
if err := syscall.Listen(fd, a.listenBacklog); err != nil {
return errors.New("syscall listen: " + err.Error())
}
if err := a.reactor.AddEvHandler(a, fd, EvAccept); err != nil {
return errors.New("AddEvHandler in Acceptor.Open: " + err.Error())
}
a.fd = fd
return nil
}
// OnRead handle listner accept event
func (a *Acceptor) OnRead() bool {
for i := 0; i < a.loopAcceptTimes; i++ {
conn, _, err := syscall.Accept4(a.fd, syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC)
if err != nil {
if err == syscall.EINTR {
continue
} else if err == syscall.EMFILE {
// The per-process limit on the number of open file descriptors has been reached
if a.ScheduleTimer(a, 100 /*msec*/, 0) == nil {
a.reactor.RemoveEvHandler(a, a.fd)
}
}
break
}
h := a.newEvHanlderFunc()
if h.OnOpen(conn) == false {
h.OnClose()
}
}
return true
}
// OnTimeout readd to evpoll
func (a *Acceptor) OnTimeout(millisecond int64) bool {
if a.fd != -1 {
a.reactor.AddEvHandler(a, a.fd, EvAccept)
}
return false
}
// OnClose will not happen
func (a *Acceptor) OnClose() {
if a.fd != -1 {
syscall.Close(a.fd)
a.fd = -1
}
}