Skip to content

Commit 1d8fe19

Browse files
authored
Add reuseport support for Solaris (#2046)
1 parent 01d533a commit 1d8fe19

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

reuseport/reuseport.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build !windows && !aix
1+
//go:build !windows && !aix && !solaris
22

33
// Package reuseport provides TCP net.Listener with SO_REUSEPORT support.
44
//

reuseport/reuseport_solaris.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build solaris
2+
3+
package reuseport
4+
5+
import (
6+
"context"
7+
"net"
8+
"syscall"
9+
10+
"golang.org/x/sys/unix"
11+
)
12+
13+
const (
14+
_SO_REUSEPORT = 0x100e
15+
)
16+
17+
var listenConfig = net.ListenConfig{
18+
Control: func(network, address string, c syscall.RawConn) (err error) {
19+
return c.Control(func(fd uintptr) {
20+
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
21+
if err == nil {
22+
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, _SO_REUSEPORT, 1)
23+
}
24+
})
25+
},
26+
}
27+
28+
// Listen returns a TCP listener with the SO_REUSEADDR and SO_REUSEPORT options set.
29+
func Listen(network, addr string) (net.Listener, error) {
30+
return listenConfig.Listen(context.Background(), network, addr)
31+
}

0 commit comments

Comments
 (0)