Browse Source

Added regular garbage collection

master
Jan-Lukas Else 1 month ago
parent
commit
94cc221625
4 changed files with 27 additions and 11 deletions
  1. +0
    -5
      cache.go
  2. +24
    -0
      garbagecollector.go
  3. +0
    -6
      http.go
  4. +3
    -0
      main.go

+ 0
- 5
cache.go 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
- 0
garbagecollector.go 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)
}

+ 0
- 6
http.go 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
}


+ 3
- 0
main.go View File

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


Loading…
Cancel
Save