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

How to clear the cache of the Rack Attack only #389

Closed
ndvbd opened this issue Oct 4, 2018 · 10 comments
Closed

How to clear the cache of the Rack Attack only #389

ndvbd opened this issue Oct 4, 2018 · 10 comments

Comments

@ndvbd
Copy link

ndvbd commented Oct 4, 2018

How to clear the cache of the Rack Attack only? (clear state)
One way would be to simply change the cache prefix of the rack attack. How do we do that?

@grzuy
Copy link
Collaborator

grzuy commented Oct 8, 2018

There's not an easy way to do that currently.

We should add that as a new feature.

@ndvbd
Copy link
Author

ndvbd commented Oct 8, 2018

How can I change the cache prefix for the rack attack? It should be somewhere, shouldn't it? If I change the prefix, it will solve the problem, as it will invalidate the relevant cache entries.

@grzuy
Copy link
Collaborator

grzuy commented Oct 11, 2018

Yes, changing the prefix would be kind of workaround to start with a clean state.

Rack::Attack.cache.prefix = ... # default is 'rack::attack'

We should at some point provide a way to clean all the data without needing to hack the cache prefix anyway.

@grzuy
Copy link
Collaborator

grzuy commented Oct 9, 2019

@ndvbd Just curious, what was your motivation for clearing the cache? Were you needing that during test cases so to achieve proper test case isolation or was that a need for you on production?

@grzuy
Copy link
Collaborator

grzuy commented Oct 9, 2019

We should consider having a Rack::Attack.reset! method to reset all Rack::Attack's state (i.e. the cache).

Also discussed in #249 (comment).

@ndvbd
Copy link
Author

ndvbd commented Oct 10, 2019

@grzuy Yes. You sometimes need to clean the cache, especially during tests, but also on production. It's a good thing to have.

@lmansur
Copy link
Contributor

lmansur commented Oct 10, 2019

@ndvbd what is your context on cleaning the cache on production? It's not something I see very often, so I'm just curious

@javierjulio
Copy link

For others that need this currently in their test suite, this is what we use in our RackAttack specific test cases:

  before do
    Rack::Attack.enabled = true
    Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
  end

  after do
    Rack::Attack.cache.store.clear
    Rack::Attack.enabled = false
  end

Note that this makes use of the new enabled flag in RackAttack 6.2.0.

I like the suggestion of Rack::Attack.reset! or even another solution. Perhaps a method specific to clearing the cache or a test mode flag as suggested in #436.

@grzuy
Copy link
Collaborator

grzuy commented Oct 21, 2019

For others that need this currently in their test suite, this is what we use in our RackAttack specific test cases:

  before do
    Rack::Attack.enabled = true
    Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
  end

  after do
    Rack::Attack.cache.store.clear
    Rack::Attack.enabled = false
  end

Note that this makes use of the new enabled flag in RackAttack 6.2.0.

I like the suggestion of Rack::Attack.reset! or even another solution. Perhaps a method specific to clearing the cache or a test mode flag as suggested in #436.

Hi @javierjulio ,

Going through this again, I am not sure we'll have something in Rack::Attack which will avoid you having to make an extra call to clear the storage.

I mean, you either:

  1. Have Rack::Attack completely disabled for tests, via enabled = false or by using NullStore (config.cache = :null_store in rails).
  2. Have Rack::Attack enabled for tests and some sort of reset state call (today Rack::Attack.cache.store.clear) somewhere in between test cases.

Were you expecting something different in that regard?
If so, what specifically?

Thanks.

@grzuy
Copy link
Collaborator

grzuy commented Oct 23, 2019

Rack::Attack.reset! has been merged in #436.

@grzuy grzuy closed this as completed Oct 23, 2019
@grzuy grzuy unpinned this issue Oct 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants