Skip to content

Commit

Permalink
Removing old format support from rate-limiting. Related to #513
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Aug 30, 2015
1 parent 61c6eae commit f6c62a3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 56 deletions.
15 changes: 2 additions & 13 deletions kong/plugins/rate-limiting/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,15 @@ function _M.execute(conf)
-- Consumer is identified by ip address or authenticated_entity id
local identifier = get_identifier()

-- Handle previous version of the rate-limiting plugin
local old_format = false
if conf.period and conf.limit then
old_format = true
conf[conf.period] = conf.limit -- Adapt to new format

-- Delete old properties
conf.period = nil
conf.limit = nil
end

local api_id = ngx.ctx.api.id

-- Load current metric for configured period
local usage, stop = get_usage(api_id, identifier, current_timestamp, conf)

-- Adding headers
for k, v in pairs(usage) do
ngx.header[constants.HEADERS.RATELIMIT_LIMIT..(old_format and "" or "-"..k)] = v.limit
ngx.header[constants.HEADERS.RATELIMIT_REMAINING..(old_format and "" or "-"..k)] = math.max(0, (stop == nil or stop == k) and v.remaining - 1 or v.remaining) -- -increment_value for this current request
ngx.header[constants.HEADERS.RATELIMIT_LIMIT.."-"..k] = v.limit
ngx.header[constants.HEADERS.RATELIMIT_REMAINING.."-"..k] = math.max(0, (stop == nil or stop == k) and v.remaining - 1 or v.remaining) -- -increment_value for this current request
end

-- If limit is exceeded, terminate the request
Expand Down
43 changes: 0 additions & 43 deletions spec/plugins/rate-limiting/access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,6 @@ describe("RateLimiting Plugin", function()
}
}

-- Updating API test6.com with old plugin value, to check retrocompatibility
local dao_factory = spec_helper.get_env().dao_factory
-- Find API
local res, err = dao_factory.apis:find_by_keys({public_dns = 'test6.com'})
if err then error(err) end
-- Find Plugin Configuration
local res, err = dao_factory.plugins_configurations:find_by_keys({api_id = res[1].id})
if err then error(err) end
-- Set old value
local plugin_configuration = res[1]
plugin_configuration.value = {
period = "minute",
limit = 6
}
-- Update plugin configuration
local _, err = dao_factory.plugins_configurations:execute(
"update plugins_configurations SET value = '{\"limit\":6, \"period\":\"minute\"}' WHERE id = "..plugin_configuration.id.." and name = 'rate-limiting'")
if err then error(err) end

spec_helper.start_kong()
end)

Expand Down Expand Up @@ -120,30 +101,6 @@ describe("RateLimiting Plugin", function()

describe("With authentication", function()

describe("Old plugin format", function()

it("should get blocked if exceeding limit", function()
wait()

-- Default rate-limiting plugin for this API says 6/minute
local limit = 6

for i = 1, limit do
local _, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test6.com"})
assert.are.equal(200, status)
assert.are.same(tostring(limit), headers["x-ratelimit-limit"])
assert.are.same(tostring(limit - i), headers["x-ratelimit-remaining"])
end

-- Third query, while limit is 2/minute
local response, status = http_client.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test6.com"})
local body = cjson.decode(response)
assert.are.equal(429, status)
assert.are.equal("API rate limit exceeded", body.message)
end)

end)

describe("Default plugin", function()

it("should get blocked if exceeding limit", function()
Expand Down

0 comments on commit f6c62a3

Please sign in to comment.