pub struct AtomicClaimManager { /* private fields */ }Expand description
Atomic claim manager for queue task ownership
This provides the CAS-based claim protocol that ensures linearizable task ownership under concurrent access.
§Thread Safety
All operations are thread-safe. The manager uses fine-grained locking to minimize contention:
- Per-queue locks for claim operations
- Read-write locks for statistics
§Durability
In production, claims should be persisted to storage with WAL durability. This in-memory implementation is for reference and testing.
Implementations§
Source§impl AtomicClaimManager
impl AtomicClaimManager
Sourcepub fn claim(
&self,
queue_id: &str,
task_id: &str,
owner: &str,
lease_duration_ms: u64,
) -> ClaimResult
pub fn claim( &self, queue_id: &str, task_id: &str, owner: &str, lease_duration_ms: u64, ) -> ClaimResult
Attempt to claim a task
This is the atomic CAS operation that establishes ownership.
§Semantics
- If task is unclaimed: creates claim, returns Success
- If task is claimed by other worker with valid lease: returns AlreadyClaimed
- If task is claimed but lease expired: creates new claim, returns TookOver
§Complexity
O(1) hash lookups + lock acquisition
Sourcepub fn release(&self, token: &ClaimToken) -> Result<(), String>
pub fn release(&self, token: &ClaimToken) -> Result<(), String>
Release a claim (acknowledge successful processing)
The claim token must be valid and owned by the caller.
Sourcepub fn extend(
&self,
queue_id: &str,
token: &ClaimToken,
additional_ms: u64,
) -> Result<ClaimToken, String>
pub fn extend( &self, queue_id: &str, token: &ClaimToken, additional_ms: u64, ) -> Result<ClaimToken, String>
Extend a claim’s lease duration
Useful when processing takes longer than expected.
Sourcepub fn is_claimed(&self, queue_id: &str, task_id: &str) -> Option<(String, u64)>
pub fn is_claimed(&self, queue_id: &str, task_id: &str) -> Option<(String, u64)>
Check if a task is currently claimed
Sourcepub fn get_token(
&self,
queue_id: &str,
task_id: &str,
owner: &str,
) -> Option<ClaimToken>
pub fn get_token( &self, queue_id: &str, task_id: &str, owner: &str, ) -> Option<ClaimToken>
Get the current claim token for a task (if owned by the given worker)
Sourcepub fn cleanup_expired(&self) -> usize
pub fn cleanup_expired(&self) -> usize
Clean up expired claims
This should be called periodically (e.g., every few seconds). Returns the number of claims cleaned up.
Sourcepub fn stats(&self) -> ClaimStats
pub fn stats(&self) -> ClaimStats
Get statistics
Sourcepub fn active_claims(&self, queue_id: &str) -> usize
pub fn active_claims(&self, queue_id: &str) -> usize
Get number of active claims for a queue
Sourcepub fn list_claims(&self, queue_id: &str) -> Vec<ClaimToken>
pub fn list_claims(&self, queue_id: &str) -> Vec<ClaimToken>
Get all active claims for a queue (for monitoring)
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for AtomicClaimManager
impl !RefUnwindSafe for AtomicClaimManager
impl Send for AtomicClaimManager
impl Sync for AtomicClaimManager
impl Unpin for AtomicClaimManager
impl UnsafeUnpin for AtomicClaimManager
impl !UnwindSafe for AtomicClaimManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more