-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
perf(token_is_current?): add simplistic cache to reduce overhead of redundant token checks during validation calls #272
Conversation
…edundant token checks during validation calls
425594e
to
27cad66
Compare
What about the memory usage ? |
should be negligible @nicolas-besnard ... the cache resets at 1000 entries and each entry uses < 100 bytes (83 fixed-length for the key) + boolean value. So ... less < 100k for the full collection. |
FWIW - we saw about this reduce overhead from an additional ~75ms -> ~5ms (once cached) per request |
Ok, you can do what ever you want, I don't care anymore about my memory :D Nice PR ;) |
Gonna run this a little longer in prod before merge, but so far, it's saving A LOT of time on our requests (we have token rotation disabled, FWIW). |
Can't find any downside to this and it's speeding everything along beautifully. Merging! |
…_perf perf(token_is_current?): add simplistic cache to reduce overhead of redundant token checks during validation calls
Fantastic work, thanks @booleanbetrayal!!! |
Thanks to @nbrustein =] |
👍 |
When are you bumping this gem to a new version ? I'd love to test this feature in production ;) |
@nicolas-besnard - Just bumped and pushed 0.1.32.beta10 to RubyGems. Let me know if you have any issues! |
Within
DeviseTokenAuth::Concerns::User#token_is_valid?
we're seeing theBCrypt::Password.new(token_hash)
+ string comparison (casting) operation itself take approximately 75ms on average when validating tokens, which makes it hard / impossible to reach our goal of < 200ms response times for request handling.This PR adds a very simplistic caching mechanism that will lookup the check value, saving unnecessary overhead on redundant token / hash checks. It can be iterated on further if a more robust caching implementation is needed / desired.
This will primarily benefit environments where
change_headers_on_each_request
is disabled, but will still help requests that occur within thebatch_request_buffer_throttle
threshold.Thanks to @nbrustein for the implementation.