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

Hash request URIs before using them as a cache key #181

Merged
merged 1 commit into from
Nov 26, 2018

Conversation

timrogers
Copy link
Contributor

Issue #132 notes that the caching middleware can generate keys which are too long when the input is a complex URL, leading to errors like Errno::ENAMETOOLONG: File name too long @ rb_sysopen. (The effective length limit depends on what cache driver you're using.)

At the moment, we use the normalised request URI as the cache key. The fixes the issue by using Digest::SHA1.hexdigest to hash the URLs, giving us a constant 20-byte result.

The trade-off of using a hash is that:

  • there is the potential for collisions (as rare as they will realistically be!)
  • performing a hash takes time

Copy link
Member

@iMacTia iMacTia left a comment

Choose a reason for hiding this comment

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

@timrogers thanks for quickly turning this in!

Appreciate you listing the trade-offs of this approach, I believe we should be good in both cases but happy to reconsider at a future date if any issue arises.

There's just a small nit that makes Travis complain, I've left a comment below.

@@ -66,7 +67,8 @@ def cache_key(env)
url.query = params.any? ? build_query(params) : nil
end
url.normalize!
url.request_uri

Copy link
Member

Choose a reason for hiding this comment

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

There appears to be a trailing whitespace here according to Travis:

lib/faraday_middleware/response/caching.rb:70 Line contains trailing whitespace

@iMacTia iMacTia added this to the v0.13.0 milestone Sep 20, 2018
@iMacTia
Copy link
Member

iMacTia commented Nov 26, 2018

@timrogers just touching base to know if you might have some time to close this off? It's basically finished except for that small typo 👍

@timrogers
Copy link
Contributor Author

timrogers commented Nov 26, 2018 via email

Issue lostisland#132 notes that the caching middleware can generate keys
which are too long when the input is a complex URL, leading to
errors like
`Errno::ENAMETOOLONG: File name too long @ rb_sysopen`. (The
effective length limit depends on what cache driver you're using.)

At the moment, we use the normalised request URI as the cache key.
The fixes the issue by using `Digest::SHA1.hexdigest` to hash
the URLs, giving us a constant 20-byte result.

The trade-off of using a hash is that:

* there is the potential for collisions (as rare as they will
realistically be!)
* performing a hash takes time
@iMacTia
Copy link
Member

iMacTia commented Nov 26, 2018

Thanks @timrogers!
LGTM 😃

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