Files
EdgeNode/internal/metrics/task_base.go
2024-04-02 19:54:04 +08:00

76 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package metrics
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"sync"
"sync/atomic"
)
type BaseTask struct {
itemConfig *serverconfigs.MetricItemConfig
isLoaded bool
isStopped bool
statsMap map[string]*Stat // 待写入队列hash => *Stat
statsLocker sync.RWMutex
}
// Add 添加数据
func (this *BaseTask) Add(obj MetricInterface) {
if this.isStopped || !this.isLoaded {
return
}
var keys = []string{}
for _, key := range this.itemConfig.Keys {
var k = obj.MetricKey(key)
// 忽略499状态
if key == "${status}" && k == "499" {
return
}
keys = append(keys, k)
}
v, ok := obj.MetricValue(this.itemConfig.Value)
if !ok {
return
}
var hash = UniqueKey(obj.MetricServerId(), keys, this.itemConfig.CurrentTime(), this.itemConfig.Version, this.itemConfig.Id)
var countItems int
this.statsLocker.RLock()
oldStat, ok := this.statsMap[hash]
if !ok {
countItems = len(this.statsMap)
}
this.statsLocker.RUnlock()
if ok {
atomic.AddInt64(&oldStat.Value, 1)
} else {
// 防止过载
if countItems < MaxQueueSize {
this.statsLocker.Lock()
this.statsMap[hash] = &Stat{
ServerId: obj.MetricServerId(),
Keys: keys,
Value: v,
Time: this.itemConfig.CurrentTime(),
Hash: hash,
}
this.statsLocker.Unlock()
}
}
}
func (this *BaseTask) Item() *serverconfigs.MetricItemConfig {
return this.itemConfig
}
func (this *BaseTask) SetItem(itemConfig *serverconfigs.MetricItemConfig) {
this.itemConfig = itemConfig
}