replace zxq.co/ripple/hanayo
This commit is contained in:
58
vendor/github.com/gin-gonic/gin/examples/realtime-advanced/stats.go
generated
vendored
Normal file
58
vendor/github.com/gin-gonic/gin/examples/realtime-advanced/stats.go
generated
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/manucorporat/stats"
|
||||
)
|
||||
|
||||
var (
|
||||
ips = stats.New()
|
||||
messages = stats.New()
|
||||
users = stats.New()
|
||||
mutexStats sync.RWMutex
|
||||
savedStats map[string]uint64
|
||||
)
|
||||
|
||||
func statsWorker() {
|
||||
c := time.Tick(1 * time.Second)
|
||||
var lastMallocs uint64
|
||||
var lastFrees uint64
|
||||
for range c {
|
||||
var stats runtime.MemStats
|
||||
runtime.ReadMemStats(&stats)
|
||||
|
||||
mutexStats.Lock()
|
||||
savedStats = map[string]uint64{
|
||||
"timestamp": uint64(time.Now().Unix()),
|
||||
"HeapInuse": stats.HeapInuse,
|
||||
"StackInuse": stats.StackInuse,
|
||||
"Mallocs": (stats.Mallocs - lastMallocs),
|
||||
"Frees": (stats.Frees - lastFrees),
|
||||
"Inbound": uint64(messages.Get("inbound")),
|
||||
"Outbound": uint64(messages.Get("outbound")),
|
||||
"Connected": connectedUsers(),
|
||||
}
|
||||
lastMallocs = stats.Mallocs
|
||||
lastFrees = stats.Frees
|
||||
messages.Reset()
|
||||
mutexStats.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func connectedUsers() uint64 {
|
||||
connected := users.Get("connected") - users.Get("disconnected")
|
||||
if connected < 0 {
|
||||
return 0
|
||||
}
|
||||
return uint64(connected)
|
||||
}
|
||||
|
||||
func Stats() map[string]uint64 {
|
||||
mutexStats.RLock()
|
||||
defer mutexStats.RUnlock()
|
||||
|
||||
return savedStats
|
||||
}
|
Reference in New Issue
Block a user