summaryrefslogtreecommitdiff
path: root/listen_linux.go
diff options
context:
space:
mode:
Diffstat (limited to 'listen_linux.go')
-rw-r--r--listen_linux.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/listen_linux.go b/listen_linux.go
new file mode 100644
index 0000000..b1220ce
--- /dev/null
+++ b/listen_linux.go
@@ -0,0 +1,34 @@
+package caddy
+
+import (
+ "context"
+ "net"
+ "syscall"
+ "time"
+
+ "go.uber.org/zap"
+ "golang.org/x/sys/unix"
+)
+
+// ListenTimeout is the same as Listen, but with a configurable keep-alive timeout duration.
+func ListenTimeout(network, addr string, keepalivePeriod time.Duration) (net.Listener, error) {
+ // check to see if plugin provides listener
+ if ln, err := getListenerFromPlugin(network, addr); err != nil || ln != nil {
+ return ln, err
+ }
+
+ config := &net.ListenConfig{Control: reusePort, KeepAlive: keepalivePeriod}
+ return config.Listen(context.Background(), network, addr)
+}
+
+func reusePort(network, address string, conn syscall.RawConn) error {
+ return conn.Control(func(descriptor uintptr) {
+ if err := unix.SetsockoptInt(int(descriptor), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
+ Log().Error("setting SO_REUSEPORT",
+ zap.String("network", network),
+ zap.String("address", address),
+ zap.Uintptr("descriptor", descriptor),
+ zap.Error(err))
+ }
+ })
+}