@@ -82,9 +82,9 @@ func canAutoBan() int {
82
82
}
83
83
84
84
// AddRequest adds a request record and checks if channel should be banned
85
- func AddRequest (ctx context.Context , model string , channelID int64 , isError bool ) (banned bool , err error ) {
85
+ func AddRequest (ctx context.Context , model string , channelID int64 , isError bool ) (beyondThreshold bool , autoBanned bool , err error ) {
86
86
if ! common .RedisEnabled {
87
- return false , nil
87
+ return false , false , nil
88
88
}
89
89
90
90
errorFlag := 0
@@ -105,9 +105,9 @@ func AddRequest(ctx context.Context, model string, channelID int64, isError bool
105
105
canAutoBan (),
106
106
).Int64 ()
107
107
if err != nil {
108
- return false , err
108
+ return false , false , err
109
109
}
110
- return val == 1 , nil
110
+ return val == 3 , val == 1 , nil
111
111
}
112
112
113
113
// GetChannelModelErrorRates gets error rates for a specific channel
@@ -287,8 +287,8 @@ local can_auto_ban = tonumber(ARGV[6])
287
287
local banned_key = "model:" .. model .. ":banned"
288
288
local stats_key = "model:" .. model .. ":channel:" .. channel_id .. ":stats"
289
289
local model_stats_key = "model:" .. model .. ":total_stats"
290
- local maxSliceCount = 6
291
- local current_slice = math.floor(now_ts / 1000 )
290
+ local maxSliceCount = 12
291
+ local current_slice = math.floor(now_ts / 10000 )
292
292
293
293
local function parse_req_err(value)
294
294
if not value then return 0, 0 end
@@ -334,24 +334,30 @@ local function check_channel_error()
334
334
total_err = total_err + err
335
335
end
336
336
end
337
-
337
+
338
338
if #to_delete > 0 then
339
339
redis.call("HDEL", stats_key, unpack(to_delete))
340
340
end
341
-
341
+ if total_req < 20 then
342
+ return 0
343
+ end
344
+
342
345
local already_banned = redis.call("SISMEMBER", banned_key, channel_id) == 1
343
-
344
- if total_req >= 10 and (total_err / total_req) < max_error_rate and already_banned then
345
- redis.call("SREM", banned_key, channel_id)
346
- return 0
347
- end
348
-
349
- if total_req >= 10 and (total_err / total_req) >= max_error_rate and not already_banned and can_auto_ban == 1 then
350
- redis.call("SADD", banned_key, channel_id)
351
- return 1
352
- end
353
-
354
- return 2
346
+ if (total_err / total_req) < max_error_rate then
347
+ if already_banned {
348
+ redis.call("SREM", banned_key, channel_id)
349
+ }
350
+ return 0
351
+ else
352
+ if already_banned {
353
+ return 2
354
+ }
355
+ if can_auto_ban == 0 then
356
+ return 3
357
+ end
358
+ redis.call("SADD", banned_key, channel_id)
359
+ return 1
360
+ end
355
361
end
356
362
357
363
return check_channel_error()
@@ -360,8 +366,8 @@ return check_channel_error()
360
366
getModelErrorRateLuaScript = `
361
367
local model_stats_key = KEYS[1]
362
368
local now_ts = tonumber(ARGV[1])
363
- local maxSliceCount = 6
364
- local current_slice = math.floor(now_ts / 1000 )
369
+ local maxSliceCount = 12
370
+ local current_slice = math.floor(now_ts / 10000 )
365
371
local min_valid_slice = current_slice - maxSliceCount
366
372
367
373
local function parse_req_err(value)
@@ -389,8 +395,8 @@ return total_err / total_req
389
395
getChannelModelErrorRateLuaScript = `
390
396
local stats_key = KEYS[1]
391
397
local now_ts = tonumber(ARGV[1])
392
- local maxSliceCount = 6
393
- local current_slice = math.floor(now_ts / 1000 )
398
+ local maxSliceCount = 12
399
+ local current_slice = math.floor(now_ts / 10000 )
394
400
local min_valid_slice = current_slice - maxSliceCount
395
401
396
402
local function parse_req_err(value)
0 commit comments