Allow to reset state#436
Conversation
grzuy
left a comment
There was a problem hiding this comment.
I can think of 2 (not functionally equal) implementations:
1. `Rack::Attack.reset!` which really resets (deletes) specific keys in cache store and can be used for testing as well as production. But, while this can be implemented for most of cache stores (using https://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html#method-i-delete_matched), this is not (and can't be) supported by memcache (the second most popular cache store), because to reset state, we should know exact key names, which is impossible for production considering rack-attack's dynamic key names nature. Unfortunately, memcache does not support deleting based on patterns/prefixes, like in redis. And need to figure out how to use this with custom, created by user, cache stores. Also, personally, I don't think this is a very popular usecase to reset _production_ rack-attack cache. I have used this once to shrink redis memory usage, implemented as a simple rake task executing `scan` and `del` on redis. I think, for general use, changing keys prefix and let the backed store prune old entries here is a valid solution.
Good call.
I would still consider providing a Rack::Attack.reset! method that depends on the store #delete_matched method (passing the prefix) which will work for redis and memory_store.
For mem_cache users wanting to reset in their test suites will have the option to:
- use the memory_store for the test suite and use this new
.reset!method - clear all mem_cache during tests
2. Implementation only to support user tests. I implemented this version here. Example of usage:# test_helper.rb Rack::Attack.test_mode# some_test_file.rb ... setup do Rack::Attack.cache.store.clear end ...After agreeing on implementation, will add missing tests and documentation.
27c1e04 to
0354bf9
Compare
| else | ||
| raise( | ||
| MisconfiguredStoreError, | ||
| "Configured store #{store.class.name} doesn't respond to #delete_matched method" |
There was a problem hiding this comment.
MisconfiguredStoreError was conceived to let a user know it need to reconfigure rack-attack or nothing will work.
In this particular case, not sure we're exactly there. This would more like "Everything will work fine with your current store but you won't be able to use the #reset! method"? Maybe IncompatibleStoreError for the constant?
|
|
||
| if store.respond_to?(:delete_matched) | ||
| store.delete_matched("#{cache.prefix}*") | ||
| else |
There was a problem hiding this comment.
Sounds like everything in this method implementation is interacting with cache-only stuff (store and prefix).
Should we move reset! inside Cache and make the implementation here just cache.reset!?
0354bf9 to
18e637a
Compare
|
Updated. |
|
Awesome, thank you! |
|
Should |
|
Any chance of getting a new release with this in? |
|
Released in |
Hi @camilova , |
|
Thanks! I had a misconception, but now I know that `.clear_configuration`
is what I was looking for.
|
#249 (comment)
I can think of 2 (not functionally equal) implementations:
Rack::Attack.reset!which really resets (deletes) specific keys in cache store and can be used for testing as well as production. But, while this can be implemented for most of cache stores (using https://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html#method-i-delete_matched), this is not (and can't be) supported by memcache (the second most popular cache store), because to reset state, we should know exact key names, which is impossible for production considering rack-attack's dynamic key names nature. Unfortunately, memcache does not support deleting based on patterns/prefixes, like in redis. And need to figure out how to use this with custom, created by user, cache stores.Also, personally, I don't think this is a very popular usecase to reset production rack-attack cache. I have used this once to shrink redis memory usage, implemented as a simple rake task executing
scananddelon redis. I think, for general use, changing keys prefix and let the backed store prune old entries here is a valid solution.Implementation only to support user tests. I implemented this version here. Example of usage:
After agreeing on implementation, will add missing tests and documentation.