上传带宽数据时同时上传流量数据

This commit is contained in:
GoEdgeLab
2023-02-27 10:48:16 +08:00
parent 53647ab343
commit c6e8052b5f
3 changed files with 16 additions and 12 deletions

View File

@@ -180,7 +180,6 @@ func (this *ClientConn) Write(b []byte) (n int, err error) {
var before = time.Now() var before = time.Now()
n, err = this.rawConn.Write(b) n, err = this.rawConn.Write(b)
if n > 0 { if n > 0 {
// 统计当前服务带宽 // 统计当前服务带宽
if this.serverId > 0 { if this.serverId > 0 {
if !this.isLO || Tea.IsTesting() { // 环路不统计带宽,避免缓存预热等行为产生带宽 if !this.isLO || Tea.IsTesting() { // 环路不统计带宽,避免缓存预热等行为产生带宽
@@ -188,9 +187,9 @@ func (this *ClientConn) Write(b []byte) (n int, err error) {
var cost = time.Since(before).Seconds() var cost = time.Since(before).Seconds()
if cost > 1 { if cost > 1 {
stats.SharedBandwidthStatManager.Add(this.userId, this.serverId, int64(float64(n)/cost)) stats.SharedBandwidthStatManager.Add(this.userId, this.serverId, int64(float64(n)/cost), int64(n))
} else { } else {
stats.SharedBandwidthStatManager.Add(this.userId, this.serverId, int64(n)) stats.SharedBandwidthStatManager.Add(this.userId, this.serverId, int64(n), int64(n))
} }
} }
} }

View File

@@ -404,7 +404,7 @@ func NewUDPConn(server *serverconfigs.ServerConfig, addr net.Addr, proxyListener
stats.SharedTrafficStatManager.Add(server.Id, "", int64(n), 0, 0, 0, 0, 0, server.ShouldCheckTrafficLimit(), server.PlanId()) stats.SharedTrafficStatManager.Add(server.Id, "", int64(n), 0, 0, 0, 0, 0, server.ShouldCheckTrafficLimit(), server.PlanId())
// 带宽 // 带宽
stats.SharedBandwidthStatManager.Add(server.UserId, server.Id, int64(n)) stats.SharedBandwidthStatManager.Add(server.UserId, server.Id, int64(n), int64(n))
} }
} }
if err != nil { if err != nil {

View File

@@ -37,6 +37,7 @@ type BandwidthStat struct {
CurrentBytes int64 CurrentBytes int64
CurrentTimestamp int64 CurrentTimestamp int64
MaxBytes int64 MaxBytes int64
TotalBytes int64
} }
// BandwidthStatManager 服务带宽统计 // BandwidthStatManager 服务带宽统计
@@ -107,6 +108,7 @@ func (this *BandwidthStatManager) Loop() error {
Day: stat.Day, Day: stat.Day,
TimeAt: stat.TimeAt, TimeAt: stat.TimeAt,
Bytes: stat.MaxBytes / bandwidthTimestampDelim, Bytes: stat.MaxBytes / bandwidthTimestampDelim,
TotalBytes: stat.TotalBytes,
NodeRegionId: regionId, NodeRegionId: regionId,
}) })
delete(this.m, key) delete(this.m, key)
@@ -132,8 +134,8 @@ func (this *BandwidthStatManager) Loop() error {
} }
// Add 添加带宽数据 // Add 添加带宽数据
func (this *BandwidthStatManager) Add(userId int64, serverId int64, bytes int64) { func (this *BandwidthStatManager) Add(userId int64, serverId int64, peekBytes int64, totalBytes int64) {
if serverId <= 0 || bytes == 0 { if serverId <= 0 || (peekBytes == 0 && totalBytes == 0) {
return return
} }
@@ -146,8 +148,8 @@ func (this *BandwidthStatManager) Add(userId int64, serverId int64, bytes int64)
// 增加TCP Header尺寸这里默认MTU为1500且默认为IPv4 // 增加TCP Header尺寸这里默认MTU为1500且默认为IPv4
const mtu = 1500 const mtu = 1500
const tcpHeaderSize = 20 const tcpHeaderSize = 20
if bytes > mtu { if peekBytes > mtu {
bytes += bytes * tcpHeaderSize / mtu peekBytes += peekBytes * tcpHeaderSize / mtu
} }
this.locker.Lock() this.locker.Lock()
@@ -156,22 +158,25 @@ func (this *BandwidthStatManager) Add(userId int64, serverId int64, bytes int64)
// 此刻如果发生用户IDuserId的变化也忽略等N分钟后有新记录后再换 // 此刻如果发生用户IDuserId的变化也忽略等N分钟后有新记录后再换
if stat.CurrentTimestamp == timestamp { if stat.CurrentTimestamp == timestamp {
stat.CurrentBytes += bytes stat.CurrentBytes += peekBytes
} else { } else {
stat.CurrentBytes = bytes stat.CurrentBytes = peekBytes
stat.CurrentTimestamp = timestamp stat.CurrentTimestamp = timestamp
} }
if stat.CurrentBytes > stat.MaxBytes { if stat.CurrentBytes > stat.MaxBytes {
stat.MaxBytes = stat.CurrentBytes stat.MaxBytes = stat.CurrentBytes
} }
stat.TotalBytes += totalBytes
} else { } else {
this.m[key] = &BandwidthStat{ this.m[key] = &BandwidthStat{
Day: day, Day: day,
TimeAt: timeAt, TimeAt: timeAt,
UserId: userId, UserId: userId,
ServerId: serverId, ServerId: serverId,
CurrentBytes: bytes, CurrentBytes: peekBytes,
MaxBytes: bytes, MaxBytes: peekBytes,
TotalBytes: totalBytes,
CurrentTimestamp: timestamp, CurrentTimestamp: timestamp,
} }
} }