From c3612c7186523deb2a62b69321e3f92b5eb4ffca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alby=20Hern=C3=A1ndez?= Date: Mon, 21 Oct 2024 16:48:52 +0100 Subject: [PATCH] feat: Add ability to perform periodic cleanup --- cmd/main.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index c21dfbf..c38fb68 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,7 +20,9 @@ import ( "log" "os" "os/signal" + "runtime/debug" "syscall" + "time" "bifrost/internal/config" "bifrost/internal/globals" @@ -28,11 +30,21 @@ import ( ) var ( - logLevelFlag = flag.String("log-level", "info", "Verbosity level for logs") - disableTraceFlag = flag.Bool("disable-trace", true, "Disable showing traces in logs") - configFlag = flag.String("config", "bifrost.yaml", "Path to the config file") + logLevelFlag = flag.String("log-level", "info", "Verbosity level for logs") + disableTraceFlag = flag.Bool("disable-trace", true, "Disable showing traces in logs") + configFlag = flag.String("config", "bifrost.yaml", "Path to the config file") + enablePeriodicCleanupFlag = flag.Bool("enable-periodic-cleanup", false, "Enable periodic memory cleanup") + periodicCleanupDurationFlag = flag.String("periodic-cleanup-duration", "4m", "Duration for periodic memory cleanup") ) +// periodicFree is a function that runs periodically to free memory +func periodicFree(d time.Duration) { + tick := time.Tick(d) + for range tick { + debug.FreeOSMemory() + } +} + func main() { flag.Parse() @@ -62,6 +74,15 @@ func main() { // EXECUTION FLOW RELATED ///////////////////////////// + if *enablePeriodicCleanupFlag { + + periodicCleanupDuration, err := time.ParseDuration(*periodicCleanupDurationFlag) + if err != nil { + globals.Application.Logger.Fatalf(fmt.Sprintf("failed parsing periodic cleanup duration: %s", err.Error())) + } + go periodicFree(periodicCleanupDuration) + } + s := httpserver.NewHttpServer() go s.Run(fmt.Sprintf("%s:%s", configContent.Listener.Host, configContent.Listener.Port)) defer s.Stop()