From 1dd2586c397199250fe8a2d002a21022aa858b84 Mon Sep 17 00:00:00 2001 From: Ciprian Dorin Craciun Date: Fri, 2 Sep 2022 12:57:16 +0300 Subject: [PATCH] [server] Fix running on Android (`tcplisten` had issues determining max TCP backlog). --- sources/cmd/server/listen.go | 39 ++++++++++++++++++++++++++++++++++++ sources/cmd/server/server.go | 11 +++++----- 2 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 sources/cmd/server/listen.go diff --git a/sources/cmd/server/listen.go b/sources/cmd/server/listen.go new file mode 100644 index 0000000..2a136b6 --- /dev/null +++ b/sources/cmd/server/listen.go @@ -0,0 +1,39 @@ + + +package server + + +import "runtime" +import "net" + + +import "github.com/valyala/tcplisten" +import "github.com/valyala/fasthttp/reuseport" + + + + +func listenTcp (_endpoint string) (net.Listener, error) { + + if runtime.GOOS == "android" { + var _config = & tcplisten.Config { + ReusePort : true, + DeferAccept : true, + FastOpen : true, + Backlog : 1024, + } + return _config.NewListener ("tcp4", _endpoint) + } + + // FIXME: Perhaps always use `tcplisten`... + + return reuseport.Listen ("tcp4", _endpoint) +} + + + + +func listenUdp (_endpoint string) (net.PacketConn, error) { + return net.ListenPacket ("udp4", _endpoint) +} + diff --git a/sources/cmd/server/server.go b/sources/cmd/server/server.go index 63a4919..8f329cc 100644 --- a/sources/cmd/server/server.go +++ b/sources/cmd/server/server.go @@ -29,7 +29,6 @@ import "unsafe" import "github.com/colinmarc/cdb" import "github.com/valyala/fasthttp" -import "github.com/valyala/fasthttp/reuseport" import "github.com/lucas-clemente/quic-go" import "github.com/lucas-clemente/quic-go/http3" @@ -1780,7 +1779,7 @@ func main_0 () (error) { var _httpPlain1Listener net.Listener if _bindPlain1 != "" { - if _listener_0, _error := reuseport.Listen ("tcp4", _bindPlain1); _error == nil { + if _listener_0, _error := listenTcp (_bindPlain1); _error == nil { _httpPlain1Listener = _listener_0 } else { AbortError (_error, "[d5f51e9f] [bind-0a.] failed creating TCP listener!") @@ -1789,7 +1788,7 @@ func main_0 () (error) { var _httpPlain2Listener net.Listener if _bindPlain2 != "" { - if _listener_0, _error := reuseport.Listen ("tcp4", _bindPlain2); _error == nil { + if _listener_0, _error := listenTcp (_bindPlain2); _error == nil { _httpPlain2Listener = _listener_0 } else { AbortError (_error, "[546075c2] [bind-0b.] failed creating TCP listener!") @@ -1798,7 +1797,7 @@ func main_0 () (error) { var _httpTls1Listener net.Listener if _bindTls1 != "" { - if _listener_0, _error := reuseport.Listen ("tcp4", _bindTls1); _error == nil { + if _listener_0, _error := listenTcp (_bindTls1); _error == nil { _httpTls1Listener = _listener_0 } else { AbortError (_error, "[e35cc693] [bind-1..] failed creating TCP listener!") @@ -1807,7 +1806,7 @@ func main_0 () (error) { var _httpTls2Listener net.Listener if _bindTls2 != "" { - if _listener_0, _error := reuseport.Listen ("tcp4", _bindTls2); _error == nil { + if _listener_0, _error := listenTcp (_bindTls2); _error == nil { _httpTls2Listener = _listener_0 } else { AbortError (_error, "[63567445] [bind-2..] failed creating TCP listener!") @@ -1816,7 +1815,7 @@ func main_0 () (error) { var _httpQuicListener net.PacketConn if _bindQuic != "" { - if _listener_0, _error := net.ListenPacket ("udp4", _bindQuic); _error == nil { + if _listener_0, _error := listenUdp (_bindQuic); _error == nil { _httpQuicListener = _listener_0 } else { AbortError (_error, "[3b1bfc15] [bind-3..] failed creating UDP listener!")