This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a function to automatically scale limits to system memory / FDs
- Loading branch information
1 parent
da2f584
commit 07c38a5
Showing
6 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package rcmgr | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFileDescriptorCounting(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("can't read file descriptors on Windows") | ||
} | ||
n := getNumFDs() | ||
require.NotZero(t, n) | ||
require.Less(t, n, int(1e6)) | ||
} | ||
|
||
func TestScaling(t *testing.T) { | ||
base := BaseLimit{ | ||
Streams: 100, | ||
StreamsInbound: 200, | ||
StreamsOutbound: 400, | ||
Conns: 10, | ||
ConnsInbound: 20, | ||
ConnsOutbound: 40, | ||
FD: 1, | ||
Memory: 1 << 20, | ||
} | ||
|
||
t.Run("no scaling if no increase is defined", func(t *testing.T) { | ||
cfg := ScalingLimitConfig{ServiceBaseLimit: base} | ||
scaled := cfg.Scale(8<<30, 100) | ||
require.Equal(t, base, scaled.DefaultServiceLimit) | ||
}) | ||
|
||
t.Run("scaling", func(t *testing.T) { | ||
cfg := ScalingLimitConfig{ | ||
TransientBaseLimit: base, | ||
TransientLimitIncrease: BaseLimitIncrease{ | ||
Streams: 1, | ||
StreamsInbound: 2, | ||
StreamsOutbound: 3, | ||
Conns: 4, | ||
ConnsInbound: 5, | ||
ConnsOutbound: 6, | ||
Memory: 7, | ||
FDFraction: 0.5, | ||
}, | ||
} | ||
scaled := cfg.Scale(128<<20+4<<30, 1000) | ||
require.Equal(t, 500, scaled.TransientLimit.FD) | ||
require.Equal(t, base.Streams+4, scaled.TransientLimit.Streams) | ||
require.Equal(t, base.StreamsInbound+4*2, scaled.TransientLimit.StreamsInbound) | ||
require.Equal(t, base.StreamsOutbound+4*3, scaled.TransientLimit.StreamsOutbound) | ||
require.Equal(t, base.Conns+4*4, scaled.TransientLimit.Conns) | ||
require.Equal(t, base.ConnsInbound+4*5, scaled.TransientLimit.ConnsInbound) | ||
require.Equal(t, base.ConnsOutbound+4*6, scaled.TransientLimit.ConnsOutbound) | ||
require.Equal(t, base.Memory+4*7, scaled.TransientLimit.Memory) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//go:build !linux && !darwin | ||
|
||
package rcmgr | ||
|
||
import "runtime" | ||
|
||
// TODO: figure out how to get the number of file descriptors on Windows and other systems | ||
func getNumFDs() int { | ||
log.Warnf("cannot determine number of file descriptors on %s", runtime.GOOS) | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build linux || darwin | ||
// +build linux darwin | ||
|
||
package rcmgr | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func getNumFDs() int { | ||
var l unix.Rlimit | ||
if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &l); err != nil { | ||
log.Errorw("failed to get fd limit", "error", err) | ||
return 0 | ||
} | ||
return int(l.Cur) | ||
} |