Authored by jie

添加版本的概念,版本增加才重新 up/down upstream

@@ -308,14 +308,28 @@ timer_handler(true,2,limit_ip_access_conf_to_worker) @@ -308,14 +308,28 @@ timer_handler(true,2,limit_ip_access_conf_to_worker)
308 timer_handler(true,2,query_common_conf_to_worker) 308 timer_handler(true,2,query_common_conf_to_worker)
309 309
310 310
  311 +local stream_ctx={}
311 function updownstream() 312 function updownstream()
312 local keys=upstream_cache:get_keys() 313 local keys=upstream_cache:get_keys()
  314 +
313 for _,k in ipairs(keys) do 315 for _,k in ipairs(keys) do
  316 + if string.sub(k,1,1)=="d" then
  317 +
  318 + local vKey="v" .. string.sub(k,2)
  319 + local version=upstream_cache:get(vKey)
  320 +
314 local value=upstream_cache:get(k) 321 local value=upstream_cache:get(k)
315 local v=cjson.decode(value) 322 local v=cjson.decode(value)
  323 +
  324 + if ( not stream_ctx[vKey] ) or stream_ctx[vKey] < version then
316 local ok,err=upstream.set_peer_down(v["upstream"],v["backup"],v["id"],v["value"]) 325 local ok,err=upstream.set_peer_down(v["upstream"],v["backup"],v["id"],v["value"])
317 if not ok then 326 if not ok then
318 ngx.log(ngx.ERR,"up or down stream err:",ngx.worker.id(),value,err) 327 ngx.log(ngx.ERR,"up or down stream err:",ngx.worker.id(),value,err)
  328 + else
  329 + stream_ctx[vKey]=version
  330 + end
  331 + end
  332 +
319 end 333 end
320 end 334 end
321 end 335 end
@@ -48,17 +48,22 @@ end @@ -48,17 +48,22 @@ end
48 48
49 49
50 function upordown(upstream_name,is_backup,peer_id,down_value) 50 function upordown(upstream_name,is_backup,peer_id,down_value)
51 - local ok, err = upstream.set_peer_down(upstream_name, is_backup, peer_id, down_value)  
52 local t={} 51 local t={}
53 t["upstream"]=upstream_name 52 t["upstream"]=upstream_name
54 t["backup"]=is_backup 53 t["backup"]=is_backup
55 t["id"]=peer_id 54 t["id"]=peer_id
56 t["value"]=down_value 55 t["value"]=down_value
57 - local suc=cache:set("d:" .. upstream_name .. ":" .. tostring(id) .. ":" .. tostring(is_backup),json.encode(t),300) 56 + local key="d:" .. upstream_name .. ":" .. tostring(id) .. ":" .. tostring(is_backup)
  57 + local vKey="v:" .. upstream_name .. ":" .. tostring(id) .. ":" .. tostring(is_backup)
  58 + cache:add(vKey,0)
  59 + local v,err=cache:incr(vKey,1)
  60 + if not v then
  61 + return false
  62 + end
  63 + local suc=cache:set(key,json.encode(t))
58 return suc 64 return suc
59 end 65 end
60 66
61 -  
62 local args=ngx.req.get_uri_args() 67 local args=ngx.req.get_uri_args()
63 local method=args["method"] 68 local method=args["method"]
64 if method == "list" then 69 if method == "list" then