Authored by chunhua.zhang

支持配置error code

@@ -125,21 +125,22 @@ local query_limit_ip_access_conf=function() @@ -125,21 +125,22 @@ local query_limit_ip_access_conf=function()
125 if property then 125 if property then
126 local source=property["source"] 126 local source=property["source"]
127 if source then 127 if source then
128 - local limit_ip_access={}  
129 - 128 + local limit_ip_access={ degrades = {} }
  129 +
130 limit_ip_access["is_open"]=source["is_open"] 130 limit_ip_access["is_open"]=source["is_open"]
131 131
132 local white_ips={} 132 local white_ips={}
133 - local degrade_methods = {}  
134 for k,v in pairs(source) do 133 for k,v in pairs(source) do
135 if string.find(k,"^white_ips%[*") then 134 if string.find(k,"^white_ips%[*") then
136 table.insert(white_ips,v) 135 table.insert(white_ips,v)
137 elseif string.find(k,"^degrade_methods%[*") then 136 elseif string.find(k,"^degrade_methods%[*") then
138 - table.insert(degrade_methods,v) 137 + d_method = split_str_list("v", ",", 2)
  138 + local e_code = d_method[2]
  139 + if e_code == nil then e_code = 9999991 end
  140 + limit_ip_access.degrades[d_method[1]] = e_code
139 end 141 end
140 end 142 end
141 limit_ip_access["white_ips"]=white_ips 143 limit_ip_access["white_ips"]=white_ips
142 - limit_ip_access["degrade_methods"]=degrade_methods  
143 lua_context.lua_conf_cache:set("limit_ip_access",cjson.encode(limit_ip_access)) 144 lua_context.lua_conf_cache:set("limit_ip_access",cjson.encode(limit_ip_access))
144 end 145 end
145 end 146 end
@@ -43,23 +43,19 @@ end @@ -43,23 +43,19 @@ end
43 function do_degrade() 43 function do_degrade()
44 local method = http_request.get_method() 44 local method = http_request.get_method()
45 45
46 - if limit_ip_config and limit_ip_config.degrade_methods then  
47 - if contains(limit_ip_config.degrade_methods,method) then  
48 - ngx.log(ngx.ERR, "[Degraded]: ", method) 46 + if limit_ip_config and limit_ip_config.degrades and limit_ip_config.degrades[method] then
  47 + ngx.log(ngx.ERR, "[Degraded]: ", method)
  48 + local err_code = limit_ip_config.degrades[method]
  49 + local err_msg = default_err_msg
49 ngx.header["Content-Type"]="application/json;charset=utf-8" 50 ngx.header["Content-Type"]="application/json;charset=utf-8"
50 - local rsp_body = '{"code":9999991,"message":"limited"}' 51 + local msg='{"code":' .. err_code .. ',"message":"'.. err_msg .. '"}'
51 ngx.say(rsp_body) 52 ngx.say(rsp_body)
52 ngx.exit(ngx.HTTP_OK) 53 ngx.exit(ngx.HTTP_OK)
53 - end  
54 end 54 end
55 end 55 end
56 56
57 -function contains(t, e)  
58 - for i = 1,#t do  
59 - if t[i] == e then return true end  
60 - end  
61 - return false  
62 -end 57 +
  58 +
63 59
64 -------- function: doing rate limit by key[interface]----- 60 -------- function: doing rate limit by key[interface]-----
65 function rate_limit() 61 function rate_limit()