From 7811cf537555f3fa39369e112e286477c26622e1 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Mon, 23 Dec 2019 15:41:23 +0300 Subject: - enable port reuse on *unix --- util/network.go | 12 +++++++++++- util/network_win.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 util/network_win.go (limited to 'util') diff --git a/util/network.go b/util/network.go index b9066de7..f8f8fb36 100644 --- a/util/network.go +++ b/util/network.go @@ -1,3 +1,5 @@ +// +build !windows + package util import ( @@ -27,7 +29,15 @@ func CreateListener(address string) (net.Listener, error) { } } - return net.Listen(dsn[0], dsn[1]) + ls := net.ListenConfig{ + Control: func(network, address string, c syscall.RawConn) error { + return c.Control(func(descriptor uintptr) { + syscall.SetsockoptInt(int(descriptor), syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1) + }) + }, + } + + return ls.Listen(dsn[0], dsn[1]) } // fileExists checks if a file exists and is not a directory before we diff --git a/util/network_win.go b/util/network_win.go new file mode 100644 index 00000000..843d5779 --- /dev/null +++ b/util/network_win.go @@ -0,0 +1,43 @@ +// +build windows + +package util + +import ( + "errors" + "fmt" + "net" + "os" + "strings" + "syscall" +) + +// CreateListener crates socket listener based on DSN definition. +func CreateListener(address string) (net.Listener, error) { + dsn := strings.Split(address, "://") + if len(dsn) != 2 { + return nil, errors.New("invalid DSN (tcp://:6001, unix://file.sock)") + } + + if dsn[0] != "unix" && dsn[0] != "tcp" { + return nil, errors.New("invalid Protocol (tcp://:6001, unix://file.sock)") + } + + if dsn[0] == "unix" && fileExists(dsn[1]) { + err := syscall.Unlink(dsn[1]) + if err != nil { + return nil, fmt.Errorf("error during the unlink syscall: error %v", err) + } + } + + return net.Listen(dsn[0], dsn[1]) +} + +// fileExists checks if a file exists and is not a directory before we +// try using it to prevent further errors. +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} -- cgit v1.2.3