Authored by jie

upstream list or up or down

... ... @@ -6,6 +6,8 @@ local limit_ip_access_url="http://config.server.yohoops.org/nginx_limit_ip/defau
local nginx_common_conf_url="http://config.server.yohoops.org/nginx_common_conf/default"
local limit_ip_redis=lua_context["redis_limit_ip"]
local upstream_cache=ngx.shared.upstream
local upstream = require "ngx.upstream"
function split_str_list(str,spliter,limit)
local ips={}
... ... @@ -306,3 +308,16 @@ timer_handler(true,2,limit_ip_access_conf_to_worker)
timer_handler(true,2,query_common_conf_to_worker)
function updownstream()
local keys=upstream_cache:get_keys()
for _,k in ipairs(keys) do
local value=upstream_cache:get(k)
local v=cjson.decode(value)
local ok,err=upstream.set_peer_down(v["upstream"],v["backup"],v["id"],v["value"])
if not ok then
ngx.log(ngx.ERR,"up or down stream err:",ngx.worker.id(),value,err)
end
end
end
timer_handler(true,2,updownstream)
\ No newline at end of file
... ...
... ... @@ -20,3 +20,6 @@ lua_context.lua_conf_cache=ngx.shared.ngxconf
lua_context.configs={}
ngx.shared.upstream:flush_all()
ngx.shared.upstream:flush_expired()
... ...
local upstream = require "ngx.upstream"
local json=require "cjson"
local get_servers = upstream.get_servers
local get_upstreams = upstream.get_upstreams
local cache=ngx.shared.upstream
function list()
local t={}
local us=get_upstreams()
for _,u in ipairs(us) do
local peers,err = upstream.get_primary_peers(u)
if err then
ngx.say("failed to get servers in upstream ", u)
return
end
local d={}
d["name"]=u
d["value"]={}
for _,p in ipairs(peers) do
local s={}
s["id"]=p.id
s["down"]=p.down and p.down or false
s["name"]=p.name
s["backup"]=false
table.insert(d["value"],s)
end
peers,err = upstream.get_backup_peers(u)
if err then
ngx.say("failed to get servers in upstream ", u)
return
end
for _,p in ipairs(peers) do
local s={}
s["id"]=p.id
s["down"]=p.down and p.down or false
s["name"]=p.name
s["backup"]=true
table.insert(d["value"],s)
end
table.insert(t,d)
end
ngx.header["Content-type"]="application/json;charset=utf-8"
ngx.say(json.encode(t))
end
function upordown(upstream_name,is_backup,peer_id,down_value)
local ok, err = upstream.set_peer_down(upstream_name, is_backup, peer_id, down_value)
local t={}
t["upstream"]=upstream_name
t["backup"]=is_backup
t["id"]=peer_id
t["value"]=down_value
local suc=cache:set("d:" .. upstream_name .. ":" .. tostring(id) .. ":" .. tostring(is_backup),json.encode(t))
return suc
end
local args=ngx.req.get_uri_args()
local method=args["method"]
if method == "list" then
list()
elseif(method=="down" or method=="up") then
local upstream=args["upstream"]
local backup=args["backup"]=="true" and true or false
local id=tonumber(args["id"])
local down= method=="down" and true or false
local suc=upordown(upstream,backup,id,down)
local t={}
t["suc"]=suc
ngx.header["Content-type"]="application/json;charset=utf-8"
ngx.say(json.encode(t))
end
... ...
... ... @@ -33,4 +33,8 @@ server {
';
}
location = /upstreams {
content_by_lua_file "conf/lua/upstream.lua";
}
}
\ No newline at end of file
... ...
... ... @@ -109,7 +109,7 @@ http
# where the lua package exists
lua_package_path "/Data/local/openresty-1.9.15.1/nginx/conf/lua/?.lua;;";
init_by_lua_file "conf/lua/init_lua.lua";
lua_shared_dict luacachedb 500m;
lua_shared_dict upstream 2m;
lua_shared_dict malips 10m;
lua_shared_dict ngxconf 20m;
init_worker_by_lua_file "conf/lua/init_config_worker.lua";
... ...