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

Rate Limit policy to take into account changes in the config #703

Merged
merged 5 commits into from
Jun 15, 2018

Conversation

y-tabata
Copy link
Contributor

@y-tabata y-tabata commented May 14, 2018

This PR addresses to Issue #667.

@mikz
Copy link
Contributor

mikz commented May 14, 2018

@y-tabata to help with the code review, could we leave out b04fbf2 to its own PR?

@y-tabata
Copy link
Contributor Author

@mikz Sure, I will divide this PR.

@y-tabata y-tabata force-pushed the improve-rate-limit-policy-retry branch from 304d623 to b04fbf2 Compare May 14, 2018 08:39
@y-tabata y-tabata changed the title Improve rate limit policy Rate Limit policy to take into account changes in the config May 14, 2018
@y-tabata y-tabata force-pushed the improve-rate-limit-policy-retry branch from 1b59fe3 to cc63df3 Compare June 12, 2018 02:49
@y-tabata y-tabata requested a review from a team as a code owner June 12, 2018 02:49
@y-tabata y-tabata force-pushed the improve-rate-limit-policy-retry branch 3 times, most recently from 13301cc to 396f259 Compare June 12, 2018 02:55
@@ -160,6 +161,7 @@ function _M.new(config)
self.redis_url = config.redis_url
self.error_settings = init_error_settings(
config.limits_exceeded_error, config.configuration_error)
self.timestamp = ngx.time()
Copy link
Contributor

@mikz mikz Jun 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to store time in self.timestamp. Local variable will be ok too.

:new() is evaluated only when the policy is initialized, if you have multiple gateways that initialize at different times then they'd not share the same key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -133,9 +134,9 @@ local function build_limiters_and_keys(type, limiters, redis, error_settings, co
local key = limiter.template_string:render(
ngx_variable.available_context(context))
if limiter.key.scope == "global" then
key = format("%s_%s", type, key)
key = format("%d_%s_%s", timestamp + window, type, key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't use just timestamp + window. In case of multiple gateways they all need to generate the same key. That can be achieved by generating a time bucket.

The formula is: time - (time % window). That will for the whole time of window return the same value and then flip to the next one when the window is over.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@mikz mikz mentioned this pull request Jun 12, 2018
4 tasks
@y-tabata y-tabata force-pushed the improve-rate-limit-policy-retry branch from ddde20b to e39eb9d Compare June 14, 2018 04:30
@@ -168,23 +168,26 @@ describe('Rate limit policy', function()
rate_limit_policy:access(context)
rate_limit_policy:access(context)

local redis_key = redis:keys('*_fixed_window_test3')[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to stub ngx.time() to return some fixed time: stub(ngx,time, function() return ... end).

To not duplicate the logic of calculating the fixed window, it could be extracted into a function that exported from own file, so it can be reused by the policy and the test.
Actually the more I think about it then how the limiters and keys are created could easily live in own module and could be unit tested standalone without the policy.

But it is no big deal, we can tackle that in the future. Definitely not a blocker for merging this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use stub.

@mikz mikz merged commit 85a0a94 into 3scale:master Jun 15, 2018
@y-tabata y-tabata deleted the improve-rate-limit-policy-retry branch June 15, 2018 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants