mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-01-05 03:05:48 +08:00
实现单个服务的带宽限制(商业版)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -128,6 +128,12 @@ service ServerService {
|
||||
|
||||
// 清除缓存
|
||||
rpc purgeServerCache(PurgeServerCacheRequest) returns (PurgeServerCacheResponse);
|
||||
|
||||
// 查找带宽限制
|
||||
rpc findEnabledServerBandwidthLimit(FindEnabledServerBandwidthLimitRequest) returns (FindEnabledServerBandwidthLimitResponse);
|
||||
|
||||
// 设置带宽限制
|
||||
rpc updateServerBandwidthLimit(UpdateServerBandwidthLimitRequest) returns (RPCSuccess);
|
||||
}
|
||||
|
||||
// 创建服务
|
||||
@@ -508,4 +514,19 @@ message PurgeServerCacheRequest {
|
||||
message PurgeServerCacheResponse {
|
||||
bool isOk = 1;
|
||||
string message = 2;
|
||||
}
|
||||
|
||||
// 查找带宽限制
|
||||
message FindEnabledServerBandwidthLimitRequest {
|
||||
int64 serverId = 1;
|
||||
}
|
||||
|
||||
message FindEnabledServerBandwidthLimitResponse {
|
||||
bytes bandwidthLimitJSON = 1;
|
||||
}
|
||||
|
||||
// 设置带宽限制
|
||||
message UpdateServerBandwidthLimitRequest {
|
||||
int64 serverId = 1;
|
||||
bytes bandwidthLimitJSON = 2;
|
||||
}
|
||||
58
pkg/serverconfigs/bandwidth_limit_config.go
Normal file
58
pkg/serverconfigs/bandwidth_limit_config.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
|
||||
// DefaultBandwidthLimitNoticePageBody 达到带宽限制时默认提示内容
|
||||
const DefaultBandwidthLimitNoticePageBody = `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bandwidth Limit Exceeded Warning/title>
|
||||
<body>
|
||||
|
||||
The site bandwidth has exceeded the limit. Please contact with the site administrator.
|
||||
|
||||
</body>
|
||||
</html>`
|
||||
|
||||
// BandwidthLimitConfig 带宽限制
|
||||
type BandwidthLimitConfig struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
|
||||
DailySize *shared.SizeCapacity `yaml:"dailySize" json:"dailySize"` // 每日限制
|
||||
MonthlySize *shared.SizeCapacity `yaml:"monthlySize" json:"monthlySize"` // 每月限制
|
||||
TotalSize *shared.SizeCapacity `yaml:"totalSize" json:"totalSize"` // 总限制 TODO 需要实现
|
||||
|
||||
NoticePageBody string `yaml:"noticePageBody" json:"noticePageBody"` // 超出限制时的提醒,支持请求变量
|
||||
}
|
||||
|
||||
// DailyBytes 每天限制
|
||||
// 不使用Init()来初始化数据,是为了让其他地方不经过Init()也能得到计算值
|
||||
func (this *BandwidthLimitConfig) DailyBytes() int64 {
|
||||
if this.DailySize != nil {
|
||||
return this.DailySize.Bytes()
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// MonthlyBytes 每月限制
|
||||
func (this *BandwidthLimitConfig) MonthlyBytes() int64 {
|
||||
if this.MonthlySize != nil {
|
||||
return this.MonthlySize.Bytes()
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// TotalBytes 总限制
|
||||
func (this *BandwidthLimitConfig) TotalBytes() int64 {
|
||||
if this.TotalSize != nil {
|
||||
return this.TotalSize.Bytes()
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// IsEmpty 检查是否有限制值
|
||||
func (this *BandwidthLimitConfig) IsEmpty() bool {
|
||||
return !this.IsOn || (this.DailyBytes() <= 0 && this.MonthlyBytes() <= 0 && this.TotalBytes() <= 0)
|
||||
}
|
||||
17
pkg/serverconfigs/bandwidth_limit_status.go
Normal file
17
pkg/serverconfigs/bandwidth_limit_status.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
|
||||
// BandwidthLimitStatus 带宽限制状态
|
||||
type BandwidthLimitStatus struct {
|
||||
UntilDay string `yaml:"untilDay" json:"untilDay"` // 有效日期,格式YYYYMMDD
|
||||
}
|
||||
|
||||
func (this *BandwidthLimitStatus) IsValid() bool {
|
||||
if len(this.UntilDay) == 0 {
|
||||
return false
|
||||
}
|
||||
return this.UntilDay >= timeutil.Format("Ymd")
|
||||
}
|
||||
@@ -42,6 +42,10 @@ type ServerConfig struct {
|
||||
HTTPCachePolicyId int64 `yaml:"httpCachePolicyId" json:"httpCachePolicyId"`
|
||||
HTTPCachePolicy *HTTPCachePolicy `yaml:"httpCachePolicy" json:"httpCachePolicy"` // 通过 HTTPCachePolicyId 获取
|
||||
|
||||
// 带宽限制
|
||||
BandwidthLimit *BandwidthLimitConfig `yaml:"bandwidthLimit" json:"bandwidthLimit"`
|
||||
BandwidthLimitStatus *BandwidthLimitStatus `yaml:"bandwidthLimitStatus" json:"bandwidthLimitStatus"`
|
||||
|
||||
// 分组
|
||||
Group *ServerGroupConfig `yaml:"group" json:"group"`
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ const (
|
||||
SizeCapacityUnitKB SizeCapacityUnit = "kb"
|
||||
SizeCapacityUnitMB SizeCapacityUnit = "mb"
|
||||
SizeCapacityUnitGB SizeCapacityUnit = "gb"
|
||||
SizeCapacityUnitTB SizeCapacityUnit = "tb"
|
||||
SizeCapacityUnitPB SizeCapacityUnit = "pb"
|
||||
)
|
||||
|
||||
type SizeCapacity struct {
|
||||
@@ -17,6 +19,9 @@ type SizeCapacity struct {
|
||||
}
|
||||
|
||||
func (this *SizeCapacity) Bytes() int64 {
|
||||
if this.Count < 0 {
|
||||
return -1
|
||||
}
|
||||
switch this.Unit {
|
||||
case SizeCapacityUnitByte:
|
||||
return this.Count
|
||||
@@ -26,6 +31,10 @@ func (this *SizeCapacity) Bytes() int64 {
|
||||
return this.Count * 1024 * 1024
|
||||
case SizeCapacityUnitGB:
|
||||
return this.Count * 1024 * 1024 * 1024
|
||||
case SizeCapacityUnitTB:
|
||||
return this.Count * 1024 * 1024 * 1024 * 1024
|
||||
case SizeCapacityUnitPB:
|
||||
return this.Count * 1024 * 1024 * 1024 * 1024 * 1024
|
||||
default:
|
||||
return this.Count
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user