Skip to main content

SessionAffinityTracker

Struct SessionAffinityTracker 

Source
pub struct SessionAffinityTracker { /* private fields */ }
Expand description

Tracks session-to-consumer affinity mappings for ordered processing.

The affinity tracker ensures that all messages for a given session are routed to the same consumer, maintaining message ordering and processing consistency within sessions.

§Thread Safety

This type uses Arc<RwLock<>> internally and can be safely shared across threads and tasks.

§Examples

use queue_runtime::sessions::SessionAffinityTracker;
use queue_runtime::message::SessionId;
use std::time::Duration;

let tracker = SessionAffinityTracker::new(Duration::from_secs(600));
let session_id = SessionId::new("session-123".to_string()).unwrap();

// Assign session to consumer
let affinity = tracker.assign_session(session_id.clone(), "worker-1".to_string()).await.unwrap();
assert_eq!(affinity.consumer_id(), "worker-1");

// Query affinity
let consumer = tracker.get_consumer(&session_id).await;
assert_eq!(consumer, Some("worker-1".to_string()));

Implementations§

Source§

impl SessionAffinityTracker

Source

pub fn new(default_affinity_duration: Duration) -> Self

Create a new session affinity tracker.

§Arguments
  • default_affinity_duration - Default duration for affinity mappings
§Returns

A new SessionAffinityTracker instance

Source

pub async fn assign_session( &self, session_id: SessionId, consumer_id: String, ) -> Result<SessionAffinity, QueueError>

Assign a session to a consumer.

If the session is already assigned and not expired, returns an error. If the session affinity has expired, reassigns to the new consumer.

§Arguments
  • session_id - The session to assign
  • consumer_id - The consumer to assign the session to
§Returns

The created affinity mapping on success, or an error if the session is already assigned to a different consumer.

§Errors

Returns QueueError::SessionLocked if the session is already assigned to a different consumer and the affinity has not expired.

Source

pub async fn get_consumer(&self, session_id: &SessionId) -> Option<String>

Get the consumer assigned to a session.

§Arguments
  • session_id - The session to query
§Returns

The consumer ID if the session has an active affinity, None otherwise

Source

pub async fn get_affinity( &self, session_id: &SessionId, ) -> Option<SessionAffinity>

Get the full affinity information for a session.

§Arguments
  • session_id - The session to query
§Returns

The affinity information if the session has an active affinity

Source

pub async fn has_affinity(&self, session_id: &SessionId) -> bool

Check if a session has an active affinity.

§Arguments
  • session_id - The session to check
§Returns

true if the session has an active affinity

Source

pub async fn touch_session( &self, session_id: &SessionId, ) -> Result<(), QueueError>

Update the last activity time for a session.

This should be called when a message is processed for the session.

§Arguments
  • session_id - The session to update
§Returns

Ok(()) if successful, error if session not found or expired

Source

pub async fn release_session( &self, session_id: &SessionId, consumer_id: &str, ) -> Result<(), QueueError>

Release a session affinity.

§Arguments
  • session_id - The session to release
  • consumer_id - The consumer releasing the session (for validation)
§Returns

Ok(()) if successful

§Errors

Returns error if the consumer doesn’t own the session

Source

pub async fn extend_affinity( &self, session_id: &SessionId, consumer_id: &str, additional_duration: Duration, ) -> Result<SessionAffinity, QueueError>

Extend the affinity duration for a session.

§Arguments
  • session_id - The session to extend
  • consumer_id - The consumer requesting the extension (for validation)
  • additional_duration - Additional time to add
§Returns

The updated affinity on success

§Errors

Returns error if consumer doesn’t own the session or session not found

Source

pub async fn get_consumer_sessions(&self, consumer_id: &str) -> Vec<SessionId>

Get all sessions assigned to a consumer.

§Arguments
  • consumer_id - The consumer to query
§Returns

List of session IDs assigned to the consumer

Source

pub async fn cleanup_expired(&self) -> usize

Clean up expired affinities.

§Returns

Number of affinities removed

Source

pub async fn affinity_count(&self) -> usize

Get the total number of affinity mappings (including expired).

Source

pub async fn active_affinity_count(&self) -> usize

Get the number of active (non-expired) affinities.

Trait Implementations§

Source§

impl Clone for SessionAffinityTracker

Source§

fn clone(&self) -> SessionAffinityTracker

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more