-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove go_reuseport module dependency
- Loading branch information
1 parent
c160257
commit 0c0e04d
Showing
20 changed files
with
953 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd | ||
// +build linux darwin dragonfly freebsd netbsd openbsd | ||
|
||
// Package reuseport provides a function that returns a net.Listener powered | ||
// by a net.FileListener with a SO_REUSEPORT option set to the socket. | ||
package reuseport | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
const fileNameTemplate = "reuseport.%d.%s.%s" | ||
|
||
var errUnsupportedProtocol = errors.New("only tcp, tcp4, tcp6, udp, udp4, udp6 are supported") | ||
|
||
// getSockaddr parses protocol and address and returns implementor | ||
// of syscall.Sockaddr: syscall.SockaddrInet4 or syscall.SockaddrInet6. | ||
func getSockaddr(proto, addr string) (sa syscall.Sockaddr, soType int, err error) { | ||
switch proto { | ||
case "tcp", "tcp4", "tcp6": | ||
return getTCPSockaddr(proto, addr) | ||
case "udp", "udp4", "udp6": | ||
return getUDPSockaddr(proto, addr) | ||
default: | ||
return nil, -1, errUnsupportedProtocol | ||
} | ||
} | ||
|
||
func getSocketFileName(proto, addr string) string { | ||
return fmt.Sprintf(fileNameTemplate, os.Getpid(), proto, addr) | ||
} | ||
|
||
// Listen function is an alias for NewReusablePortListener. | ||
func Listen(proto, addr string) (l net.Listener, err error) { | ||
return NewReusablePortListener(proto, addr) | ||
} | ||
|
||
// ListenPacket is an alias for NewReusablePortPacketConn. | ||
func ListenPacket(proto, addr string) (l net.PacketConn, err error) { | ||
return NewReusablePortPacketConn(proto, addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build darwin || dragonfly || freebsd || netbsd || openbsd | ||
// +build darwin dragonfly freebsd netbsd openbsd | ||
|
||
package reuseport | ||
|
||
import ( | ||
"runtime" | ||
"syscall" | ||
) | ||
|
||
var reusePort = syscall.SO_REUSEPORT | ||
|
||
func maxListenerBacklog() int { | ||
var ( | ||
n uint32 | ||
err error | ||
) | ||
|
||
switch runtime.GOOS { | ||
case "darwin", "freebsd": | ||
n, err = syscall.SysctlUint32("kern.ipc.somaxconn") | ||
case "netbsd": | ||
// NOTE: NetBSD has no somaxconn-like kernel state so far | ||
case "openbsd": | ||
n, err = syscall.SysctlUint32("kern.somaxconn") | ||
default: | ||
} | ||
|
||
return defaultBacklog(n, err) | ||
} | ||
|
||
func defaultBacklog(n uint32, err error) int { | ||
if n == 0 || err != nil { | ||
return syscall.SOMAXCONN | ||
} | ||
|
||
// FreeBSD stores the backlog in a uint16, as does Linux. | ||
// Assume the other BSDs do too. Truncate number to avoid wrapping. | ||
// See issue 5030. | ||
if n > 1<<16-1 { | ||
n = 1<<16 - 1 | ||
} | ||
return int(n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package reuseport | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
var reusePort = 0x0F | ||
var maxConnFileName = "/proc/sys/net/core/somaxconn" | ||
|
||
func maxListenerBacklog() int { | ||
fd, err := os.Open(maxConnFileName) | ||
if err != nil { | ||
return syscall.SOMAXCONN | ||
} | ||
defer fd.Close() | ||
|
||
rd := bufio.NewReader(fd) | ||
line, err := rd.ReadString('\n') | ||
if err != nil { | ||
return syscall.SOMAXCONN | ||
} | ||
|
||
f := strings.Fields(line) | ||
if len(f) < 1 { | ||
return syscall.SOMAXCONN | ||
} | ||
|
||
n, err := strconv.Atoi(f[0]) | ||
return defaultBacklog(uint32(n), err) | ||
} | ||
|
||
func defaultBacklog(n uint32, err error) int { | ||
if n == 0 || err != nil { | ||
return syscall.SOMAXCONN | ||
} | ||
|
||
// Linux stores the backlog in a uint16. | ||
// Truncate number to avoid wrapping. | ||
// See issue 5030. | ||
if n > 1<<16-1 { | ||
n = 1<<16 - 1 | ||
} | ||
return int(n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package reuseport | ||
|
||
import ( | ||
"net" | ||
"syscall" | ||
) | ||
|
||
var ListenerBacklogMaxSize = maxListenerBacklog() | ||
|
||
func maxListenerBacklog() int { | ||
return syscall.SOMAXCONN | ||
} | ||
|
||
func NewReusablePortListener(proto, addr string) (net.Listener, error) { | ||
return net.Listen(proto, addr) | ||
} | ||
|
||
func NewReusablePortPacketConn(proto, addr string) (net.PacketConn, error) { | ||
return net.ListenPacket(proto, addr) | ||
} | ||
|
||
// Listen function is an alias for NewReusablePortListener. | ||
func Listen(proto, addr string) (l net.Listener, err error) { | ||
return NewReusablePortListener(proto, addr) | ||
} | ||
|
||
// ListenPacket is an alias for NewReusablePortPacketConn. | ||
func ListenPacket(proto, addr string) (l net.PacketConn, err error) { | ||
return NewReusablePortPacketConn(proto, addr) | ||
} |
Oops, something went wrong.