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

Policy: rate_limit_headers fix cache issues. #1197

Merged
merged 2 commits into from
Jun 18, 2020

Conversation

eloycoto
Copy link
Contributor

When testing rate_limit_headers the number of APICast workers are always set to

  1. When the number of workers increased, the lru_cache is not longer effective
    due to multiple threads.

Because we only have information about usage statistics on Authrep, and this is
using ngx.shared info, this policy also need to use shared information to be
able to reply without doing the authrep in all sessions.

Fix THREESCALE-3795

Signed-off-by: Eloy Coto [email protected]

@eloycoto eloycoto requested a review from a team as a code owner June 15, 2020 14:13
@eloycoto eloycoto force-pushed the FIX-RATELIMITS_HEADERS branch from 8f34ddf to b82f3ff Compare June 15, 2020 14:24
When testing rate_limit_headers the number of APICast workers are always set to
1. When the number of workers increased, the lru_cache is not longer effective
due to multiple threads.

Because we only have information about usage statistics on Authrep, and this is
using ngx.shared info, this policy also need to use shared information to be
able to reply without doing the authrep in all sessions.

Fix THREESCALE-3795

Signed-off-by: Eloy Coto <[email protected]>
@eloycoto eloycoto force-pushed the FIX-RATELIMITS_HEADERS branch from b82f3ff to b489e35 Compare June 15, 2020 14:36
@@ -41,4 +42,25 @@ function _M:reset(max, remaining, reset)
self.reset = countdown_counter.new(reset, now())
end

function _M:export()
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: should this be called tostring or similar for clarity?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not think so, export/import is more declarative when you read the code. tostring was my first idea 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

return _M.Init_empty(usage)
end

local data = stringx.split(exported_data, "#")
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: the "#" could be extracted to a constant that could be used in this method and in export().

local data = cache_entry.import(usage, entry:export())
assert.same(data.limit:__tostring(), "1")
assert.same(data.remaining:__tostring(), "10")
end)
Copy link
Contributor

Choose a reason for hiding this comment

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

You forgot one assert for reset (3).

Copy link
Contributor

Choose a reason for hiding this comment

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

Same for the tests below.

usage:add("foo", 1)
end)

it('if Auth cache is disabled, report the usage', function()
Copy link
Contributor

Choose a reason for hiding this comment

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

Not too important, but in cases like this, I like to put the condition outside the it clause, so the it clause reads like natural English. Something like this:

describe('when the auth cache is disabled', function()
  it('reports the usage', function()
     ...
  )
)

@eloycoto eloycoto force-pushed the FIX-RATELIMITS_HEADERS branch from b1f73c6 to 1c0896d Compare June 18, 2020 13:57
@eloycoto eloycoto force-pushed the FIX-RATELIMITS_HEADERS branch from 1c0896d to ea9133e Compare June 18, 2020 14:00
@eloycoto eloycoto merged commit 28cffcf into 3scale:master Jun 18, 2020
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