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

Added audit_only, fault_tolerant and custom error_code config #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions kong/plugins/scalable-rate-limiter/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function RateLimitingHandler:access(conf)

-- Consumer is identified by ip address or authenticated_credential id
local identifier, err = get_identifier(conf)
local fault_tolerant = conf.fault_tolerant

if err then
kong.log.err(err)
Expand All @@ -88,13 +89,23 @@ function RateLimitingHandler:access(conf)

local usage, stop, err = get_usage(conf, identifier, current_timestamp, limits)
if err then
if not fault_tolerant then
return error(err)
end
kong.log.err("failed to get usage: ", tostring(err))
end

-- If get_usage succeeded and limit has been crossed
if usage and stop then
kong.log.err("API rate limit exceeded")
return kong.response.exit(429, { error = { message = conf.error_message }})
if conf.limit_by == "service" then
kong.log.err("API rate limit exceeded for " .. conf.limit_by .. ":" .. identifier)
elseif conf.limit_by == "header" then
kong.log.err("API rate limit exceeded for header " .. conf.header_name .. ":" .. identifier)
end

if not conf.audit_only then
return kong.response.exit(conf.error_code, { error = { message = conf.error_message }})
end
end

kong.ctx.plugin.timer = function()
Expand Down
23 changes: 23 additions & 0 deletions kong/plugins/scalable-rate-limiter/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ return {
len_min = 0,
},
},
{
error_code = {
description = "Set a custom error code to return when the rate limit is exceeded.",
type = "number",
default = 429,
gt = 0
},
},
{
fault_tolerant = {
description = "A boolean value that determines if the requests should be proxied even if Kong has troubles connecting a redis. If `true`, requests will be proxied anyway, effectively disabling the rate-limiting function until the data store is working again. If `false`, then the clients will see `500` errors.",
type = "boolean",
required = true,
default = true
},
},
{
audit_only = {
description = "Run the rate-limiter in audit mode only. Enabling this will allow all rate-limited requests to pass through while logging for audit purpose",
type = "boolean",
default = false,
},
},
-- {
-- use_app_config = {
-- type = "boolean",
Expand Down
Loading