Skip to content

Commit

Permalink
change default scope to service and use service_id in the context
Browse files Browse the repository at this point in the history
  • Loading branch information
y-tabata committed May 14, 2018
1 parent 5609be3 commit d49179b
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 93 deletions.
18 changes: 3 additions & 15 deletions gateway/src/apicast/policy/rate_limit/apicast-policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@
"scope": {
"type": "string",
"description": "Scope of the key",
"default": "global",
"default": "service",
"oneOf": [{
"enum": ["global"],
"description": "Global scope, affecting to all services"
}, {
"enum": ["service"],
"description": "Service scope, affecting to one service"
}]
},
"service_name": {
"type": "string",
"description": "Name of service, necessary for service scope"
}
}
},
Expand Down Expand Up @@ -72,18 +68,14 @@
"scope": {
"type": "string",
"description": "Scope of the key",
"default": "global",
"default": "service",
"oneOf": [{
"enum": ["global"],
"description": "Global scope, affecting to all services"
}, {
"enum": ["service"],
"description": "Service scope, affecting to one service"
}]
},
"service_name": {
"type": "string",
"description": "Name of service, necessary for service scope"
}
}
},
Expand Down Expand Up @@ -116,18 +108,14 @@
"scope": {
"type": "string",
"description": "Scope of the key",
"default": "global",
"default": "service",
"oneOf": [{
"enum": ["global"],
"description": "Global scope, affecting to all services"
}, {
"enum": ["service"],
"description": "Service scope, affecting to one service"
}]
},
"service_name": {
"type": "string",
"description": "Name of service, necessary for service scope"
}
}
},
Expand Down
30 changes: 15 additions & 15 deletions gateway/src/apicast/policy/rate_limit/rate_limit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ local function init_error_settings(limits_exceeded_error, configuration_error)
return error_settings
end

local function initialize_redis_records(type, redis, seed, limiters)
local function initialize_redis_records(type, redis, seed, limiters, service_id)
for _, limiter in ipairs(limiters) do
local key
if limiter.key.scope == "service" then
key = format("%s_%s_%s", limiter.key.service_name, type, limiter.key.name)
else
if limiter.key.scope == "global" then
key = format("%s_%s", type, limiter.key.name)
else
key = format("%s_%s_%s", service_id, type, limiter.key.name)
end

local seed_key = format("%s_seed", key)
Expand Down Expand Up @@ -147,7 +147,7 @@ local function initialize_redis_records(type, redis, seed, limiters)
end
end

local function build_limiters_and_keys(type, limiters, redis, error_settings)
local function build_limiters_and_keys(type, limiters, redis, error_settings, service_id)
local res_limiters = {}
local res_keys = {}

Expand All @@ -164,10 +164,10 @@ local function build_limiters_and_keys(type, limiters, redis, error_settings)
insert(res_limiters, lim)

local key
if limiter.key.scope == "service" then
key = format("%s_%s_%s", limiter.key.service_name, type, limiter.key.name)
else
if limiter.key.scope == "global" then
key = format("%s_%s", type, limiter.key.name)
else
key = format("%s_%s_%s", service_id, type, limiter.key.name)
end

insert(res_keys, key)
Expand All @@ -190,7 +190,7 @@ function _M.new(config)
return self
end

function _M:access()
function _M:access(context)
local red
if self.redis_url then
local rederr
Expand All @@ -201,19 +201,19 @@ function _M:access()
return
end

initialize_redis_records('connections', red, self.seed, self.connection_limiters)
initialize_redis_records('leaky_bucket', red, self.seed, self.leaky_bucket_limiters)
initialize_redis_records('fixed_window', red, self.seed, self.fixed_window_limiters)
initialize_redis_records('connections', red, self.seed, self.connection_limiters, context.service.id)
initialize_redis_records('leaky_bucket', red, self.seed, self.leaky_bucket_limiters, context.service.id)
initialize_redis_records('fixed_window', red, self.seed, self.fixed_window_limiters, context.service.id)
end

local conn_limiters, conn_keys = build_limiters_and_keys(
'connections', self.connection_limiters, red, self.error_settings)
'connections', self.connection_limiters, red, self.error_settings, context.service.id)

local leaky_bucket_limiters, leaky_bucket_keys = build_limiters_and_keys(
'leaky_bucket', self.leaky_bucket_limiters, red, self.error_settings)
'leaky_bucket', self.leaky_bucket_limiters, red, self.error_settings, context.service.id)

local fixed_window_limiters, fixed_window_keys = build_limiters_and_keys(
'fixed_window', self.fixed_window_limiters, red, self.error_settings)
'fixed_window', self.fixed_window_limiters, red, self.error_settings, context.service.id)

local limiters = {}
local limiter_groups = { conn_limiters, leaky_bucket_limiters, fixed_window_limiters }
Expand Down
Loading

0 comments on commit d49179b

Please sign in to comment.