From a78fb6d7476111bf464af25f0d208fed98989eca Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Thu, 29 Jul 2021 14:26:41 +0200 Subject: [PATCH] Increase file descriptor limit at startup. --- galene.go | 8 ++++++++ go.mod | 1 + go.sum | 3 ++- limit/limit.go | 7 +++++++ limit/limit_unix.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 limit/limit.go create mode 100644 limit/limit_unix.go diff --git a/galene.go b/galene.go index 8a25d85..b384d52 100644 --- a/galene.go +++ b/galene.go @@ -15,6 +15,7 @@ import ( "github.com/jech/galene/diskwriter" "github.com/jech/galene/group" "github.com/jech/galene/ice" + "github.com/jech/galene/limit" "github.com/jech/galene/turnserver" "github.com/jech/galene/webserver" ) @@ -104,6 +105,13 @@ func main() { }() } + n, err := limit.Nofile(0xFFFF) + if err != nil { + log.Printf("Couldn't set file descriptor limit: %v", err) + } else if n < 0xFFFF { + log.Printf("File descriptor limit is %v, please increase it!", n) + } + ice.ICEFilename = filepath.Join(dataDir, "ice-servers.json") go group.ReadPublicGroups() diff --git a/go.mod b/go.mod index 56aebcb..ee1d1af 100644 --- a/go.mod +++ b/go.mod @@ -13,4 +13,5 @@ require ( github.com/pion/turn/v2 v2.0.5 github.com/pion/webrtc/v3 v3.0.31 golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c ) diff --git a/go.sum b/go.sum index 68dd18c..f0dd56d 100644 --- a/go.sum +++ b/go.sum @@ -122,8 +122,9 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/limit/limit.go b/limit/limit.go new file mode 100644 index 0000000..11fb867 --- /dev/null +++ b/limit/limit.go @@ -0,0 +1,7 @@ +// +build windows plan9 + +package limit + +func Nofile(desired uint64) (uint64, error) { + return desired, nil +} diff --git a/limit/limit_unix.go b/limit/limit_unix.go new file mode 100644 index 0000000..6365d54 --- /dev/null +++ b/limit/limit_unix.go @@ -0,0 +1,44 @@ +// +build !windows +// +build !plan9 + +package limit + +import ( + "golang.org/x/sys/unix" +) + +func Nofile(desired uint64) (uint64, error) { + var old unix.Rlimit + + err := unix.Getrlimit(unix.RLIMIT_NOFILE, &old) + if err != nil { + return 0, err + } + + if old.Cur >= desired { + return old.Cur, nil + } + + if old.Max < desired { + limit := unix.Rlimit{ + Cur: desired, + Max: desired, + } + err := unix.Setrlimit(unix.RLIMIT_NOFILE, &limit) + if err == nil { + return limit.Cur, nil + } + + desired = old.Max + } + + limit := unix.Rlimit{ + Cur: desired, + Max: old.Max, + } + err = unix.Setrlimit(unix.RLIMIT_NOFILE, &limit) + if err != nil { + return old.Cur, err + } + return limit.Cur, nil +}