removed locks from memory bg operations

This commit is contained in:
Josh Baker 2017-09-30 08:16:34 -07:00
parent 4a3800596e
commit a99613fe23

View File

@ -182,8 +182,8 @@ func ListenAndServeEx(host string, port int, dir string, ln *net.Listener, http
c.followc.add(1) // this will force any follow communication to die c.followc.add(1) // this will force any follow communication to die
}() }()
go c.processLives() go c.processLives()
go c.watchMemory() go c.watchOutOfMemory()
go c.watchGC() go c.watchAutoGC()
go c.backgroundExpiring() go c.backgroundExpiring()
defer func() { defer func() {
c.stopBackgroundExpiring.set(true) c.stopBackgroundExpiring.set(true)
@ -250,29 +250,21 @@ func ListenAndServeEx(host string, port int, dir string, ln *net.Listener, http
return server.ListenAndServe(host, port, protected, handler, opened, closed, ln, http) return server.ListenAndServe(host, port, protected, handler, opened, closed, ln, http)
} }
func (c *Controller) watchGC() { func (c *Controller) watchAutoGC() {
t := time.NewTicker(time.Second) t := time.NewTicker(time.Second)
defer t.Stop() defer t.Stop()
s := time.Now() s := time.Now()
for range t.C { for range t.C {
c.mu.RLock()
if c.stopWatchingAutoGC.on() { if c.stopWatchingAutoGC.on() {
c.mu.RUnlock()
return return
} }
autoGC := c.config.autoGC()
c.mu.RUnlock() if autoGC == 0 {
if c.config.autoGC() == 0 {
continue continue
} }
if time.Now().Sub(s) < time.Second*time.Duration(autoGC) {
if time.Now().Sub(s) < time.Second*time.Duration(c.config.autoGC()) {
continue continue
} }
var mem1, mem2 runtime.MemStats var mem1, mem2 runtime.MemStats
runtime.ReadMemStats(&mem1) runtime.ReadMemStats(&mem1)
log.Debugf("autogc(before): "+ log.Debugf("autogc(before): "+
@ -281,7 +273,6 @@ func (c *Controller) watchGC() {
runtime.GC() runtime.GC()
debug.FreeOSMemory() debug.FreeOSMemory()
runtime.ReadMemStats(&mem2) runtime.ReadMemStats(&mem2)
log.Debugf("autogc(after): "+ log.Debugf("autogc(after): "+
"alloc: %v, heap_alloc: %v, heap_released: %v", "alloc: %v, heap_alloc: %v, heap_released: %v",
@ -290,7 +281,7 @@ func (c *Controller) watchGC() {
} }
} }
func (c *Controller) watchMemory() { func (c *Controller) watchOutOfMemory() {
t := time.NewTicker(time.Second * 2) t := time.NewTicker(time.Second * 2)
defer t.Stop() defer t.Stop()
var mem runtime.MemStats var mem runtime.MemStats