Skip to content

Commit ce0475f

Browse files
committed
feat: beyond threshold notify
1 parent 44062a9 commit ce0475f

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

service/aiproxy/common/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var (
3636
var (
3737
retryTimes atomic.Int64
3838
enableModelErrorAutoBan atomic.Bool
39-
modelErrorAutoBanRate = math.Float64bits(0.5)
39+
modelErrorAutoBanRate = math.Float64bits(0.3)
4040
timeoutWithModelType atomic.Value
4141
disableModelConfig = env.Bool("DISABLE_MODEL_CONFIG", false)
4242
)

service/aiproxy/controller/relay.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func relayController(mode relaymode.Mode) (RelayController, bool) {
6262
func RelayHelper(meta *meta.Meta, c *gin.Context, relayController RelayController) (*model.ErrorWithStatusCode, bool) {
6363
err := relayController(meta, c)
6464
if err == nil {
65-
if _, err := monitor.AddRequest(
65+
if _, _, err := monitor.AddRequest(
6666
context.Background(),
6767
meta.OriginModel,
6868
int64(meta.Channel.ID),
@@ -73,7 +73,7 @@ func RelayHelper(meta *meta.Meta, c *gin.Context, relayController RelayControlle
7373
return nil, false
7474
}
7575
if shouldErrorMonitor(err.StatusCode) {
76-
banned, err := monitor.AddRequest(
76+
beyondThreshold, autoBanned, err := monitor.AddRequest(
7777
context.Background(),
7878
meta.OriginModel,
7979
int64(meta.Channel.ID),
@@ -82,13 +82,20 @@ func RelayHelper(meta *meta.Meta, c *gin.Context, relayController RelayControlle
8282
if err != nil {
8383
log.Errorf("add request failed: %+v", err)
8484
}
85-
if banned {
85+
if autoBanned {
8686
notify.ErrorThrottle(
87-
fmt.Sprintf("banned:%d:%s", meta.Channel.ID, meta.OriginModel),
87+
fmt.Sprintf("autoBanned:%d:%s", meta.Channel.ID, meta.OriginModel),
8888
time.Minute,
8989
fmt.Sprintf("channel[%d] %s(%d) model %s is auto banned",
9090
meta.Channel.Type, meta.Channel.Name, meta.Channel.ID, meta.OriginModel),
91-
"banned")
91+
"autoBanned")
92+
} else if beyondThreshold {
93+
notify.WarnThrottle(
94+
fmt.Sprintf("beyondThreshold:%d:%s", meta.Channel.ID, meta.OriginModel),
95+
time.Minute,
96+
fmt.Sprintf("channel[%d] %s(%d) model %s error rate is beyond threshold",
97+
meta.Channel.Type, meta.Channel.Name, meta.Channel.ID, meta.OriginModel),
98+
"beyondThreshold")
9299
}
93100
}
94101
return err, shouldRetry(c, err.StatusCode)

service/aiproxy/monitor/model.go

+30-24
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ func canAutoBan() int {
8282
}
8383

8484
// 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) {
8686
if !common.RedisEnabled {
87-
return false, nil
87+
return false, false, nil
8888
}
8989

9090
errorFlag := 0
@@ -105,9 +105,9 @@ func AddRequest(ctx context.Context, model string, channelID int64, isError bool
105105
canAutoBan(),
106106
).Int64()
107107
if err != nil {
108-
return false, err
108+
return false, false, err
109109
}
110-
return val == 1, nil
110+
return val == 3, val == 1, nil
111111
}
112112

113113
// GetChannelModelErrorRates gets error rates for a specific channel
@@ -287,8 +287,8 @@ local can_auto_ban = tonumber(ARGV[6])
287287
local banned_key = "model:" .. model .. ":banned"
288288
local stats_key = "model:" .. model .. ":channel:" .. channel_id .. ":stats"
289289
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)
292292
293293
local function parse_req_err(value)
294294
if not value then return 0, 0 end
@@ -334,24 +334,30 @@ local function check_channel_error()
334334
total_err = total_err + err
335335
end
336336
end
337-
337+
338338
if #to_delete > 0 then
339339
redis.call("HDEL", stats_key, unpack(to_delete))
340340
end
341-
341+
if total_req < 20 then
342+
return 0
343+
end
344+
342345
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
355361
end
356362
357363
return check_channel_error()
@@ -360,8 +366,8 @@ return check_channel_error()
360366
getModelErrorRateLuaScript = `
361367
local model_stats_key = KEYS[1]
362368
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)
365371
local min_valid_slice = current_slice - maxSliceCount
366372
367373
local function parse_req_err(value)
@@ -389,8 +395,8 @@ return total_err / total_req
389395
getChannelModelErrorRateLuaScript = `
390396
local stats_key = KEYS[1]
391397
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)
394400
local min_valid_slice = current_slice - maxSliceCount
395401
396402
local function parse_req_err(value)

0 commit comments

Comments
 (0)