Skip to content

Commit

Permalink
libruleset: reduce overhead if auth not required
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Nov 29, 2024
1 parent 726739d commit 2d43f58
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions libruleset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,6 @@ local ruleset = {}
_G.secrets = _G.secrets or {}

local function authenticate(addr, username, password)
if not config.auth_required then
-- authenticate is not required
return true
end
local auth = table.get(_G, "ruleset", "authenticate")
if auth then
return auth(addr, username, password)
Expand All @@ -906,32 +902,28 @@ local function authenticate(addr, username, password)
return false
end

function ruleset.resolve(addr, username, password)
_G.num_requests = _G.num_requests + 1
if not authenticate(addr, username, password) then
return nil
local function with_authenticate(f)
if not config.auth_required then
-- authenticate is not required
return function(addr, username, password)
_G.num_requests = _G.num_requests + 1
_G.num_authorized = _G.num_authorized + 1
return f(addr)
end
end
_G.num_authorized = _G.num_authorized + 1
return resolve_(addr)
end

function ruleset.route(addr, username, password)
_G.num_requests = _G.num_requests + 1
if not authenticate(addr, username, password) then
return nil
return function(addr, username, password)
_G.num_requests = _G.num_requests + 1
if not authenticate(addr, username, password) then
return nil
end
_G.num_authorized = _G.num_authorized + 1
return f(addr)
end
_G.num_authorized = _G.num_authorized + 1
return route_(addr)
end

function ruleset.route6(addr, username, password)
_G.num_requests = _G.num_requests + 1
if not authenticate(addr, username, password) then
return nil
end
_G.num_authorized = _G.num_authorized + 1
return route6_(addr)
end
ruleset.resolve = with_authenticate(resolve_)
ruleset.route = with_authenticate(route_)
ruleset.route6 = with_authenticate(route6_)

function ruleset.tick(now)
stat_requests:push(num_requests)
Expand Down

0 comments on commit 2d43f58

Please sign in to comment.