Skip to main content

AtomicClaimManager

Struct AtomicClaimManager 

Source
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

Source

pub fn new() -> Self

Create a new claim manager

Source

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

Source

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.

Source

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.

Source

pub fn is_claimed(&self, queue_id: &str, task_id: &str) -> Option<(String, u64)>

Check if a task is currently claimed

Source

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)

Source

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.

Source

pub fn stats(&self) -> ClaimStats

Get statistics

Source

pub fn active_claims(&self, queue_id: &str) -> usize

Get number of active claims for a queue

Source

pub fn list_claims(&self, queue_id: &str) -> Vec<ClaimToken>

Get all active claims for a queue (for monitoring)

Trait Implementations§

Source§

impl Default for AtomicClaimManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.