balancer_by_lua_block {
local b = require "ngx.balancer"
if not ngx.ctx.tries then
ngx.ctx.tries = 0
end
if ngx.ctx.tries < 2 then
local ok, err = b.set_more_tries(1)
if not ok then
return error("failed to set more tries: ", err)
elseif err then
ngx.log(ngx.WARN, "set more tries: ", err)
end
end
ngx.ctx.tries = ngx.ctx.tries + 1
assert(b.set_current_peer("127.0.0.3", 12345))
}
代码示例中,首先一个请求进来,给它设置 ngx.ctx.tries = 0
中间忽略,然后 tries +1,拿到一个 peer 发送给后端。
如果触发了 proxy_nex_upstream,那么这个 ctx 又会回到 balancer_by_lua ,这个时候 tries 变成了 1,然后仍然要 set_current_peer
set_more_tries(1) 这一步到底做了什么?