Skip to content

Commit

Permalink
Increase file descriptor limit at startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jech committed Jul 29, 2021
1 parent 82b10b2 commit acb3eac
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
8 changes: 8 additions & 0 deletions galene.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
7 changes: 7 additions & 0 deletions limit/limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build windows plan9

package limit

func Nofile(desired uint64) (uint64, error) {
return desired, nil
}
42 changes: 42 additions & 0 deletions limit/limit_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// +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
}
}

limit := unix.Rlimit{
Cur: old.Max,
Max: old.Max,
}
err = unix.Setrlimit(unix.RLIMIT_NOFILE, &limit)
if err != nil {
return old.Cur, err
}
return limit.Cur, nil
}

0 comments on commit acb3eac

Please sign in to comment.