Authored by chunhua.zhang

支持配置error code

... ... @@ -125,21 +125,22 @@ local query_limit_ip_access_conf=function()
if property then
local source=property["source"]
if source then
local limit_ip_access={}
local limit_ip_access={ degrades = {} }
limit_ip_access["is_open"]=source["is_open"]
local white_ips={}
local degrade_methods = {}
for k,v in pairs(source) do
if string.find(k,"^white_ips%[*") then
table.insert(white_ips,v)
elseif string.find(k,"^degrade_methods%[*") then
table.insert(degrade_methods,v)
d_method = split_str_list("v", ",", 2)
local e_code = d_method[2]
if e_code == nil then e_code = 9999991 end
limit_ip_access.degrades[d_method[1]] = e_code
end
end
limit_ip_access["white_ips"]=white_ips
limit_ip_access["degrade_methods"]=degrade_methods
lua_context.lua_conf_cache:set("limit_ip_access",cjson.encode(limit_ip_access))
end
end
... ...
... ... @@ -43,23 +43,19 @@ end
function do_degrade()
local method = http_request.get_method()
if limit_ip_config and limit_ip_config.degrade_methods then
if contains(limit_ip_config.degrade_methods,method) then
ngx.log(ngx.ERR, "[Degraded]: ", method)
if limit_ip_config and limit_ip_config.degrades and limit_ip_config.degrades[method] then
ngx.log(ngx.ERR, "[Degraded]: ", method)
local err_code = limit_ip_config.degrades[method]
local err_msg = default_err_msg
ngx.header["Content-Type"]="application/json;charset=utf-8"
local rsp_body = '{"code":9999991,"message":"limited"}'
local msg='{"code":' .. err_code .. ',"message":"'.. err_msg .. '"}'
ngx.say(rsp_body)
ngx.exit(ngx.HTTP_OK)
end
end
end
function contains(t, e)
for i = 1,#t do
if t[i] == e then return true end
end
return false
end
-------- function: doing rate limit by key[interface]-----
function rate_limit()
... ...