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
impl SessionAffinityTracker
Sourcepub async fn assign_session(
&self,
session_id: SessionId,
consumer_id: String,
) -> Result<SessionAffinity, QueueError>
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 assignconsumer_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.
Sourcepub async fn get_consumer(&self, session_id: &SessionId) -> Option<String>
pub async fn get_consumer(&self, session_id: &SessionId) -> Option<String>
Sourcepub async fn get_affinity(
&self,
session_id: &SessionId,
) -> Option<SessionAffinity>
pub async fn get_affinity( &self, session_id: &SessionId, ) -> Option<SessionAffinity>
Sourcepub async fn has_affinity(&self, session_id: &SessionId) -> bool
pub async fn has_affinity(&self, session_id: &SessionId) -> bool
Sourcepub async fn touch_session(
&self,
session_id: &SessionId,
) -> Result<(), QueueError>
pub async fn touch_session( &self, session_id: &SessionId, ) -> Result<(), QueueError>
Sourcepub async fn release_session(
&self,
session_id: &SessionId,
consumer_id: &str,
) -> Result<(), QueueError>
pub async fn release_session( &self, session_id: &SessionId, consumer_id: &str, ) -> Result<(), QueueError>
Sourcepub async fn extend_affinity(
&self,
session_id: &SessionId,
consumer_id: &str,
additional_duration: Duration,
) -> Result<SessionAffinity, QueueError>
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 extendconsumer_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
Sourcepub async fn get_consumer_sessions(&self, consumer_id: &str) -> Vec<SessionId>
pub async fn get_consumer_sessions(&self, consumer_id: &str) -> Vec<SessionId>
Sourcepub async fn cleanup_expired(&self) -> usize
pub async fn cleanup_expired(&self) -> usize
Sourcepub async fn affinity_count(&self) -> usize
pub async fn affinity_count(&self) -> usize
Get the total number of affinity mappings (including expired).
Sourcepub async fn active_affinity_count(&self) -> usize
pub async fn active_affinity_count(&self) -> usize
Get the number of active (non-expired) affinities.
Trait Implementations§
Source§impl Clone for SessionAffinityTracker
impl Clone for SessionAffinityTracker
Source§fn clone(&self) -> SessionAffinityTracker
fn clone(&self) -> SessionAffinityTracker
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more