From f6c62a3695ef559d537b48854ad964db39fd111d Mon Sep 17 00:00:00 2001 From: thefosk Date: Sun, 30 Aug 2015 13:13:37 +0200 Subject: [PATCH] Removing old format support from rate-limiting. Related to #513 --- kong/plugins/rate-limiting/access.lua | 15 +------- spec/plugins/rate-limiting/access_spec.lua | 43 ---------------------- 2 files changed, 2 insertions(+), 56 deletions(-) diff --git a/kong/plugins/rate-limiting/access.lua b/kong/plugins/rate-limiting/access.lua index d0ae58215480..6e1d0db58af5 100644 --- a/kong/plugins/rate-limiting/access.lua +++ b/kong/plugins/rate-limiting/access.lua @@ -59,17 +59,6 @@ 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 @@ -77,8 +66,8 @@ function _M.execute(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 diff --git a/spec/plugins/rate-limiting/access_spec.lua b/spec/plugins/rate-limiting/access_spec.lua index fa3f59cffaa1..532e6d1c65cf 100644 --- a/spec/plugins/rate-limiting/access_spec.lua +++ b/spec/plugins/rate-limiting/access_spec.lua @@ -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) @@ -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()