1 Star 0 Fork 0

magicianlyx / GoLog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
latency_map.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
magicianlyx 提交于 2022-01-24 17:45 . -
package pool
import (
"sync"
"time"
)
// 多状态总结(线程安全)
type LatencyMap struct {
l sync.RWMutex
m map[GoroutineStatus]*Latency
}
func NewLatencyMap() *LatencyMap {
return &LatencyMap{
sync.RWMutex{},
make(map[GoroutineStatus]*Latency),
}
}
func (g *LatencyMap) getOrCreate(status GoroutineStatus) *Latency {
if v, ok := g.m[status]; ok {
return v
} else {
l := NewLatency(status)
g.m[status] = l
return l
}
}
func (g *LatencyMap) set(status GoroutineStatus, l *Latency) {
g.m[status] = l
}
func (g *LatencyMap) Set(status GoroutineStatus, l *Latency) {
g.l.Lock()
defer g.l.Unlock()
g.set(status, l)
}
func (g *LatencyMap) getAll() map[GoroutineStatus]time.Duration {
r := make(map[GoroutineStatus]time.Duration)
for status := range g.m {
latency := g.m[status]
latency = latency.AmountDurationOfNow()
r[status] = latency.amount
}
return r
}
func (g *LatencyMap) GetAll() map[GoroutineStatus]time.Duration {
g.l.RLock()
defer g.l.RUnlock()
return g.getAll()
}
func (g *LatencyMap) Clone() *LatencyMap {
g.l.RLock()
defer g.l.RUnlock()
r := NewLatencyMap()
for status := range g.m {
latency := g.m[status]
latency = latency.AmountDurationOfNow()
r.set(status, latency)
}
return r
}
func (g *LatencyMap) IsStart(status GoroutineStatus) bool {
g.l.Lock()
defer g.l.Unlock()
return g.getOrCreate(status).IsStart()
}
func (g *LatencyMap) Start(status GoroutineStatus) {
g.l.Lock()
defer g.l.Unlock()
g.getOrCreate(status).Start()
}
func (g *LatencyMap) Stop(status GoroutineStatus) {
g.l.Lock()
defer g.l.Unlock()
g.getOrCreate(status).Stop()
}
Go
1
https://gitee.com/magicianlyx/GoLog.git
git@gitee.com:magicianlyx/GoLog.git
magicianlyx
GoLog
GoLog
20c45f9b998d

搜索帮助

53164aa7 5694891 3bd8fe86 5694891