Consolidate getMemoryStats (#1122)

* Consolidate getMemoryStats

* Avoid divide-by-0
This commit is contained in:
Raja Subramanian
2022-10-26 09:16:39 +05:30
committed by GitHub
parent 7a7fc09372
commit 1e8cc0dc76
4 changed files with 33 additions and 22 deletions
+20
View File
@@ -2,6 +2,7 @@ package routing
import (
"context"
"runtime"
"sync"
"time"
@@ -171,6 +172,7 @@ func (r *LocalRouter) Start() error {
return nil
}
go r.statsWorker()
go r.memStatsWorker()
// on local routers, Start doesn't do anything, websocket connections initiate the connections
go r.rtcMessageWorker()
return nil
@@ -201,6 +203,24 @@ func (r *LocalRouter) statsWorker() {
}
}
func (r *LocalRouter) memStatsWorker() {
ticker := time.NewTicker(time.Second * 30)
defer ticker.Stop()
for {
<-ticker.C
var m runtime.MemStats
runtime.ReadMemStats(&m)
logger.Infow("memstats",
"mallocs", m.Mallocs, "frees", m.Frees, "m-f", m.Mallocs-m.Frees,
"hinuse", m.HeapInuse, "halloc", m.HeapAlloc, "frag", m.HeapInuse-m.HeapAlloc,
)
runtime.GC()
}
}
func (r *LocalRouter) rtcMessageWorker() {
// is a new channel available? if so swap to that one
if !r.isStarted.Load() {