Automatically set GOMEMLIMIT at 90% of available memory.

Or at the ratio specified by `AUTOMEMLIMIT`.
This commit is contained in:
Catherine
2025-09-18 03:31:51 +00:00
parent fb9f4a7c4a
commit 07a736382c
3 changed files with 21 additions and 0 deletions
+15
View File
@@ -3,10 +3,13 @@ package main
import (
"flag"
"log"
"log/slog"
"net"
"net/http"
"os"
"strings"
"github.com/KimMachineGun/automemlimit/memlimit"
)
var backend Backend
@@ -84,6 +87,18 @@ func main() {
log.Println("migrate v1 ok")
}
// Avoid being OOM killed by not garbage collecting early enough.
memlimit.SetGoMemLimitWithOpts(
memlimit.WithLogger(slog.Default()),
memlimit.WithProvider(
memlimit.ApplyFallback(
memlimit.FromCgroup,
memlimit.FromSystem,
),
),
memlimit.WithRatio(0.9),
)
log.Println("ready")
go serveHandler("pages", config.Listen.Pages, ServePages)