套餐增加Websocket连接数限制

This commit is contained in:
刘祥超
2023-12-19 14:56:44 +08:00
parent 16083fd0d7
commit e49db916f8
7 changed files with 406 additions and 293 deletions

View File

@@ -22,6 +22,8 @@ const (
PlanField_TotalServerNames dbs.FieldName = "totalServerNames" // 总域名数量
PlanField_MonthlyRequests dbs.FieldName = "monthlyRequests" // 每月访问量额度
PlanField_DailyRequests dbs.FieldName = "dailyRequests" // 每日访问量额度
PlanField_DailyWebsocketConnections dbs.FieldName = "dailyWebsocketConnections" // 每日Websocket连接数
PlanField_MonthlyWebsocketConnections dbs.FieldName = "monthlyWebsocketConnections" // 每月Websocket连接数
)
// Plan 用户套餐
@@ -45,6 +47,8 @@ type Plan struct {
TotalServerNames uint32 `field:"totalServerNames"` // 总域名数量
MonthlyRequests uint64 `field:"monthlyRequests"` // 每月访问量额度
DailyRequests uint64 `field:"dailyRequests"` // 每日访问量额度
DailyWebsocketConnections uint64 `field:"dailyWebsocketConnections"` // 每日Websocket连接数
MonthlyWebsocketConnections uint64 `field:"monthlyWebsocketConnections"` // 每月Websocket连接数
}
type PlanOperator struct {
@@ -67,6 +71,8 @@ type PlanOperator struct {
TotalServerNames any // 总域名数量
MonthlyRequests any // 每月访问量额度
DailyRequests any // 每日访问量额度
DailyWebsocketConnections any // 每日Websocket连接数
MonthlyWebsocketConnections any // 每月Websocket连接数
}
func NewPlanOperator() *PlanOperator {

View File

@@ -59,7 +59,7 @@ func init() {
// UpdateUserPlanBandwidth 写入数据
// 暂时不使用region区分
func (this *UserPlanBandwidthStatDAO) UpdateUserPlanBandwidth(tx *dbs.Tx, userId int64, userPlanId int64, regionId int64, day string, timeAt string, bandwidthBytes int64, totalBytes int64, cachedBytes int64, attackBytes int64, countRequests int64, countCachedRequests int64, countAttackRequests int64) error {
func (this *UserPlanBandwidthStatDAO) UpdateUserPlanBandwidth(tx *dbs.Tx, userId int64, userPlanId int64, regionId int64, day string, timeAt string, bandwidthBytes int64, totalBytes int64, cachedBytes int64, attackBytes int64, countRequests int64, countCachedRequests int64, countAttackRequests int64, countWebsocketConnections int64) error {
if userId <= 0 || userPlanId <= 0 {
return nil
}
@@ -73,6 +73,7 @@ func (this *UserPlanBandwidthStatDAO) UpdateUserPlanBandwidth(tx *dbs.Tx, userId
Param("countRequests", countRequests).
Param("countCachedRequests", countCachedRequests).
Param("countAttackRequests", countAttackRequests).
Param("countWebsocketConnections", countWebsocketConnections).
InsertOrUpdateQuickly(maps.Map{
"userId": userId,
"userPlanId": userPlanId,
@@ -87,6 +88,7 @@ func (this *UserPlanBandwidthStatDAO) UpdateUserPlanBandwidth(tx *dbs.Tx, userId
"countRequests": countRequests,
"countCachedRequests": countCachedRequests,
"countAttackRequests": countAttackRequests,
"countWebsocketConnections": countWebsocketConnections,
}, maps.Map{
"bytes": dbs.SQL("bytes+:bytes"),
"avgBytes": dbs.SQL("(totalBytes+:totalBytes)/300"), // 因为生成SQL语句时会自动将avgBytes排在totalBytes之前所以这里不用担心先后顺序的问题
@@ -96,6 +98,7 @@ func (this *UserPlanBandwidthStatDAO) UpdateUserPlanBandwidth(tx *dbs.Tx, userId
"countRequests": dbs.SQL("countRequests+:countRequests"),
"countCachedRequests": dbs.SQL("countCachedRequests+:countCachedRequests"),
"countAttackRequests": dbs.SQL("countAttackRequests+:countAttackRequests"),
"countWebsocketConnections": dbs.SQL("countWebsocketConnections+:countWebsocketConnections"),
})
}

View File

@@ -17,6 +17,7 @@ const (
UserPlanBandwidthStatField_CountRequests dbs.FieldName = "countRequests" // 请求数
UserPlanBandwidthStatField_CountCachedRequests dbs.FieldName = "countCachedRequests" // 缓存的请求数
UserPlanBandwidthStatField_CountAttackRequests dbs.FieldName = "countAttackRequests" // 攻击请求数
UserPlanBandwidthStatField_CountWebsocketConnections dbs.FieldName = "countWebsocketConnections" // Websocket连接数
)
// UserPlanBandwidthStat 用户套餐带宽峰值
@@ -35,6 +36,7 @@ type UserPlanBandwidthStat struct {
CountRequests uint64 `field:"countRequests"` // 请求数
CountCachedRequests uint64 `field:"countCachedRequests"` // 缓存的请求数
CountAttackRequests uint64 `field:"countAttackRequests"` // 攻击请求数
CountWebsocketConnections uint64 `field:"countWebsocketConnections"` // Websocket连接数
}
type UserPlanBandwidthStatOperator struct {
@@ -52,6 +54,7 @@ type UserPlanBandwidthStatOperator struct {
CountRequests any // 请求数
CountCachedRequests any // 缓存的请求数
CountAttackRequests any // 攻击请求数
CountWebsocketConnections any // Websocket连接数
}
func NewUserPlanBandwidthStatOperator() *UserPlanBandwidthStatOperator {

View File

@@ -5,6 +5,6 @@ package models
import "github.com/iwind/TeaGo/dbs"
func (this *UserPlanStatDAO) IncreaseUserPlanStat(tx *dbs.Tx, userPlanId int64, trafficBytes int64, countRequests int64) error {
func (this *UserPlanStatDAO) IncreaseUserPlanStat(tx *dbs.Tx, userPlanId int64, trafficBytes int64, countRequests int64, countWebsocketConnections int64) error {
return nil
}

View File

@@ -9,6 +9,7 @@ const (
UserPlanStatField_DateType dbs.FieldName = "dateType" // 日期类型day|month
UserPlanStatField_TrafficBytes dbs.FieldName = "trafficBytes" // 流量
UserPlanStatField_CountRequests dbs.FieldName = "countRequests" // 总请求数
UserPlanStatField_CountWebsocketConnections dbs.FieldName = "countWebsocketConnections" // Websocket连接数
UserPlanStatField_IsProcessed dbs.FieldName = "isProcessed" // 是否已处理
)
@@ -20,6 +21,7 @@ type UserPlanStat struct {
DateType string `field:"dateType"` // 日期类型day|month
TrafficBytes uint64 `field:"trafficBytes"` // 流量
CountRequests uint64 `field:"countRequests"` // 总请求数
CountWebsocketConnections uint64 `field:"countWebsocketConnections"` // Websocket连接数
IsProcessed bool `field:"isProcessed"` // 是否已处理
}
@@ -30,6 +32,7 @@ type UserPlanStatOperator struct {
DateType any // 日期类型day|month
TrafficBytes any // 流量
CountRequests any // 总请求数
CountWebsocketConnections any // Websocket连接数
IsProcessed any // 是否已处理
}

View File

@@ -80,13 +80,13 @@ func init() {
// 套餐统计
if stat.UserPlanId > 0 {
// 总体统计
err = models.SharedUserPlanStatDAO.IncreaseUserPlanStat(tx, stat.UserPlanId, stat.TotalBytes, stat.CountRequests)
err = models.SharedUserPlanStatDAO.IncreaseUserPlanStat(tx, stat.UserPlanId, stat.TotalBytes, stat.CountRequests, stat.CountWebsocketConnections)
if err != nil {
remotelogs.Error("ServerBandwidthStatService", "IncreaseUserPlanStat: "+err.Error())
}
// 分时统计
err = models.SharedUserPlanBandwidthStatDAO.UpdateUserPlanBandwidth(tx, stat.UserId, stat.UserPlanId, stat.NodeRegionId, stat.Day, stat.TimeAt, stat.Bytes, stat.TotalBytes, stat.CachedBytes, stat.AttackBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests)
err = models.SharedUserPlanBandwidthStatDAO.UpdateUserPlanBandwidth(tx, stat.UserId, stat.UserPlanId, stat.NodeRegionId, stat.Day, stat.TimeAt, stat.Bytes, stat.TotalBytes, stat.CachedBytes, stat.AttackBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests, stat.CountWebsocketConnections)
}
}
@@ -146,6 +146,7 @@ func (this *ServerBandwidthStatService) UploadServerBandwidthStats(ctx context.C
oldStat.CountRequests += stat.CountRequests
oldStat.CountCachedRequests += stat.CountCachedRequests
oldStat.CountAttackRequests += stat.CountAttackRequests
oldStat.CountWebsocketConnections += stat.CountWebsocketConnections
} else {
serverBandwidthStatsMap[key] = &pb.ServerBandwidthStat{
Id: 0,
@@ -161,6 +162,7 @@ func (this *ServerBandwidthStatService) UploadServerBandwidthStats(ctx context.C
CountRequests: stat.CountRequests,
CountCachedRequests: stat.CountCachedRequests,
CountAttackRequests: stat.CountAttackRequests,
CountWebsocketConnections: stat.CountWebsocketConnections,
UserPlanId: stat.UserPlanId,
}
}

File diff suppressed because it is too large Load Diff