Skip to content

Commit

Permalink
fix(bk-traffic-label): bug when weight is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
wklken committed Nov 7, 2024
1 parent 7e66678 commit b2a71e0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/apisix/plugins/bk-traffic-label.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,24 @@ function _M.check_schema(conf)
return false, "failed to validate the 'match' expression: " .. err
end
end
-- Calculate total weight of all actions

-- Calculate total weight of all actions and preprocess actions to set default weight
local total_weight = 0
for _, action in ipairs(rule.actions) do
total_weight = total_weight + (action.weight or 1)
if action.weight == nil or action.weight <= 0 then
action.weight = 1
end
total_weight = total_weight + action.weight
end

-- Normalize the weight of each action
local accumulated_weight = 0
for i, action in ipairs(rule.actions) do
if i == #rule.actions then
-- Assign the remaining weight to the last action to ensure the total is 100
action.weight = 100 - accumulated_weight
else
action.weight = math.floor((action.weight or 1) / total_weight * 100)
action.weight = math.floor(action.weight / total_weight * 100)
accumulated_weight = accumulated_weight + action.weight
end
end
Expand Down

0 comments on commit b2a71e0

Please sign in to comment.