pub trait SessionKeyGenerator: Send + Sync {
// Required method
fn generate_key(
&self,
extractor: &dyn SessionKeyExtractor,
) -> Option<SessionId>;
}Expand description
Strategy trait for generating session keys from messages.
Implementations define how messages are grouped for ordered processing. The generator extracts relevant metadata from messages and produces session keys that group related messages together.
§Design Principles
- Domain-Agnostic: Works with any message structure via
SessionKeyExtractor - Strategy Pattern: Different strategies provide different ordering semantics
- Composable: Strategies can be combined or chained
- Optional Ordering: Returning
Noneallows concurrent processing
§Common Patterns
- Entity-based: Group by entity ID (order-123, user-456)
- Hierarchical: Group by parent/child relationships
- Temporal: Group by time windows
- Custom: Domain-specific grouping logic
§Example
use queue_runtime::sessions::{SessionKeyGenerator, SessionKeyExtractor};
use queue_runtime::message::SessionId;
struct ResourceIdStrategy;
impl SessionKeyGenerator for ResourceIdStrategy {
fn generate_key(&self, extractor: &dyn SessionKeyExtractor) -> Option<SessionId> {
extractor.get_metadata("resource_id")
.and_then(|id| SessionId::new(format!("resource-{}", id)).ok())
}
}Required Methods§
Sourcefn generate_key(&self, extractor: &dyn SessionKeyExtractor) -> Option<SessionId>
fn generate_key(&self, extractor: &dyn SessionKeyExtractor) -> Option<SessionId>
Generate a session key for the given message.
Returns None if the message should not be session-ordered, allowing
it to be processed concurrently without ordering constraints.
§Arguments
extractor- Message implementing SessionKeyExtractor trait
§Returns
Optional session ID for grouping related messages