Skip to content

Instantly share code, notes, and snippets.

@miekg
Created May 1, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miekg/e80d2eadfea48422178cdbaaf9f8a33f to your computer and use it in GitHub Desktop.
Save miekg/e80d2eadfea48422178cdbaaf9f8a33f to your computer and use it in GitHub Desktop.
SO_REUSEPORT for am64 Golang
diff --git a/udp_linux.go b/udp_linux.go
index 033df42..e1f9be8 100644
--- a/udp_linux.go
+++ b/udp_linux.go
@@ -44,18 +44,25 @@ func setUDPSocketOptions4(conn *net.UDPConn) error {
if err != nil {
return err
}
+ defer file.Close()
+
+ if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IP, syscall.SO_REUSEADDR, 1); err != nil {
+ return err
+ }
+
+ if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IP, 0x0200, 1); err != nil { // SO_REUSEPORT
+ return err
+ }
+
if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IP, syscall.IP_PKTINFO, 1); err != nil {
- file.Close()
return err
}
// Calling File() above results in the connection becoming blocking, we must fix that.
// See https://github.com/miekg/dns/issues/279
err = syscall.SetNonblock(int(file.Fd()), true)
if err != nil {
- file.Close()
return err
}
- file.Close()
return nil
}
@@ -65,16 +72,24 @@ func setUDPSocketOptions6(conn *net.UDPConn) error {
if err != nil {
return err
}
+ defer file.Close()
+
+ if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.SO_REUSEADDR, 1); err != nil {
+ return err
+ }
+
+ if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, 0x0200, 1); err != nil { // SO_REUSEPORT
+ return err
+ }
+
if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.IPV6_RECVPKTINFO, 1); err != nil {
- file.Close()
return err
}
+
err = syscall.SetNonblock(int(file.Fd()), true)
if err != nil {
- file.Close()
return err
}
- file.Close()
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment