Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(*-rate-limiting) optimize redis pipelining #2956

Merged
merged 2 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions kong/plugins/rate-limiting/policies/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ return {
end
end

local keys = {}
local expirations = {}
local idx = 0
local periods = timestamp.get_timestamps(current_timestamp)
for period, period_date in pairs(periods) do
if limits[period] then
Expand All @@ -109,20 +112,27 @@ return {
return nil, err
end

red:init_pipeline((not exists or exists == 0) and 2 or 1)
red:incrby(cache_key, value)
idx = idx + 1
keys[idx] = cache_key
if not exists or exists == 0 then
red:expire(cache_key, EXPIRATIONS[period])
expirations[idx] = EXPIRATIONS[period]
end
end
end

local _, err = red:commit_pipeline()
if err then
ngx_log(ngx.ERR, "failed to commit pipeline in Redis: ", err)
return nil, err
end
red:init_pipeline()
for i = 1, idx do
red:incrby(keys[i], value)
if expirations[i] then
red:expire(keys[i], expirations[i])
end
end

local _, err = red:commit_pipeline()
if err then
ngx_log(ngx.ERR, "failed to commit pipeline in Redis: ", err)
return nil, err
end
local ok, err = red:set_keepalive(10000, 100)
if not ok then
ngx_log(ngx.ERR, "failed to set Redis keepalive: ", err)
Expand Down
24 changes: 17 additions & 7 deletions kong/plugins/response-ratelimiting/policies/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ return {
end
end

local keys = {}
local expirations = {}
local idx = 0
local periods = timestamp.get_timestamps(current_timestamp)
for period, period_date in pairs(periods) do
local cache_key = get_local_key(api_id, identifier, period_date, name, period)
Expand All @@ -108,19 +111,26 @@ return {
return nil, err
end

red:init_pipeline((not exists or exists == 0) and 2 or 1)
red:incrby(cache_key, value)
idx = idx + 1
keys[idx] = cache_key
if not exists or exists == 0 then
red:expire(cache_key, EXPIRATIONS[period])
expirations[idx] = EXPIRATIONS[period]
end
end

local _, err = red:commit_pipeline()
if err then
ngx_log(ngx.ERR, "failed to commit pipeline in Redis: ", err)
return nil, err
red:init_pipeline()
for i = 1, idx do
red:incrby(keys[i], value)
if expirations[i] then
red:expire(keys[i], expirations[i])
end
end

local _, err = red:commit_pipeline()
if err then
ngx_log(ngx.ERR, "failed to commit pipeline in Redis: ", err)
return nil, err
end
local ok, err = red:set_keepalive(10000, 100)
if not ok then
ngx_log(ngx.ERR, "failed to set Redis keepalive: ", err)
Expand Down