Added regular garbage collection

This commit is contained in:
Jan-Lukas Else 2021-03-13 13:17:42 +01:00
parent 5aa04ba366
commit 94cc221625
4 changed files with 27 additions and 11 deletions

View File

@ -9,7 +9,6 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"runtime"
"strconv"
"strings"
"time"
@ -211,10 +210,6 @@ func getCache(key string, next http.Handler, r *http.Request) (item *cacheItem)
func purgeCache() {
cacheR.Clear()
// Do manual GC
go func() {
runtime.GC()
}()
}
func setInternalCacheExpirationHeader(w http.ResponseWriter, expiration int) {

24
garbagecollector.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"log"
"runtime"
"time"
)
func initGC() {
go func() {
ticker := time.NewTicker(10 * time.Minute)
for range ticker.C {
go doGC()
}
}()
}
func doGC() {
var old, new runtime.MemStats
runtime.ReadMemStats(&old)
runtime.GC()
runtime.ReadMemStats(&new)
log.Printf("Alloc: %v MiB → %v MiB", old.Alloc/1024/1024, new.Alloc/1024/1024)
}

View File

@ -6,7 +6,6 @@ import (
"log"
"net/http"
"net/url"
"runtime"
"strconv"
"strings"
"sync/atomic"
@ -90,11 +89,6 @@ func reloadRouter() error {
}
purgeCache()
d.swapHandler(h)
// Do manual GC
go func() {
time.Sleep(10 * time.Second)
runtime.GC()
}()
return nil
}

View File

@ -52,6 +52,9 @@ func main() {
}
}
// Init regular garbage collection
initGC()
// Execute pre-start hooks
preStartHooks()
// Initialize everything else