Skip to content

Commit 8db0c44

Browse files
author
zhengshuxin
committed
Support SO_REUSEPORT as default.
1 parent e5cc767 commit 8db0c44

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/acl-dev/master-go
22

33
go 1.16
4+
5+
require golang.org/x/sys v0.4.0 // indirect

service.go

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
//go:build linux || darwin
12
// +build linux darwin
23

34
package master
45

56
import (
7+
"context"
68
"errors"
79
"flag"
810
"fmt"
11+
"golang.org/x/sys/unix"
912
"log"
1013
"net"
1114
"os"
@@ -24,9 +27,9 @@ const (
2427

2528
var (
2629
listenFdCount = 1
27-
confPath string
28-
sockType string
29-
services string
30+
confPath string
31+
sockType string
32+
services string
3033
privilege = false
3134
verbose = false
3235
chrootOn = false
@@ -40,24 +43,24 @@ var (
4043
preJailHandler PreJailFunc = nil
4144
initHandler InitFunc = nil
4245
exitHandler ExitFunc = nil
43-
doneChan = make(chan bool)
44-
connCount = 0
45-
connMutex sync.RWMutex
46-
stopping = false
47-
prepareCalled = false
46+
doneChan = make(chan bool)
47+
connCount = 0
48+
connMutex sync.RWMutex
49+
stopping = false
50+
prepareCalled = false
4851
)
4952

5053
// from command args
5154
var (
52-
Configure string
53-
ServiceName string
54-
ServiceType string
55-
Verbose bool
56-
Unprivileged bool
57-
Chroot = false
58-
SocketCount = 1
59-
60-
Alone bool
55+
Configure string
56+
ServiceName string
57+
ServiceType string
58+
Verbose bool
59+
Unprivileged bool
60+
Chroot = false
61+
SocketCount = 1
62+
63+
Alone bool
6164
)
6265

6366
// setOpenMax set the max opened file handles for current process which let
@@ -208,8 +211,8 @@ func chroot() {
208211
}
209212
}
210213

211-
// GetListenersByAddrs In run alone mode, the application should give the listening addrs
212-
// and call this function to listen the given addrs
214+
// GetListenersByAddrs In run alone mode, the application should give the
215+
// listening addrs and call this function to listen the given addrs
213216
func GetListenersByAddrs(addrs string) ([]net.Listener, error) {
214217
if len(addrs) == 0 {
215218
log.Println("No valid addrs for listening")
@@ -222,9 +225,17 @@ func GetListenersByAddrs(addrs string) ([]net.Listener, error) {
222225
addrs = strings.Replace(addrs, ",", ";", -1)
223226
tokens := strings.Split(addrs, ";")
224227

228+
cfg := net.ListenConfig{
229+
Control: func(network, address string, c syscall.RawConn) error {
230+
return c.Control(func(fd uintptr) {
231+
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEADDR, 1)
232+
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1)
233+
})
234+
},
235+
}
225236
listeners := []net.Listener(nil)
226237
for _, addr := range tokens {
227-
ln, err := net.Listen("tcp", addr)
238+
ln, err := cfg.Listen(context.Background(), "tcp", addr)
228239
if err == nil {
229240
listeners = append(listeners, ln)
230241
log.Printf("Listen %s ok\r\n", addr)
@@ -240,11 +251,11 @@ func GetListenersByAddrs(addrs string) ([]net.Listener, error) {
240251
return listeners, nil
241252
}
242253

243-
// GetListeners In acl_master daemon running mode, this function will be called to
244-
// init the listener handles.
254+
// GetListeners In acl_master daemon running mode, this function will be called
255+
// to init the listener handles.
245256
func GetListeners() ([]net.Listener, error) {
246257
listeners := []net.Listener(nil)
247-
for fd := listenFdStart; fd < listenFdStart + listenFdCount; fd++ {
258+
for fd := listenFdStart; fd < listenFdStart+listenFdCount; fd++ {
248259
file := os.NewFile(uintptr(fd), "open one listen fd")
249260
if file == nil {
250261
log.Printf("os.NewFile failed for fd=%d", fd)
@@ -292,9 +303,9 @@ func ServiceInit(addrs string, stopHandler func(bool)) ([]net.Listener, error) {
292303
var listeners []net.Listener
293304
var daemonMode bool
294305

295-
// If alone is false and sockType has been set, we'll start the service in daemon mode,
296-
// else we'll start the service in alone mode. The sockType is coming from the the
297-
// master service framework.
306+
// If alone is false and sockType has been set, we'll start the service
307+
// in daemon mode, else we'll start the service in alone mode. The sockType
308+
// is coming from the the master service framework.
298309

299310
if !Alone && len(sockType) > 0 {
300311
var err error

0 commit comments

Comments
 (0)