Http caching & Conditional requests#831
Merged
Merged
Conversation
Collaborator
|
could you please rebase on master @pnevyk |
Contributor
Author
Owner
|
Thank you for your PR! |
Merged
Contributor
Author
Do you think that the issues I mentioned in the PR description, in particular synchronous/blocking API of |
Owner
|
I think they are fine enough to start with and if someone wants an async implementation, that can be made as a follow up. I think in memory makes sense for what we can offer as we don't want to bless certain crates other people might want such as redis. A filesystem implementation would also be acceptable behind a feature flag but again that can be done in a follow up. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
This PR implements HTTP cache layer for the Octocrab client which uses conditional requests that do not count against the primary rate limit if a
304 Not Modifiedresponse is returned by GitHub API.Example usage:
Implements #175.
Design
First and foremost, I am looking for feedback on the design. The implementation originates from my code which I hastily put together because I needed this feature for my use case and in-memory cache for good enough for me.
In #175 (comment) it is specified that this feature needs to be done in an IO-agnostic way and use a cache storage trait.
Main types are
CacheStorage::try_hit– When a new request is made, the layer asks if there is an existing cache record (identified by either ETag or Last-Modified). If true, the corresponding cache header is added to the request.CacheStorage::load– When a new response is received and is304 Not Modified, then the cached response is loaded from the cache (assuming hit) and the response body is replaced with the cached body.CacheStorage::writer– Returns a responseBodywrapper that is supposed to write each received frame to the cache.The
CacheWriter::write_bodyis currently synchronous. I assume that this is not desired, but I don't have an experience with lower-level async code and I didn't know how to cleanly implement this:I am very much open to advice.
I also considered using the
std::io::Write(orAsyncWrite) instead of customCacheWriter, but I don't think that types already implementing this standard trait could be used because the writer likely needs to manage some cache-related state.CacheStorage::loadis also synchronous currently, but that one would be probably easy to make asynchronous to allow async loading from filesystem.There is an ETag implementation in this library already. It's not used in my implementation, but maybe it should. My thought process was to always use the ETag value returned by response header as is in the request header.
Notes
I implemented only in-memory cache because it was the most straightforward. The original issue asks for filesystem storage. I am not sure if such implementation should be part of the library or should be implemented by the user. If it should be implemented in the library, it's not obvious to me what is the best implementation.
Only after my implementation I discovered the
http-cachecrate which might be used for the caching implementation but I am not sure how, because I am not really familiar with the ecosystem.