Files
EdgeNode/internal/stats/traffic_stat_manager.go

130 lines
3.2 KiB
Go
Raw Normal View History

2021-01-25 16:40:31 +08:00
package stats
import (
2021-01-25 16:40:31 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
2021-01-25 16:40:31 +08:00
"github.com/TeaOSLab/EdgeNode/internal/events"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/rpc"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/Tea"
"strconv"
"sync"
"time"
)
var SharedTrafficStatManager = NewTrafficStatManager()
2021-06-08 11:24:41 +08:00
type TrafficItem struct {
Bytes int64
CachedBytes int64
CountRequests int64
CountCachedRequests int64
}
2021-04-29 16:48:47 +08:00
// TrafficStatManager 区域流量统计
type TrafficStatManager struct {
2021-06-08 11:24:41 +08:00
itemMap map[string]*TrafficItem // [timestamp serverId] => bytes
2021-01-25 16:40:31 +08:00
locker sync.Mutex
configFunc func() *nodeconfigs.NodeConfig
}
2021-04-29 16:48:47 +08:00
// NewTrafficStatManager 获取新对象
func NewTrafficStatManager() *TrafficStatManager {
manager := &TrafficStatManager{
2021-06-08 11:24:41 +08:00
itemMap: map[string]*TrafficItem{},
}
return manager
}
2021-04-29 16:48:47 +08:00
// Start 启动自动任务
2021-01-25 16:40:31 +08:00
func (this *TrafficStatManager) Start(configFunc func() *nodeconfigs.NodeConfig) {
this.configFunc = configFunc
duration := 5 * time.Minute
if Tea.IsTesting() {
// 测试环境缩短上传时间,方便我们调试
duration = 30 * time.Second
}
ticker := time.NewTicker(duration)
2021-01-25 16:40:31 +08:00
events.On(events.EventQuit, func() {
remotelogs.Println("TRAFFIC_STAT_MANAGER", "quit")
ticker.Stop()
})
2021-01-26 18:42:46 +08:00
remotelogs.Println("TRAFFIC_STA_MANAGER", "start ...")
for range ticker.C {
err := this.Upload()
if err != nil {
remotelogs.Error("TRAFFIC_STAT_MANAGER", "upload stats failed: "+err.Error())
}
}
}
2021-04-29 16:48:47 +08:00
// Add 添加流量
2021-06-08 11:24:41 +08:00
func (this *TrafficStatManager) Add(serverId int64, bytes int64, cachedBytes int64, countRequests int64, countCachedRequests int64) {
if bytes == 0 {
return
}
timestamp := utils.UnixTime() / 300 * 300
key := strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10)
this.locker.Lock()
2021-06-08 11:24:41 +08:00
item, ok := this.itemMap[key]
if !ok {
item = &TrafficItem{}
this.itemMap[key] = item
}
item.Bytes += bytes
item.CachedBytes += cachedBytes
item.CountRequests += countRequests
item.CountCachedRequests += countCachedRequests
this.locker.Unlock()
}
2021-04-29 16:48:47 +08:00
// Upload 上传流量
func (this *TrafficStatManager) Upload() error {
2021-01-25 16:40:31 +08:00
config := this.configFunc()
if config == nil {
return nil
}
client, err := rpc.SharedRPC()
if err != nil {
return err
}
this.locker.Lock()
2021-06-08 11:24:41 +08:00
m := this.itemMap
this.itemMap = map[string]*TrafficItem{}
this.locker.Unlock()
pbStats := []*pb.ServerDailyStat{}
2021-06-08 11:24:41 +08:00
for key, item := range m {
timestamp, err := strconv.ParseInt(key[:10], 10, 64)
if err != nil {
return err
}
serverId, err := strconv.ParseInt(key[10:], 10, 64)
if err != nil {
return err
}
pbStats = append(pbStats, &pb.ServerDailyStat{
2021-06-08 11:24:41 +08:00
ServerId: serverId,
RegionId: config.RegionId,
Bytes: item.Bytes,
CachedBytes: item.CachedBytes,
CountRequests: item.CountRequests,
CountCachedRequests: item.CountCachedRequests,
CreatedAt: timestamp,
})
}
if len(pbStats) == 0 {
return nil
}
_, err = client.ServerDailyStatRPC().UploadServerDailyStats(client.Context(), &pb.UploadServerDailyStatsRequest{Stats: pbStats})
return err
}