fix speedlimit

This commit is contained in:
yuzuki999
2023-01-03 18:58:52 +08:00
parent 2f223e173f
commit e2aee13d78

View File

@@ -56,7 +56,6 @@ func (l *Limiter) AddInboundLimiter(tag string, nodeInfo *panel.NodeInfo, users
inboundInfo.UserLimitInfo.Store(fmt.Sprintf("%s|%s|%d", tag, users[i].Uuid, users[i].Id), userLimit) inboundInfo.UserLimitInfo.Store(fmt.Sprintf("%s|%s|%d", tag, users[i].Uuid, users[i].Id), userLimit)
} }
} }
inboundInfo.UserLimitInfo = new(sync.Map)
l.InboundInfo.Store(tag, inboundInfo) // Replace the old inbound info l.InboundInfo.Store(tag, inboundInfo) // Replace the old inbound info
return nil return nil
} }
@@ -188,14 +187,16 @@ func (l *Limiter) CheckSpeedAndDeviceLimit(tag string, email string, ip string)
inboundInfo := value.(*InboundInfo) inboundInfo := value.(*InboundInfo)
nodeLimit := inboundInfo.NodeSpeedLimit nodeLimit := inboundInfo.NodeSpeedLimit
userLimit := 0 userLimit := 0
expired := false
if v, ok := inboundInfo.UserLimitInfo.Load(email); ok { if v, ok := inboundInfo.UserLimitInfo.Load(email); ok {
u := v.(*UserLimitInfo) u := v.(*UserLimitInfo)
if u.ExpireTime < time.Now().Unix() && u.ExpireTime != 0 { if u.ExpireTime < time.Now().Unix() && u.ExpireTime != 0 {
if u.SpeedLimit != 0 { if u.SpeedLimit != 0 {
userLimit = u.SpeedLimit userLimit = u.SpeedLimit
u.DynamicSpeedLimit = 0
u.ExpireTime = 0
} else {
inboundInfo.UserLimitInfo.Delete(email)
} }
expired = true
} else { } else {
userLimit = determineSpeedLimit(u.SpeedLimit, u.DynamicSpeedLimit) userLimit = determineSpeedLimit(u.SpeedLimit, u.DynamicSpeedLimit)
} }
@@ -226,14 +227,9 @@ func (l *Limiter) CheckSpeedAndDeviceLimit(tag string, email string, ip string)
if limit > 0 { if limit > 0 {
limiter := ratelimit.NewBucketWithQuantum(time.Second, limit, limit) // Byte/s limiter := ratelimit.NewBucketWithQuantum(time.Second, limit, limit) // Byte/s
if v, ok := inboundInfo.SpeedLimiter.LoadOrStore(email, limiter); ok { if v, ok := inboundInfo.SpeedLimiter.LoadOrStore(email, limiter); ok {
if expired { return v.(*ratelimit.Bucket), true, false
inboundInfo.SpeedLimiter.Store(email, limiter)
inboundInfo.UserLimitInfo.Delete(email)
return limiter, true, false
}
bucket := v.(*ratelimit.Bucket)
return bucket, true, false
} else { } else {
inboundInfo.SpeedLimiter.Store(email, limiter)
return limiter, true, false return limiter, true, false
} }
} else { } else {