Skip to content

Commit e6acb25

Browse files
fix(ai-rate-limiting): not allowed to limit to a single instance
1 parent 99a0792 commit e6acb25

File tree

2 files changed

+394
-6
lines changed

2 files changed

+394
-6
lines changed

apisix/plugins/ai-rate-limiting.lua

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ local schema = {
4747
},
4848
instances = {
4949
type = "array",
50-
items = instance_limit_schema
50+
items = instance_limit_schema,
51+
minItems = 1,
5152
},
5253
rejected_code = {
5354
type = "integer", minimum = 200, maximum = 599, default = 503
@@ -56,7 +57,18 @@ local schema = {
5657
type = "string", minLength = 1
5758
},
5859
},
59-
required = {"limit", "time_window"},
60+
dependencies = {
61+
limit = {"time_window"},
62+
time_window = {"limit"}
63+
},
64+
anyOf = {
65+
{
66+
required = {"limit", "time_window"}
67+
},
68+
{
69+
required = {"instances"}
70+
}
71+
}
6072
}
6173

6274
local _M = {
@@ -112,6 +124,10 @@ end
112124
local function fetch_limit_conf_kvs(conf)
113125
local mt = {
114126
__index = function(t, k)
127+
if not conf.limit then
128+
return nil
129+
end
130+
115131
local limit_conf = transform_limit_conf(conf, nil, k)
116132
t[k] = limit_conf
117133
return limit_conf
@@ -134,6 +150,9 @@ function _M.access(conf, ctx)
134150

135151
local limit_conf_kvs = limit_conf_cache(conf, nil, fetch_limit_conf_kvs, conf)
136152
local limit_conf = limit_conf_kvs[ai_instance_name]
153+
if not limit_conf then
154+
return
155+
end
137156
local code, msg = limit_count.rate_limit(limit_conf, ctx, plugin_name, 1, true)
138157
ctx.ai_rate_limiting = code and true or false
139158
return code, msg
@@ -164,6 +183,10 @@ function _M.check_instance_status(conf, ctx, instance_name)
164183

165184
local limit_conf_kvs = limit_conf_cache(conf, nil, fetch_limit_conf_kvs, conf)
166185
local limit_conf = limit_conf_kvs[instance_name]
186+
if not limit_conf then
187+
return true
188+
end
189+
167190
local code, _ = limit_count.rate_limit(limit_conf, ctx, plugin_name, 1, true)
168191
if code then
169192
core.log.info("rate limit for instance: ", instance_name, " code: ", code)
@@ -202,7 +225,9 @@ function _M.log(conf, ctx)
202225

203226
local limit_conf_kvs = limit_conf_cache(conf, nil, fetch_limit_conf_kvs, conf)
204227
local limit_conf = limit_conf_kvs[instance_name]
205-
limit_count.rate_limit(limit_conf, ctx, plugin_name, used_tokens)
228+
if limit_conf then
229+
limit_count.rate_limit(limit_conf, ctx, plugin_name, used_tokens)
230+
end
206231
end
207232

208233

0 commit comments

Comments
 (0)