Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(rexctl): doctor check and fix for memory overcommit (#3645)

Hampus385febe1570b
diff --git a/fluxer_rexctl/internal/checks/overcommit.go b/fluxer_rexctl/internal/checks/overcommit.go
new file mode 100644
index 000000000..a4c63ee53
--- /dev/null
+++ b/fluxer_rexctl/internal/checks/overcommit.go
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package checks
+
+import (
+ "context"
+ "runtime"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host"
+)
+
+func init() {
+ Register(Check{ID: "memory-overcommit", Phases: Preinstall | Installed, Run: runOvercommit, Fix: fixOvercommit, SafeFix: true})
+}
+
+func runOvercommit(_ context.Context, _ *Env) Result {
+ if runtime.GOOS != "linux" {
+ return skip("")
+ }
+ if !host.OvercommitDisabled() {
+ return pass("Memory overcommit is on.")
+ }
+ r := fixable(warn("Memory overcommit is off, so Fluxer's cache can fail to save data when memory runs low.", "Run rexctl doctor --fix to turn it on."))
+ r.Manual = "Ask an administrator to add " + host.OvercommitSetting + " to " + host.OvercommitFile + " and run sudo sysctl --system."
+ return r
+}
+
+func fixOvercommit(ctx context.Context, _ *Env) error {
+ return host.EnableOvercommit(ctx)
+}
diff --git a/fluxer_rexctl/internal/host/overcommit.go b/fluxer_rexctl/internal/host/overcommit.go
new file mode 100644
index 000000000..b73c3bfad
--- /dev/null
+++ b/fluxer_rexctl/internal/host/overcommit.go
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package host
+
+import (
+ "context"
+ "os"
+ "runtime"
+ "strconv"
+ "strings"
+)
+
+const (
+ OvercommitSetting = "vm.overcommit_memory = 1"
+ OvercommitFile = "/etc/sysctl.d/61-fluxer-overcommit.conf"
+)
+
+func OvercommitDisabled() bool {
+ if runtime.GOOS != "linux" {
+ return false
+ }
+ data, err := os.ReadFile("/proc/sys/vm/overcommit_memory")
+ if err != nil {
+ return false
+ }
+ n, err := strconv.Atoi(strings.TrimSpace(string(data)))
+ return err == nil && n != 1
+}
+
+func EnableOvercommit(ctx context.Context) error {
+ if _, err := output(ctx, false, "sh", "-c", "printf '"+OvercommitSetting+"\\n' > "+OvercommitFile); err != nil {
+ return err
+ }
+ _, err := output(ctx, false, "sysctl", "-p", OvercommitFile)
+ return err
+}