From f0fb28c68dfff4979113c1037e877ae57c64f9f6 Mon Sep 17 00:00:00 2001 From: w1n2k Date: Thu, 19 Jan 2017 19:00:14 +0300 Subject: [PATCH] Implemented hot swap of autogc --- controller/controller.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index 096ac171..98d486af 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -230,25 +230,34 @@ func ListenAndServeEx(host string, port int, dir string, ln *net.Listener, http } func (c *Controller) watchGC() { - autoGC := c.config.AutoGC - - if autoGC == 0 { - return - } - - t := time.NewTicker(time.Second * time.Duration(autoGC)) + var elapsed uint64 + t := time.NewTicker(time.Second) defer t.Stop() for range t.C { + elapsed++ + c.mu.RLock() + autoGC := c.config.AutoGC + c.mu.RUnlock() + func() { - c.mu.RLock() - if c.stopWatchingAutoGC { - c.mu.RUnlock() + if autoGC == 0 { return } + if c.stopWatchingAutoGC { + return + } + + if elapsed < autoGC { + return + } + + c.mu.RLock() runtime.GC() debug.FreeOSMemory() + c.mu.RUnlock() + elapsed = 0 }() } }