From de0514379109646a4f6cb68196c20aecaf06d048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Wed, 3 Jan 2024 12:00:05 +0100 Subject: [PATCH] cmd/forwarder: add default GOMEMLIMIT Setting GOMEMLIMIT has very positive impact on the GC policy. It allows to defer GC til there is less work. In my tests, using local/browser tests with MITM, the Heap usage is bellow 40MiB and the system allocated memory bellow 100MiB at all times. --- cmd/forwarder/main.go | 3 +++ command/run/run.go | 6 +----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/forwarder/main.go b/cmd/forwarder/main.go index 9e8dd21f..733580f3 100644 --- a/cmd/forwarder/main.go +++ b/cmd/forwarder/main.go @@ -18,6 +18,9 @@ func main() { if _, err := maxprocs.Set(maxprocs.Logger(nil)); err != nil { fmt.Fprintf(os.Stderr, "failed to set GOMAXPROCS: %v\n", err) } + if _, ok := os.LookupEnv("GOMEMLIMIT"); !ok { + os.Setenv("GOMEMLIMIT", "250MiB") + } if err := forwarder.Command().Execute(); err != nil { os.WriteFile("/dev/termination-log", []byte(err.Error()), 0o644) //nolint // best effort diff --git a/command/run/run.go b/command/run/run.go index a643cdc5..2a57b775 100644 --- a/command/run/run.go +++ b/command/run/run.go @@ -73,11 +73,7 @@ func (c *command) runE(cmd *cobra.Command, _ []string) (cmdErr error) { }() logger.Infof("Forwarder %s (%s)", version.Version, version.Commit) - if limit, ok := os.LookupEnv("GOMEMLIMIT"); ok { - logger.Debugf("resource limits: GOMAXPROCS=%d GOMEMLIMIT=%s", runtime.GOMAXPROCS(0), limit) - } else { - logger.Debugf("resource limits: GOMAXPROCS=%d", runtime.GOMAXPROCS(0)) - } + logger.Debugf("resource limits: GOMAXPROCS=%d GOMEMLIMIT=%s", runtime.GOMAXPROCS(0), os.Getenv("GOMEMLIMIT")) var ep []forwarder.APIEndpoint