From 45f56813e467099cf255455e0608519be1794529 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Thu, 17 Aug 2023 11:29:20 +0530 Subject: [PATCH] use SO_REUSEPORT_LB on FreeBSD (#106) * feat: Add support for SO_REUSEPORT_LB option as per freeBSD * fix: address review comments * chore: updated to use build flags * chore: remove unnecessary code * chore: fix gofmt error --- control_freebsd.go | 27 +++++++++++++++++++++++++++ control_unix.go | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 control_freebsd.go diff --git a/control_freebsd.go b/control_freebsd.go new file mode 100644 index 0000000..cec1b11 --- /dev/null +++ b/control_freebsd.go @@ -0,0 +1,27 @@ +//go:build freebsd + +package reuseport + +import ( + "syscall" + + "golang.org/x/sys/unix" +) + +func Control(network, address string, c syscall.RawConn) (err error) { + controlErr := c.Control(func(fd uintptr) { + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1) + if err != nil { + return + } + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1) + if err != nil { + return + } + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT_LB, 1) + }) + if controlErr != nil { + err = controlErr + } + return +} diff --git a/control_unix.go b/control_unix.go index 4197d1f..e80688b 100644 --- a/control_unix.go +++ b/control_unix.go @@ -1,4 +1,4 @@ -//go:build !plan9 && !windows && !wasm +//go:build !plan9 && !windows && !wasm && !freebsd package reuseport