Skip to content

Commit

Permalink
perf(ip-restriction) cache globals and use table.size()
Browse files Browse the repository at this point in the history
Also add checks to make sure #782 does not happen (even if I could not
reproduce).
  • Loading branch information
thibaultcha committed Jan 14, 2016
1 parent af8baa4 commit e1f5ff7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
16 changes: 5 additions & 11 deletions kong/plugins/ip-restriction/access.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
local iputils = require "resty.iputils"
local responses = require "kong.tools.responses"
local utils = require "kong.tools.utils"

local _M = {}

function _M.execute(conf)
local block = false
local remote_addr = ngx.var.remote_addr

if utils.table_size(conf.blacklist) > 0 then
if iputils.ip_in_cidrs(ngx.var.remote_addr, conf._blacklist_cache) then
block = true
end
if conf._blacklist_cache and #conf._blacklist_cache > 0 then
block = iputils.ip_in_cidrs(remote_addr, conf._blacklist_cache)
end

if utils.table_size(conf.whitelist) > 0 then
if iputils.ip_in_cidrs(ngx.var.remote_addr, conf._whitelist_cache) then
block = false
else
block = true
end
if conf._whitelist_cache and #conf._whitelist_cache > 0 then
block = not iputils.ip_in_cidrs(remote_addr, conf._whitelist_cache)
end

if block then
Expand Down
5 changes: 4 additions & 1 deletion kong/plugins/ip-restriction/init_worker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ local iputils = require "resty.iputils"
local _M = {}

function _M.execute()
iputils.enable_lrucache()
local ok, err = iputils.enable_lrucache()
if not ok then
ngx.log(ngx.ERR, "[ip-restriction] Could not enable lrucache: ", err)
end
end

return _M

0 comments on commit e1f5ff7

Please sign in to comment.