perf(server): cache speed limit calculations
Build docker and publish / build (20.15.1) (push) Successful in 5m37s
Build docker and publish / build (20.15.1) (push) Successful in 5m37s
This commit is contained in:
@@ -137,7 +137,7 @@ func (l *GetServerUserListLogic) GetServerUserList(req *types.GetServerUserListR
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if len(subs) == 0 {
|
||||
return &types.GetServerUserListResponse{
|
||||
Users: []types.ServerUser{
|
||||
@@ -194,7 +194,7 @@ func (l *GetServerUserListLogic) GetServerUserList(req *types.GetServerUserListR
|
||||
val, _ := json.Marshal(resp)
|
||||
etag := tool.GenerateETag(val)
|
||||
l.ctx.Header("ETag", etag)
|
||||
err = l.svcCtx.Redis.Set(l.ctx, cacheKey, string(val), -1).Err()
|
||||
err = l.svcCtx.Redis.Set(l.ctx, cacheKey, string(val), l.serverUserListCacheTTL()).Err()
|
||||
if err != nil {
|
||||
l.Errorw("[ServerUserListCacheKey] redis set error", logger.Field("error", err.Error()))
|
||||
}
|
||||
@@ -205,6 +205,18 @@ func (l *GetServerUserListLogic) GetServerUserList(req *types.GetServerUserListR
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (l *GetServerUserListLogic) serverUserListCacheTTL() time.Duration {
|
||||
pullInterval := l.svcCtx.Config.Node.NodePullInterval
|
||||
if pullInterval <= 0 {
|
||||
pullInterval = 60
|
||||
}
|
||||
ttl := time.Duration(pullInterval*2) * time.Second
|
||||
if ttl < time.Minute {
|
||||
return time.Minute
|
||||
}
|
||||
return ttl
|
||||
}
|
||||
|
||||
func (l *GetServerUserListLogic) shouldIncludeServerUser(userSub *user.Subscribe, serverNodeGroupIds []int64) bool {
|
||||
if userSub == nil {
|
||||
return false
|
||||
@@ -295,6 +307,15 @@ func (l *GetServerUserListLogic) canUseExpiredNodeGroup(userSub *user.Subscribe,
|
||||
|
||||
// calculateEffectiveSpeedLimit 计算用户的实际限速值(考虑按量限速规则)
|
||||
func (l *GetServerUserListLogic) calculateEffectiveSpeedLimit(sub *subscribe.Subscribe, userSub *user.Subscribe) int64 {
|
||||
result := speedlimit.Calculate(l.ctx.Request.Context(), l.svcCtx.DB, userSub.UserId, userSub.Id, sub.SpeedLimit, sub.TrafficLimit)
|
||||
result := speedlimit.CalculateWithCache(
|
||||
l.ctx.Request.Context(),
|
||||
l.svcCtx.Redis,
|
||||
l.svcCtx.DB,
|
||||
userSub.UserId,
|
||||
userSub.Id,
|
||||
sub.SpeedLimit,
|
||||
sub.TrafficLimit,
|
||||
30*time.Second,
|
||||
)
|
||||
return result.EffectiveSpeed
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user