Skip to content

Commit

Permalink
optimize: local cache global variable and avoid single lines over 80
Browse files Browse the repository at this point in the history
    characters.
  • Loading branch information
membphis committed Sep 24, 2019
1 parent 9b66f2a commit 1ce68c8
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions rootfs/etc/nginx/lua/util.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
local string_len = string.len
local string_sub = string.sub
local string = string
local string_len = string.len
local string_sub = string.sub
local string_format = string.format
local pairs = pairs
local tonumber = tonumber
local getmetatable = getmetatable
local type = type
local next = next
local table = table


local _M = {}

Expand Down Expand Up @@ -28,8 +36,8 @@ function _M.lua_ngx_var(ngx_var)
end

-- normalize_endpoints takes endpoints as an array of endpoint objects
-- and returns a table where keys are string that's endpoint.address .. ":" .. endpoint.port
-- and values are all true
-- and returns a table where keys are string that's
-- endpoint.address .. ":" .. endpoint.port and values are all true
local function normalize_endpoints(endpoints)
local normalized_endpoints = {}

Expand All @@ -48,7 +56,8 @@ end
-- Both return values are normalized (ip:port).
function _M.diff_endpoints(old, new)
local endpoints_added, endpoints_removed = {}, {}
local normalized_old, normalized_new = normalize_endpoints(old), normalize_endpoints(new)
local normalized_old = normalize_endpoints(old)
local normalized_new = normalize_endpoints(new)

for endpoint_string, _ in pairs(normalized_old) do
if not normalized_new[endpoint_string] then
Expand Down Expand Up @@ -120,7 +129,8 @@ local function tablelength(T)
end
_M.tablelength = tablelength

-- replaces special character value a with value b for all occurences in a string
-- replaces special character value a with value b for all occurences in a
-- string
local function replace_special_char(str, a, b)
return string.gsub(str, "%" .. a, b)
end
Expand Down

0 comments on commit 1ce68c8

Please sign in to comment.