diff options
Diffstat (limited to 'utils/network.go')
-rwxr-xr-x | utils/network.go | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/utils/network.go b/utils/network.go index 301e2bab..4ff41350 100755 --- a/utils/network.go +++ b/utils/network.go @@ -1,5 +1,4 @@ //go:build linux || darwin || freebsd -// +build linux darwin freebsd package utils @@ -13,6 +12,11 @@ import ( "github.com/valyala/tcplisten" ) +const ( + IPV4 string = "tcp4" + IPV6 string = "tcp6" +) + // CreateListener // - SO_REUSEPORT. This option allows linear scaling server performance // on multi-CPU servers. @@ -61,13 +65,35 @@ func createTCPListener(addr string) (net.Listener, error) { ReusePort: true, DeferAccept: true, FastOpen: true, - Backlog: 0, } - listener, err := cfg.NewListener("tcp4", addr) + + /* + Options we may have here: + 1. [::1]:8080 //ipv6 + 2. [0:0:..]:8080 //ipv6 + 3. 127.0.0.1:8080 //ipv4 + 4. :8080 //ipv4 + 5. [::]:8080 //ipv6 + */ + host, _, err := net.SplitHostPort(addr) if err != nil { return nil, err } - return listener, nil + + // consider this is IPv4 + if host == "" { + return cfg.NewListener(IPV4, addr) + } + + return cfg.NewListener(netw(net.ParseIP(host)), addr) +} + +// check if we are listening on the ipv6 or ipv4 address +func netw(addr net.IP) string { + if addr.To4() == nil { + return IPV6 + } + return IPV4 } // fileExists checks if a file exists and is not a directory before we |