in apisix/plugins/traffic-split.lua [229:302]
function _M.access(conf, ctx)
if not conf or not conf.rules then
return
end
local weighted_upstreams
local match_passed = true
for _, rule in ipairs(conf.rules) do
if rule.weighted_upstreams then
for _, wupstream in ipairs(rule.weighted_upstreams) do
local ups_id = wupstream.upstream_id
if ups_id then
local ups = upstream.get_by_id(ups_id)
if not ups then
return 500, "failed to fetch upstream info by "
.. "upstream id: " .. ups_id
end
end
end
end
if not rule.match then
match_passed = true
weighted_upstreams = rule.weighted_upstreams
break
end
for _, single_match in ipairs(rule.match) do
local expr, err = expr.new(single_match.vars)
if err then
core.log.error("vars expression does not match: ", err)
return 500, err
end
match_passed = expr:eval(ctx.var)
if match_passed then
break
end
end
if match_passed then
weighted_upstreams = rule.weighted_upstreams
break
end
end
core.log.info("match_passed: ", match_passed)
if not match_passed then
return
end
local rr_up, err = lrucache(weighted_upstreams, nil, new_rr_obj, weighted_upstreams)
if not rr_up then
core.log.error("lrucache roundrobin failed: ", err)
return 500
end
local upstream = rr_up:find()
if upstream and type(upstream) == "table" then
core.log.info("upstream: ", core.json.encode(upstream))
return set_upstream(upstream, ctx)
elseif upstream and upstream ~= "plugin#upstream#is#empty" then
ctx.upstream_id = upstream
core.log.info("upstream_id: ", upstream)
return
end
ctx.upstream_id = nil
core.log.info("route_up: ", upstream)
return
end