修改速率模块

This commit is contained in:
2026-05-06 20:16:58 +08:00
parent c9c2fa8185
commit ef0c5b9694

View File

@@ -3,13 +3,12 @@
local _M = {} local _M = {}
local config_module = require "config.rate_limit_config"
local common_config = require "config.common_config" local common_config = require "config.common_config"
local logger = require "lib.logger" local logger = require "lib.logger"
-- 获取配置 -- 获取配置(直接从公共配置读取)
local function get_config() local function get_config()
return config_module.get_config() return common_config.rate_limit
end end
-- 检查域名是否应该应用限流 -- 检查域名是否应该应用限流
@@ -118,12 +117,12 @@ function _M.check_rate_limit()
-- 检查是否超过限制 -- 检查是否超过限制
if new_count > max_requests then if new_count > max_requests then
-- 根据配置的处置策略执行相应操作 -- 根据配置的处置策略执行相应操作
if cfg.action == config_module.ACTION_ALLOW then if cfg.action == "allow" then
-- 放行:仅记录日志,不阻止 -- 放行:仅记录日志,不阻止
logger.log_allowed(client_ip, new_count, time_window) logger.log_allowed(client_ip, new_count, time_window)
return true return true
elseif cfg.action == config_module.ACTION_RATE_LIMIT then elseif cfg.action == "rate_limit" then
-- 限制速率返回429 -- 限制速率返回429
logger.log_rate_limited(client_ip, new_count, max_requests, time_window) logger.log_rate_limited(client_ip, new_count, max_requests, time_window)
@@ -132,7 +131,7 @@ function _M.check_rate_limit()
ngx.say(cfg.message or "Rate limit exceeded") ngx.say(cfg.message or "Rate limit exceeded")
return false return false
elseif cfg.action == config_module.ACTION_BLOCK then elseif cfg.action == "block" then
-- 封禁返回403 -- 封禁返回403
logger.log_blocked(client_ip, new_count, max_requests, time_window) logger.log_blocked(client_ip, new_count, max_requests, time_window)