套餐可以设置带宽限制

This commit is contained in:
刘祥超
2024-01-11 15:21:00 +08:00
parent c94895a7c4
commit 194b0ec184
6 changed files with 172 additions and 41 deletions

View File

@@ -34,6 +34,7 @@ const (
NodeTaskTypeWebPPolicyChanged NodeTaskType = "webPPolicyChanged" // WebP策略变化
NodeTaskTypeUpdatingServers NodeTaskType = "updatingServers" // 更新一组服务
NodeTaskTypeTOAChanged NodeTaskType = "toaChanged" // TOA配置变化
NodeTaskTypePlanChanged NodeTaskType = "planChanged" // 套餐变化
// NS相关

View File

@@ -49,7 +49,12 @@ func (this *PlanDAO) EnablePlan(tx *dbs.Tx, id uint32) error {
// DisablePlan 禁用条目
func (this *PlanDAO) DisablePlan(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
clusterId, err := this.FindPlanClusterId(tx, id)
if err != nil {
return err
}
_, err = this.Query(tx).
Pk(id).
Set("state", PlanStateDisabled).
Update()
@@ -57,7 +62,7 @@ func (this *PlanDAO) DisablePlan(tx *dbs.Tx, id int64) error {
return err
}
return this.NotifyUpdate(tx, id)
return this.NotifyUpdate(tx, id, clusterId)
}
// FindEnabledPlan 查找启用中的条目
@@ -175,18 +180,18 @@ func (this *PlanDAO) FindEnabledPlanTrafficLimit(tx *dbs.Tx, planId int64, cache
return config, nil
}
// NotifyUpdate 通知变更
func (this *PlanDAO) NotifyUpdate(tx *dbs.Tx, planId int64) error {
// 这里不要加入状态参数,因为需要适应删除后的更新
clusterId, err := this.Query(tx).
// FindPlanClusterId 查找套餐所属集群
func (this *PlanDAO) FindPlanClusterId(tx *dbs.Tx, planId int64) (clusterId int64, err error) {
return this.Query(tx).
Pk(planId).
Result("clusterId").
FindInt64Col(0)
if err != nil {
return err
}
if clusterId > 0 {
return SharedNodeClusterDAO.NotifyUpdate(tx, clusterId)
}
return nil
}
// NotifyUpdate 通知变更
func (this *PlanDAO) NotifyUpdate(tx *dbs.Tx, planId int64, clusterId int64) error {
if clusterId <= 0 {
return nil
}
return SharedNodeClusterDAO.NotifyUpdate(tx, clusterId)
}

View File

@@ -6,9 +6,10 @@ const (
PlanField_Id dbs.FieldName = "id" // ID
PlanField_IsOn dbs.FieldName = "isOn" // 是否启用
PlanField_Name dbs.FieldName = "name" // 套餐名
PlanField_Description dbs.FieldName = "description" // 描述
PlanField_Description dbs.FieldName = "description" // 套餐简介
PlanField_ClusterId dbs.FieldName = "clusterId" // 集群ID
PlanField_TrafficLimit dbs.FieldName = "trafficLimit" // 流量限制
PlanField_BandwidthLimitPerNode dbs.FieldName = "bandwidthLimitPerNode" // 带宽限制
PlanField_Features dbs.FieldName = "features" // 允许的功能
PlanField_HasFullFeatures dbs.FieldName = "hasFullFeatures" // 是否有完整的功能
PlanField_TrafficPrice dbs.FieldName = "trafficPrice" // 流量价格设定
@@ -33,9 +34,10 @@ type Plan struct {
Id uint32 `field:"id"` // ID
IsOn bool `field:"isOn"` // 是否启用
Name string `field:"name"` // 套餐名
Description string `field:"description"` // 描述
Description string `field:"description"` // 套餐简介
ClusterId uint32 `field:"clusterId"` // 集群ID
TrafficLimit dbs.JSON `field:"trafficLimit"` // 流量限制
BandwidthLimitPerNode dbs.JSON `field:"bandwidthLimitPerNode"` // 带宽限制
Features dbs.JSON `field:"features"` // 允许的功能
HasFullFeatures bool `field:"hasFullFeatures"` // 是否有完整的功能
TrafficPrice dbs.JSON `field:"trafficPrice"` // 流量价格设定
@@ -59,9 +61,10 @@ type PlanOperator struct {
Id any // ID
IsOn any // 是否启用
Name any // 套餐名
Description any // 描述
Description any // 套餐简介
ClusterId any // 集群ID
TrafficLimit any // 流量限制
BandwidthLimitPerNode any // 带宽限制
Features any // 允许的功能
HasFullFeatures any // 是否有完整的功能
TrafficPrice any // 流量价格设定

View File

@@ -1322,28 +1322,10 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, ignoreCer
}
// 套餐是否依然有效
plan, err := SharedPlanDAO.FindEnabledPlan(tx, int64(userPlan.PlanId), cacheMap)
if err != nil {
return nil, err
}
if plan != nil {
config.UserPlan = &serverconfigs.UserPlanConfig{
Id: int64(userPlan.Id),
DayTo: userPlan.DayTo,
Plan: &serverconfigs.PlanConfig{
Id: int64(plan.Id),
Name: plan.Name,
},
}
if len(plan.TrafficLimit) > 0 && (config.TrafficLimit == nil || !config.TrafficLimit.IsOn) {
var trafficLimitConfig = &serverconfigs.TrafficLimitConfig{}
err = json.Unmarshal(plan.TrafficLimit, trafficLimitConfig)
if err != nil {
return nil, err
}
config.TrafficLimit = trafficLimitConfig
}
config.UserPlan = &serverconfigs.UserPlanConfig{
Id: int64(userPlan.Id),
DayTo: userPlan.DayTo,
PlanId: int64(userPlan.PlanId),
}
}
}
@@ -2358,8 +2340,36 @@ func (this *ServerDAO) UpdateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64
// RenewServerTrafficLimitStatus 根据限流配置更新网站的流量限制状态
func (this *ServerDAO) RenewServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitConfig *serverconfigs.TrafficLimitConfig, serverId int64, isUpdatingConfig bool) error {
if serverId <= 0 {
return nil
}
if !trafficLimitConfig.IsOn {
if isUpdatingConfig {
var oldStatus = &serverconfigs.TrafficLimitStatus{}
trafficLimitStatus, err := this.Query(tx).
Pk(serverId).
Result("trafficLimitStatus").
FindJSONCol()
if err != nil {
return err
}
if IsNotNull(trafficLimitStatus) {
err = json.Unmarshal(trafficLimitStatus, oldStatus)
if err != nil {
return err
}
if oldStatus.PlanId == 0 /** 说明是网站自行设置的限制 **/ {
err = this.Query(tx).
Pk(serverId).
Set("trafficLimitStatus", dbs.SQL("NULL")).
UpdateQuickly()
if err != nil {
return err
}
}
}
return this.NotifyUpdate(tx, serverId)
}
return nil
@@ -2470,7 +2480,9 @@ func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, serverId int64
if err != nil {
return err
}
if len(oldStatus.UntilDay) > 0 && oldStatus.UntilDay >= day /** 如果已经限制,且比当前日期长,则无需重复 **/ {
if len(oldStatus.UntilDay) > 0 &&
oldStatus.UntilDay >= day /** 如果已经限制,且比当前日期长,则无需重复 **/ &&
oldStatus.PlanId == planId /** 套餐无变化 **/ {
// no need to change
return nil
}