mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-06 01:50:25 +08:00
改进流量限制
This commit is contained in:
@@ -729,7 +729,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap maps.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, server := range servers {
|
for _, server := range servers {
|
||||||
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap)
|
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server, cacheMap, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -210,6 +213,40 @@ func (this *PlanDAO) SortPlans(tx *dbs.Tx, planIds []int64) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindEnabledPlanTrafficLimit 获取套餐的流量限制
|
||||||
|
func (this *PlanDAO) FindEnabledPlanTrafficLimit(tx *dbs.Tx, planId int64, cacheMap maps.Map) (*serverconfigs.TrafficLimitConfig, error) {
|
||||||
|
var cacheKey = this.Table + ":FindEnabledPlanTrafficLimit:" + types.String(planId)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap[cacheKey]
|
||||||
|
if ok {
|
||||||
|
return cache.(*serverconfigs.TrafficLimitConfig), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trafficLimit, err := this.Query(tx).
|
||||||
|
Pk(planId).
|
||||||
|
State(PlanStateEnabled).
|
||||||
|
Result("trafficLimit").
|
||||||
|
FindStringCol("")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(trafficLimit) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
var config = &serverconfigs.TrafficLimitConfig{}
|
||||||
|
err = json.Unmarshal([]byte(trafficLimit), config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap[cacheKey] = config
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
// NotifyUpdate 通知变更
|
// NotifyUpdate 通知变更
|
||||||
func (this *PlanDAO) NotifyUpdate(tx *dbs.Tx, planId int64) error {
|
func (this *PlanDAO) NotifyUpdate(tx *dbs.Tx, planId int64) error {
|
||||||
// 这里不要加入状态参数,因为需要适应删除后的更新
|
// 这里不要加入状态参数,因为需要适应删除后的更新
|
||||||
|
|||||||
@@ -105,15 +105,22 @@ func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailySta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新流量限制状态
|
// 更新流量限制状态
|
||||||
trafficLimit, err := SharedServerDAO.FindServerTrafficLimitConfig(tx, stat.ServerId, cacheMap)
|
if stat.CheckTrafficLimiting {
|
||||||
|
trafficLimitConfig, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, stat.ServerId, cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if trafficLimit != nil && trafficLimit.IsOn && !trafficLimit.IsEmpty() {
|
if trafficLimitConfig != nil && trafficLimitConfig.IsOn && !trafficLimitConfig.IsEmpty() {
|
||||||
err = SharedServerDAO.UpdateServerTrafficLimitStatus(tx, trafficLimit, stat.ServerId, false)
|
err = SharedServerDAO.IncreaseServerTotalTraffic(tx, stat.ServerId, stat.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = SharedServerDAO.UpdateServerTrafficLimitStatus(tx, trafficLimitConfig, stat.ServerId, false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
@@ -827,7 +826,7 @@ func (this *ServerDAO) FindServerNodeFilters(tx *dbs.Tx, serverId int64) (isOk b
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ComposeServerConfigWithServerId 构造服务的Config
|
// ComposeServerConfigWithServerId 构造服务的Config
|
||||||
func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int64) (*serverconfigs.ServerConfig, error) {
|
func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int64, forNode bool) (*serverconfigs.ServerConfig, error) {
|
||||||
server, err := this.FindEnabledServer(tx, serverId)
|
server, err := this.FindEnabledServer(tx, serverId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -835,11 +834,12 @@ func (this *ServerDAO) ComposeServerConfigWithServerId(tx *dbs.Tx, serverId int6
|
|||||||
if server == nil {
|
if server == nil {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
return this.ComposeServerConfig(tx, server, nil)
|
return this.ComposeServerConfig(tx, server, nil, forNode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ComposeServerConfig 构造服务的Config
|
// ComposeServerConfig 构造服务的Config
|
||||||
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap maps.Map) (*serverconfigs.ServerConfig, error) {
|
// forNode 是否是节点请求
|
||||||
|
func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap maps.Map, forNode bool) (*serverconfigs.ServerConfig, error) {
|
||||||
if server == nil {
|
if server == nil {
|
||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
@@ -1039,24 +1039,11 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.TrafficLimit = trafficLimitConfig
|
config.TrafficLimit = trafficLimitConfig
|
||||||
|
|
||||||
if trafficLimitConfig.IsOn && !trafficLimitConfig.IsEmpty() {
|
|
||||||
if len(server.TrafficLimitStatus) > 0 {
|
|
||||||
var status = &serverconfigs.TrafficLimitStatus{}
|
|
||||||
err = json.Unmarshal([]byte(server.TrafficLimitStatus), status)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if status.IsValid() {
|
|
||||||
config.TrafficLimitStatus = status
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 用户套餐
|
// 用户套餐
|
||||||
if server.UserPlanId > 0 {
|
if forNode && server.UserPlanId > 0 {
|
||||||
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, int64(server.UserPlanId))
|
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, int64(server.UserPlanId), cacheMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1087,6 +1074,19 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.TrafficLimit != nil && config.TrafficLimit.IsOn && !config.TrafficLimit.IsEmpty() {
|
||||||
|
if len(server.TrafficLimitStatus) > 0 {
|
||||||
|
var status = &serverconfigs.TrafficLimitStatus{}
|
||||||
|
err = json.Unmarshal([]byte(server.TrafficLimitStatus), status)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if status.IsValid() {
|
||||||
|
config.TrafficLimitStatus = status
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1819,30 +1819,106 @@ func (this *ServerDAO) FindServerTrafficLimitConfig(tx *dbs.Tx, serverId int64,
|
|||||||
return result.(*serverconfigs.TrafficLimitConfig), nil
|
return result.(*serverconfigs.TrafficLimitConfig), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
trafficLimit, err := this.Query(tx).
|
serverOne, err := this.Query(tx).
|
||||||
Pk(serverId).
|
Pk(serverId).
|
||||||
Result("trafficLimit").
|
Result("trafficLimit").
|
||||||
FindStringCol("")
|
Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var limit = &serverconfigs.TrafficLimitConfig{}
|
var limit = &serverconfigs.TrafficLimitConfig{}
|
||||||
if len(trafficLimit) == 0 {
|
if serverOne == nil {
|
||||||
return limit, nil
|
return limit, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var trafficLimit = serverOne.(*Server).TrafficLimit
|
||||||
|
|
||||||
err = json.Unmarshal([]byte(trafficLimit), limit)
|
err = json.Unmarshal([]byte(trafficLimit), limit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO 套餐
|
|
||||||
|
|
||||||
cacheMap[cacheKey] = limit
|
cacheMap[cacheKey] = limit
|
||||||
|
|
||||||
return limit, nil
|
return limit, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CalculateServerTrafficLimitConfig 计算服务的流量限制
|
||||||
|
// TODO 优化性能
|
||||||
|
func (this *ServerDAO) CalculateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, cacheMap maps.Map) (*serverconfigs.TrafficLimitConfig, error) {
|
||||||
|
if cacheMap == nil {
|
||||||
|
cacheMap = maps.Map{}
|
||||||
|
}
|
||||||
|
var cacheKey = this.Table + ":FindServerTrafficLimitConfig:" + types.String(serverId)
|
||||||
|
result, ok := cacheMap[cacheKey]
|
||||||
|
if ok {
|
||||||
|
return result.(*serverconfigs.TrafficLimitConfig), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
serverOne, err := this.Query(tx).
|
||||||
|
Pk(serverId).
|
||||||
|
Result("trafficLimit", "userPlanId").
|
||||||
|
Find()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var limitConfig = &serverconfigs.TrafficLimitConfig{}
|
||||||
|
if serverOne == nil {
|
||||||
|
return limitConfig, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var trafficLimit = serverOne.(*Server).TrafficLimit
|
||||||
|
var userPlanId = int64(serverOne.(*Server).UserPlanId)
|
||||||
|
|
||||||
|
if len(trafficLimit) == 0 {
|
||||||
|
if userPlanId > 0 {
|
||||||
|
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if userPlan != nil {
|
||||||
|
planLimit, err := SharedPlanDAO.FindEnabledPlanTrafficLimit(tx, int64(userPlan.PlanId), cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if planLimit != nil {
|
||||||
|
return planLimit, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return limitConfig, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal([]byte(trafficLimit), limitConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !limitConfig.IsOn {
|
||||||
|
if userPlanId > 0 {
|
||||||
|
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if userPlan != nil {
|
||||||
|
planLimit, err := SharedPlanDAO.FindEnabledPlanTrafficLimit(tx, int64(userPlan.PlanId), cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if planLimit != nil {
|
||||||
|
return planLimit, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheMap[cacheKey] = limitConfig
|
||||||
|
|
||||||
|
return limitConfig, nil
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateServerTrafficLimitConfig 修改服务的流量限制
|
// UpdateServerTrafficLimitConfig 修改服务的流量限制
|
||||||
func (this *ServerDAO) UpdateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, trafficLimitConfig *serverconfigs.TrafficLimitConfig) error {
|
func (this *ServerDAO) UpdateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64, trafficLimitConfig *serverconfigs.TrafficLimitConfig) error {
|
||||||
if serverId <= 0 {
|
if serverId <= 0 {
|
||||||
@@ -1873,16 +1949,22 @@ func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitCo
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
oldStatusString, err := this.Query(tx).
|
serverOne, err := this.Query(tx).
|
||||||
Pk(serverId).
|
Pk(serverId).
|
||||||
Result("trafficLimitStatus").
|
Result("trafficLimitStatus", "totalTraffic", "totalDailyTraffic", "totalMonthlyTraffic", "trafficDay", "trafficMonth").
|
||||||
FindStringCol("")
|
Find()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if serverOne == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var server = serverOne.(*Server)
|
||||||
|
|
||||||
var oldStatus = &serverconfigs.TrafficLimitStatus{}
|
var oldStatus = &serverconfigs.TrafficLimitStatus{}
|
||||||
if len(oldStatusString) > 0 {
|
if len(server.TrafficLimitStatus) > 0 {
|
||||||
err = json.Unmarshal([]byte(oldStatusString), oldStatus)
|
err = json.Unmarshal([]byte(server.TrafficLimitStatus), oldStatus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1897,38 +1979,22 @@ func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitCo
|
|||||||
|
|
||||||
// daily
|
// daily
|
||||||
if trafficLimitConfig.DailyBytes() > 0 {
|
if trafficLimitConfig.DailyBytes() > 0 {
|
||||||
stat, err := SharedServerDailyStatDAO.SumDailyStat(tx, serverId, timeutil.Format("Ymd"))
|
if server.TrafficDay == timeutil.Format("Ymd") && server.TotalDailyTraffic >= float64(trafficLimitConfig.DailyBytes())/1024/1024/1024 {
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if stat != nil && stat.Bytes >= trafficLimitConfig.DailyBytes() {
|
|
||||||
untilDay = timeutil.Format("Ymd")
|
untilDay = timeutil.Format("Ymd")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// monthly
|
// monthly
|
||||||
if trafficLimitConfig.MonthlyBytes() > 0 {
|
if server.TrafficMonth == timeutil.Format("Ym") && trafficLimitConfig.MonthlyBytes() > 0 {
|
||||||
stat, err := SharedServerDailyStatDAO.SumMonthlyStat(tx, serverId, timeutil.Format("Ym"))
|
if server.TotalMonthlyTraffic >= float64(trafficLimitConfig.MonthlyBytes())/1024/1024/1024 {
|
||||||
if err != nil {
|
untilDay = timeutil.Format("Ym32")
|
||||||
return err
|
|
||||||
}
|
|
||||||
if stat != nil && stat.Bytes >= trafficLimitConfig.MonthlyBytes() {
|
|
||||||
untilDay = timeutil.Format("Ym") + fmt.Sprintf("%02d", types.Int(timeutil.Format("t")))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// totally
|
// totally
|
||||||
if trafficLimitConfig.TotalBytes() > 0 {
|
if trafficLimitConfig.TotalBytes() > 0 {
|
||||||
totalTraffic, err := this.Query(tx).
|
if server.TotalTraffic >= float64(trafficLimitConfig.TotalBytes())/1024/1024/1024 {
|
||||||
Pk(serverId).
|
untilDay = "30000101"
|
||||||
Result("totalTraffic").
|
|
||||||
FindFloat64Col(0)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if totalTraffic >= float64(trafficLimitConfig.TotalBytes()) {
|
|
||||||
untilDay = "20990101"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1958,9 +2024,17 @@ func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitCo
|
|||||||
// IncreaseServerTotalTraffic 增加服务的总流量
|
// IncreaseServerTotalTraffic 增加服务的总流量
|
||||||
func (this *ServerDAO) IncreaseServerTotalTraffic(tx *dbs.Tx, serverId int64, bytes int64) error {
|
func (this *ServerDAO) IncreaseServerTotalTraffic(tx *dbs.Tx, serverId int64, bytes int64) error {
|
||||||
var gb = float64(bytes) / 1024 / 1024 / 1024
|
var gb = float64(bytes) / 1024 / 1024 / 1024
|
||||||
|
var day = timeutil.Format("Ymd")
|
||||||
|
var month = timeutil.Format("Ym")
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(serverId).
|
Pk(serverId).
|
||||||
|
Set("totalDailyTraffic", dbs.SQL("IF(trafficDay=:day, totalDailyTraffic, 0)+:trafficGB")).
|
||||||
|
Set("totalMonthlyTraffic", dbs.SQL("IF(trafficMonth=:month, totalMonthlyTraffic, 0)+:trafficGB")).
|
||||||
Set("totalTraffic", dbs.SQL("totalTraffic+:trafficGB")).
|
Set("totalTraffic", dbs.SQL("totalTraffic+:trafficGB")).
|
||||||
|
Set("trafficDay", day).
|
||||||
|
Set("trafficMonth", month).
|
||||||
|
Param("day", day).
|
||||||
|
Param("month", month).
|
||||||
Param("trafficGB", gb).
|
Param("trafficGB", gb).
|
||||||
UpdateQuickly()
|
UpdateQuickly()
|
||||||
|
|
||||||
@@ -1970,7 +2044,8 @@ func (this *ServerDAO) IncreaseServerTotalTraffic(tx *dbs.Tx, serverId int64, by
|
|||||||
func (this *ServerDAO) ResetServerTotalTraffic(tx *dbs.Tx, serverId int64) error {
|
func (this *ServerDAO) ResetServerTotalTraffic(tx *dbs.Tx, serverId int64) error {
|
||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
Pk(serverId).
|
Pk(serverId).
|
||||||
Set("totalTraffic", 0).
|
Set("totalDailyTraffic", 0).
|
||||||
|
Set("totalMonthlyTraffic", 0).
|
||||||
UpdateQuickly()
|
UpdateQuickly()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1994,7 +2069,7 @@ func (this *ServerDAO) UpdateServersClusterIdWithPlanId(tx *dbs.Tx, planId int64
|
|||||||
|
|
||||||
// UpdateServerUserPlanId 设置服务所属套餐
|
// UpdateServerUserPlanId 设置服务所属套餐
|
||||||
func (this *ServerDAO) UpdateServerUserPlanId(tx *dbs.Tx, serverId int64, userPlanId int64) error {
|
func (this *ServerDAO) UpdateServerUserPlanId(tx *dbs.Tx, serverId int64, userPlanId int64) error {
|
||||||
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId)
|
userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/logs"
|
"github.com/iwind/TeaGo/logs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -15,7 +16,7 @@ import (
|
|||||||
func TestServerDAO_ComposeServerConfig(t *testing.T) {
|
func TestServerDAO_ComposeServerConfig(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1)
|
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -25,7 +26,7 @@ func TestServerDAO_ComposeServerConfig(t *testing.T) {
|
|||||||
func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
|
func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 14)
|
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 14, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -35,7 +36,7 @@ func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
|
|||||||
func TestServerDAO_UpdateServerConfig(t *testing.T) {
|
func TestServerDAO_UpdateServerConfig(t *testing.T) {
|
||||||
dbs.NotifyReady()
|
dbs.NotifyReady()
|
||||||
var tx *dbs.Tx
|
var tx *dbs.Tx
|
||||||
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1)
|
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -176,7 +177,7 @@ func TestServerDAO_UpdateServerTrafficLimitStatus(t *testing.T) {
|
|||||||
IsOn: true,
|
IsOn: true,
|
||||||
DailySize: &shared.SizeCapacity{Count: 1, Unit: "mb"},
|
DailySize: &shared.SizeCapacity{Count: 1, Unit: "mb"},
|
||||||
MonthlySize: &shared.SizeCapacity{Count: 10, Unit: "mb"},
|
MonthlySize: &shared.SizeCapacity{Count: 10, Unit: "mb"},
|
||||||
TotalSize: nil,
|
TotalSize: &shared.SizeCapacity{Count: 100, Unit: "gb"},
|
||||||
NoticePageBody: "",
|
NoticePageBody: "",
|
||||||
}, 23, false)
|
}, 23, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -185,6 +186,42 @@ func TestServerDAO_UpdateServerTrafficLimitStatus(t *testing.T) {
|
|||||||
t.Log("ok")
|
t.Log("ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServerDAO_CalculateServerTrafficLimitConfig(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
before := time.Now()
|
||||||
|
defer func() {
|
||||||
|
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||||
|
}()
|
||||||
|
|
||||||
|
var cacheMap = maps.Map{}
|
||||||
|
config, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, 23, cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
logs.PrintAsJSON(config, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerDAO_CalculateServerTrafficLimitConfig_Cache(t *testing.T) {
|
||||||
|
dbs.NotifyReady()
|
||||||
|
|
||||||
|
var tx *dbs.Tx
|
||||||
|
before := time.Now()
|
||||||
|
defer func() {
|
||||||
|
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||||
|
}()
|
||||||
|
|
||||||
|
var cacheMap = maps.Map{}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
config, err := SharedServerDAO.CalculateServerTrafficLimitConfig(tx, 23, cacheMap)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_ = config
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkServerDAO_CountAllEnabledServers(b *testing.B) {
|
func BenchmarkServerDAO_CountAllEnabledServers(b *testing.B) {
|
||||||
SharedServerDAO = NewServerDAO()
|
SharedServerDAO = NewServerDAO()
|
||||||
|
|
||||||
|
|||||||
@@ -35,8 +35,12 @@ type Server struct {
|
|||||||
UdpPorts string `field:"udpPorts"` // 所包含UDP端口
|
UdpPorts string `field:"udpPorts"` // 所包含UDP端口
|
||||||
SupportCNAME uint8 `field:"supportCNAME"` // 允许CNAME不在域名名单
|
SupportCNAME uint8 `field:"supportCNAME"` // 允许CNAME不在域名名单
|
||||||
TrafficLimit string `field:"trafficLimit"` // 流量限制
|
TrafficLimit string `field:"trafficLimit"` // 流量限制
|
||||||
TotalTraffic float64 `field:"totalTraffic"` // 总流量用量(单位GB)
|
TrafficDay string `field:"trafficDay"` // YYYYMMDD
|
||||||
|
TrafficMonth string `field:"trafficMonth"` // YYYYMM
|
||||||
|
TotalDailyTraffic float64 `field:"totalDailyTraffic"` // 日流量
|
||||||
|
TotalMonthlyTraffic float64 `field:"totalMonthlyTraffic"` // 月流量
|
||||||
TrafficLimitStatus string `field:"trafficLimitStatus"` // 流量限制状态
|
TrafficLimitStatus string `field:"trafficLimitStatus"` // 流量限制状态
|
||||||
|
TotalTraffic float64 `field:"totalTraffic"` // 总流量
|
||||||
UserPlanId uint32 `field:"userPlanId"` // 所属套餐ID
|
UserPlanId uint32 `field:"userPlanId"` // 所属套餐ID
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,8 +78,12 @@ type ServerOperator struct {
|
|||||||
UdpPorts interface{} // 所包含UDP端口
|
UdpPorts interface{} // 所包含UDP端口
|
||||||
SupportCNAME interface{} // 允许CNAME不在域名名单
|
SupportCNAME interface{} // 允许CNAME不在域名名单
|
||||||
TrafficLimit interface{} // 流量限制
|
TrafficLimit interface{} // 流量限制
|
||||||
TotalTraffic interface{} // 总流量用量(单位GB)
|
TrafficDay interface{} // YYYYMMDD
|
||||||
|
TrafficMonth interface{} // YYYYMM
|
||||||
|
TotalDailyTraffic interface{} // 日流量
|
||||||
|
TotalMonthlyTraffic interface{} // 月流量
|
||||||
TrafficLimitStatus interface{} // 流量限制状态
|
TrafficLimitStatus interface{} // 流量限制状态
|
||||||
|
TotalTraffic interface{} // 总流量
|
||||||
UserPlanId interface{} // 所属套餐ID
|
UserPlanId interface{} // 所属套餐ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/maps"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -59,7 +61,15 @@ func (this *UserPlanDAO) DisableUserPlan(tx *dbs.Tx, id int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FindEnabledUserPlan 查找启用中的条目
|
// FindEnabledUserPlan 查找启用中的条目
|
||||||
func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, userPlanId int64) (*UserPlan, error) {
|
func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, userPlanId int64, cacheMap maps.Map) (*UserPlan, error) {
|
||||||
|
var cacheKey = this.Table + ":FindEnabledUserPlan:" + types.String(userPlanId)
|
||||||
|
if cacheMap != nil {
|
||||||
|
cache, ok := cacheMap[cacheKey]
|
||||||
|
if ok {
|
||||||
|
return cache.(*UserPlan), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(userPlanId).
|
Pk(userPlanId).
|
||||||
Attr("state", UserPlanStateEnabled).
|
Attr("state", UserPlanStateEnabled).
|
||||||
@@ -67,6 +77,11 @@ func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, userPlanId int64) (*Use
|
|||||||
if result == nil {
|
if result == nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cacheMap != nil {
|
||||||
|
cacheMap[cacheKey] = result
|
||||||
|
}
|
||||||
|
|
||||||
return result.(*UserPlan), err
|
return result.(*UserPlan), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -100,9 +100,9 @@ func (this *ServerService) CreateServer(ctx context.Context, req *pb.CreateServe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 校验用户套餐
|
// 检查用户套餐
|
||||||
if req.UserPlanId > 0 {
|
if req.UserPlanId > 0 {
|
||||||
userPlan, err := models.SharedUserPlanDAO.FindEnabledUserPlan(tx, req.UserPlanId)
|
userPlan, err := models.SharedUserPlanDAO.FindEnabledUserPlan(tx, req.UserPlanId, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -643,7 +643,7 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 配置
|
// 配置
|
||||||
config, err := models.SharedServerDAO.ComposeServerConfig(tx, server, nil)
|
config, err := models.SharedServerDAO.ComposeServerConfig(tx, server, nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -784,7 +784,7 @@ func (this *ServerService) FindEnabledServer(ctx context.Context, req *pb.FindEn
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 配置
|
// 配置
|
||||||
config, err := models.SharedServerDAO.ComposeServerConfig(tx, server, nil)
|
config, err := models.SharedServerDAO.ComposeServerConfig(tx, server, nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -844,7 +844,7 @@ func (this *ServerService) FindEnabledServerConfig(ctx context.Context, req *pb.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
config, err := models.SharedServerDAO.ComposeServerConfigWithServerId(tx, req.ServerId)
|
config, err := models.SharedServerDAO.ComposeServerConfigWithServerId(tx, req.ServerId, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1779,7 +1779,7 @@ func (this *ServerService) UpdateServerUserPlan(ctx context.Context, req *pb.Upd
|
|||||||
return nil, errors.New("the server is not belong to any user")
|
return nil, errors.New("the server is not belong to any user")
|
||||||
}
|
}
|
||||||
|
|
||||||
userPlan, err := models.SharedUserPlanDAO.FindEnabledUserPlan(tx, req.UserPlanId)
|
userPlan, err := models.SharedUserPlanDAO.FindEnabledUserPlan(tx, req.UserPlanId, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user