实现请求连接数等限制

This commit is contained in:
刘祥超
2021-12-12 11:44:58 +08:00
parent 31a04b32b1
commit 15c952e6eb
7 changed files with 568 additions and 159 deletions

View File

@@ -11,6 +11,9 @@ const (
SizeCapacityUnitGB SizeCapacityUnit = "gb"
SizeCapacityUnitTB SizeCapacityUnit = "tb"
SizeCapacityUnitPB SizeCapacityUnit = "pb"
SizeCapacityUnitEB SizeCapacityUnit = "eb"
//SizeCapacityUnitZB SizeCapacityUnit = "zb" // zb和yb超出int64范围暂不支持
//SizeCapacityUnitYB SizeCapacityUnit = "yb"
)
type SizeCapacity struct {
@@ -26,15 +29,17 @@ func (this *SizeCapacity) Bytes() int64 {
case SizeCapacityUnitByte:
return this.Count
case SizeCapacityUnitKB:
return this.Count * 1024
return this.Count * this.pow(1)
case SizeCapacityUnitMB:
return this.Count * 1024 * 1024
return this.Count * this.pow(2)
case SizeCapacityUnitGB:
return this.Count * 1024 * 1024 * 1024
return this.Count * this.pow(3)
case SizeCapacityUnitTB:
return this.Count * 1024 * 1024 * 1024 * 1024
return this.Count * this.pow(4)
case SizeCapacityUnitPB:
return this.Count * 1024 * 1024 * 1024 * 1024 * 1024
return this.Count * this.pow(5)
case SizeCapacityUnitEB:
return this.Count * this.pow(6)
default:
return this.Count
}
@@ -43,3 +48,13 @@ func (this *SizeCapacity) Bytes() int64 {
func (this *SizeCapacity) AsJSON() ([]byte, error) {
return json.Marshal(this)
}
func (this *SizeCapacity) pow(n int) int64 {
if n <= 0 {
return 1
}
if n == 1 {
return 1024
}
return this.pow(n-1) * 1024
}