mirror of https://github.com/jlelse/GoBlog
Added regular garbage collection
This commit is contained in:
parent
5aa04ba366
commit
94cc221625
5
cache.go
5
cache.go
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
6
http.go
6
http.go
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue