-
Notifications
You must be signed in to change notification settings - Fork 579
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
fix(lru): correctly handle future cancellation #3911
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
878489f
fix(lru): correctly handle future cancellation
skyzh 8e4cc87
fix test blocking
skyzh 7757fda
fix failpoints test
skyzh 8dc9662
Merge branch 'main' into skyzh/fix-lru-cancel
skyzh fc36989
fix clippy
skyzh 85ce7b6
Merge branch 'main' into skyzh/fix-lru-cancel
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -757,7 +757,9 @@ impl<K: LruKey, T: LruValue> LruCache<K, T> { | |
} | ||
} | ||
|
||
impl<K: LruKey + Clone, T: LruValue> LruCache<K, T> { | ||
/// Only implement `lookup_with_request_dedup` for static values, as they can be sent across tokio | ||
/// spawned futures. | ||
impl<K: LruKey + Clone + 'static, T: LruValue + 'static> LruCache<K, T> { | ||
pub async fn lookup_with_request_dedup<F, E, VC>( | ||
self: &Arc<Self>, | ||
hash: u64, | ||
|
@@ -766,25 +768,33 @@ impl<K: LruKey + Clone, T: LruValue> LruCache<K, T> { | |
) -> Result<Result<CachableEntry<K, T>, E>, RecvError> | ||
where | ||
F: FnOnce() -> VC, | ||
E: Error, | ||
VC: Future<Output = Result<(T, usize), E>>, | ||
E: Error + Send + 'static, | ||
VC: Future<Output = Result<(T, usize), E>> + Send + 'static, | ||
{ | ||
match self.lookup_for_request(hash, key.clone()) { | ||
LookupResult::Cached(entry) => Ok(Ok(entry)), | ||
LookupResult::WaitPendingRequest(recv) => { | ||
let entry = recv.await?; | ||
Ok(Ok(entry)) | ||
} | ||
LookupResult::Miss => match fetch_value().await { | ||
Ok((value, charge)) => { | ||
let entry = self.insert(key, hash, charge, value); | ||
Ok(Ok(entry)) | ||
} | ||
Err(e) => { | ||
self.clear_pending_request(&key, hash); | ||
Ok(Err(e)) | ||
} | ||
}, | ||
LookupResult::Miss => { | ||
let this = self.clone(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we rewrite it as let ret = tokio::spawn(fetch_value).await; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. fetch_value itself is not 'static. The future it returns is 'static. |
||
let fetch_value = fetch_value(); | ||
tokio::spawn(async move { | ||
match fetch_value.await { | ||
Ok((value, charge)) => { | ||
let entry = this.insert(key, hash, charge, value); | ||
Ok(Ok(entry)) | ||
} | ||
Err(e) => { | ||
this.clear_pending_request(&key, hash); | ||
Ok(Err(e)) | ||
} | ||
} | ||
}) | ||
.await | ||
.unwrap() | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems async closure is unnecessary? We only need a future here. cc @Little-Wallace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's necessary. There's some variables that only need to be computed when cache miss. See other parts of the PR for more information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.g. we need to call
self.meta_path
to compute the meta path ONLY when cache miss. But&self
is not'static
, so we can only compute it in this FnOnce closure and outside theFuture + 'static
.