summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-02-26 16:51:18 +0300
committerValery Piashchynski <[email protected]>2020-02-26 16:51:18 +0300
commita2d1bc08a29a4e95020db2d92ae35e6194b6b179 (patch)
treed8dbf1d8dcac7cc423d5bf5e6bac8b0280a1fa24 /util
parentf9c4839266ff5c9aa289b2db0dae1e157011ffd3 (diff)
Add reuse, defer and fast-accept ports
Slightly update CI, add windows and macOS build targets
Diffstat (limited to 'util')
-rw-r--r--util/network.go15
-rw-r--r--util/network_windows.go33
2 files changed, 48 insertions, 0 deletions
diff --git a/util/network.go b/util/network.go
index b9066de7..54d29519 100644
--- a/util/network.go
+++ b/util/network.go
@@ -1,8 +1,11 @@
+// +build linux
+
package util
import (
"errors"
"fmt"
+ "github.com/valyala/tcplisten"
"net"
"os"
"strings"
@@ -27,6 +30,18 @@ func CreateListener(address string) (net.Listener, error) {
}
}
+ cfg := tcplisten.Config{
+ ReusePort: true,
+ DeferAccept: true,
+ FastOpen: true,
+ Backlog: 0,
+ }
+
+ // tcp4 is currently supported
+ if dsn[0] == "tcp" {
+ return cfg.NewListener("tcp4", dsn[1])
+ }
+
return net.Listen(dsn[0], dsn[1])
}
diff --git a/util/network_windows.go b/util/network_windows.go
new file mode 100644
index 00000000..e5bc67c8
--- /dev/null
+++ b/util/network_windows.go
@@ -0,0 +1,33 @@
+// +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])
+} \ No newline at end of file