2023-05-15 21:15:29 -04:00
|
|
|
package limiter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2023-07-29 07:27:15 -04:00
|
|
|
|
|
|
|
"github.com/InazumaV/V2bX/api/panel"
|
|
|
|
"github.com/InazumaV/V2bX/common/format"
|
2023-05-15 21:15:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (l *Limiter) AddDynamicSpeedLimit(tag string, userInfo *panel.UserInfo, limitNum int, expire int64) error {
|
|
|
|
userLimit := &UserLimitInfo{
|
|
|
|
DynamicSpeedLimit: limitNum,
|
|
|
|
ExpireTime: time.Now().Add(time.Duration(expire) * time.Second).Unix(),
|
|
|
|
}
|
2023-07-12 13:44:15 -04:00
|
|
|
l.UserLimitInfo.Store(format.UserTag(tag, userInfo.Uuid), userLimit)
|
2023-05-15 21:15:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// determineSpeedLimit returns the minimum non-zero rate
|
|
|
|
func determineSpeedLimit(limit1, limit2 int) (limit int) {
|
|
|
|
if limit1 == 0 || limit2 == 0 {
|
|
|
|
if limit1 > limit2 {
|
|
|
|
return limit1
|
|
|
|
} else if limit1 < limit2 {
|
|
|
|
return limit2
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if limit1 > limit2 {
|
|
|
|
return limit2
|
|
|
|
} else if limit1 < limit2 {
|
|
|
|
return limit1
|
|
|
|
} else {
|
|
|
|
return limit1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|