Skip to content

Add non-blocking (try_*) cache methods#112

Open
fsdvh wants to merge 19 commits intoarthurprs:masterfrom
fsdvh:non-blocking-methods
Open

Add non-blocking (try_*) cache methods#112
fsdvh wants to merge 19 commits intoarthurprs:masterfrom
fsdvh:non-blocking-methods

Conversation

@fsdvh
Copy link
Copy Markdown
Contributor

@fsdvh fsdvh commented Apr 12, 2026

Summary

  • Adds try_read / try_write to the internal RwLock wrapper, normalizing
    WouldBlock to None and panicking on poison (matching the blocking variants' behavior).
  • Introduces a LockContention error type returned by all non-blocking operations
    when the target shard lock cannot be acquired immediately.
  • Adds non-blocking counterparts for all major cache operations on Cache:
    • try_contains_key — returns Result<bool, LockContention>
    • try_get — returns Result<Option<Val>, LockContention>
    • try_peek — returns Result<Option<Val>, LockContention>
    • try_remove — returns Result<Option<(Key, Val)>, LockContention>
    • try_insert / try_insert_with_lifecycle — returns Result<(), (Key, Val)> /
      Result<L::RequestState, (Key, Val)> (key+value returned on contention so the
      caller can retry or discard)
  • All read-path methods return Err(LockContention) on contention; write-path insert
    methods return Err((key, val)) so the caller can retry or discard without losing data.

Test plan

  • Unit tests added for each try_* method covering: hit, miss, and contended-lock scenarios.
  • test_try_remove_contended verifies the entry is still present after a failed non-blocking remove.
  • test_try_insert_contended verifies the key+value is returned to the caller on failure.
  • Run cargo test — all tests pass.

@fsdvh fsdvh marked this pull request as ready for review April 12, 2026 10:21
@fsdvh
Copy link
Copy Markdown
Contributor Author

fsdvh commented Apr 12, 2026

Let me know what you think, @arthurprs. Thank you in advance!

@fsdvh fsdvh changed the title Add non-blocking methods for sync cache Add non-blocking cache methods (non-blocking feature) Apr 12, 2026
@arthurprs arthurprs requested a review from Copilot April 12, 2026 17:00
@arthurprs
Copy link
Copy Markdown
Owner

Thanks for the PR. In general I like the additions 👍 Do you think there's a practical benefit putting them behind the feature flag?

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds an opt-in non-blocking Cargo feature that exposes non-blocking cache operations, allowing callers to skip work when a shard lock is contended instead of blocking. This extends the existing synchronous cache API with contention-aware variants while keeping the default (blocking) behavior unchanged.

Changes:

  • Introduces a non-blocking feature flag in Cargo.toml.
  • Adds try_read / try_write to the internal RwLock wrapper (feature-gated).
  • Adds ContendedResult plus Cache::try_* methods that return Contended on lock contention rather than blocking.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
Cargo.toml Adds the non-blocking feature flag.
src/rw_lock.rs Implements non-blocking try_read/try_write on the lock wrapper under the new feature.
src/sync.rs Introduces ContendedResult and new Cache::try_* non-blocking operations gated by the feature.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/rw_lock.rs
Comment thread src/rw_lock.rs Outdated
Comment thread src/sync.rs
Comment thread Cargo.toml
@fsdvh
Copy link
Copy Markdown
Contributor Author

fsdvh commented Apr 12, 2026

Thanks for the PR. In general I like the additions 👍 Do you think there's a practical benefit putting them behind the feature flag?

Putting it under a feature flag for convenience, if you think it belongs in the default feature set, I have no objections. 😄

@fsdvh fsdvh changed the title Add non-blocking cache methods (non-blocking feature) Add non-blocking cache methods Apr 13, 2026
@fsdvh fsdvh requested a review from Copilot April 18, 2026 11:48
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sync.rs Outdated
Comment thread src/sync.rs Outdated
Comment thread src/sync.rs Outdated
Comment thread src/rw_lock.rs
fsdvh and others added 2 commits April 18, 2026 13:57
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sync.rs Outdated
Comment thread src/rw_lock.rs
Comment thread src/sync.rs
@fsdvh fsdvh changed the title Add non-blocking cache methods Add non-blocking (try_*) cache methods Apr 20, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sync.rs Outdated
Comment thread src/sync.rs Outdated
Comment thread README.md Outdated
Comment thread src/sync.rs Outdated
fsdvh and others added 3 commits April 20, 2026 15:16
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/sync.rs Outdated
Comment thread src/rw_lock.rs
Comment on lines +108 to +124
/// Attempts to acquire this `RwLock` with shared read access without blocking.
///
/// Returns `Some(guard)` if the lock was acquired, or `None` if it is already
/// held by a writer.
#[inline]
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> {
#[cfg(feature = "parking_lot")]
{
self.0.try_read().map(RwLockReadGuard)
}
#[cfg(not(feature = "parking_lot"))]
{
match self.0.try_read() {
Ok(guard) => Some(RwLockReadGuard(guard)),
Err(std::sync::TryLockError::WouldBlock) => None,
Err(std::sync::TryLockError::Poisoned(err)) => panic!("{}", err),
}
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

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

The non-blocking lock APIs are documented/implemented here as panicking on poison, but when the crate is built with the shuttle feature it uses the RwLock wrapper in src/shim.rs, whose try_read/try_write currently convert all errors (including poisoning) into None. That means Cache::try_* can silently treat a poisoned lock as mere contention under shuttle. Align the shuttle implementation with these semantics (panic on poison, None only for WouldBlock) so behavior is consistent across feature sets.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@arthurprs I think this is a valid suggestion, but it's outside the scope of this PR, wdyt?

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@fsdvh
Copy link
Copy Markdown
Contributor Author

fsdvh commented Apr 22, 2026

@arthurprs, if this is merged, can you also cut a release?

@arthurprs
Copy link
Copy Markdown
Owner

Sorry, I've been unable to get to this. I'll cut a release after merged 👍 that may be during this weekend.

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.

3 participants