queue-runtime 0.2.0

Multi-provider queue runtime for Queue-Keeper
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# Session Management Module


The session management module provides a **generic, domain-agnostic** framework for session key generation that enables ordered message processing for any application domain.

## Overview


**Critical Design Principle**: This module is intentionally **NOT GitHub-specific**. It provides infrastructure for session-based ordering without assuming any specific message structure or business domain (GitHub events, e-commerce orders, IoT telemetry, financial transactions, etc.).

The module provides:

1. **SessionKeyExtractor**: Trait for messages to expose metadata
2. **SessionKeyGenerator**: Strategy trait for generating session keys
3. **Pre-built Strategies**: Common patterns for session key generation
4. **Composable Design**: Strategies can be combined and chained

## Core Concepts


### Session Keys


Session keys are strings that group related messages for FIFO ordered processing. Messages with the same session key are guaranteed to be delivered in the order they were sent.

**Session Key Purpose**:

- Group related messages (e.g., all orders for a customer)
- Ensure FIFO delivery within each group
- Allow concurrent processing of different groups
- Provide ordering semantics application can rely on

**Example Session Keys**:

- `order-12345` - All events for order 12345
- `user-alice-cart` - All cart events for user Alice
- `tenant-123-resource-456` - All resource events for tenant 123's resource 456
- `github/owner/repo/pr/42` - All events for PR #42 in owner/repo

### No Ordering


Returning `None` from a session key generator allows **concurrent processing** without ordering constraints. Use for stateless operations that don't require message ordering.

## Core Traits


### SessionKeyExtractor


Trait for extracting metadata from messages. Messages implement this trait to expose data that session key generators can use.

**Trait Definition**:

```rust
pub trait SessionKeyExtractor {
    /// Get a metadata value by key
    ///
    /// Returns `None` if the key doesn't exist or has no value
    fn get_metadata(&self, key: &str) -> Option<String>;

    /// List all available metadata keys for this message
    ///
    /// Useful for debugging and introspection
    fn list_metadata_keys(&self) -> Vec<String> {
        Vec::new()
    }

    /// Get all metadata as a map (optional, for bulk operations)
    fn get_all_metadata(&self) -> HashMap<String, String> {
        /* default implementation provided */
    }
}
```

**Design Philosophy**:

- **Key-Value Interface**: Messages expose named fields without assuming structure
- **No Assumptions**: Works with any message type or domain
- **Pull-Based**: Generators query for data they need

**Example Implementation** (E-commerce):

```rust
use queue_runtime::sessions::SessionKeyExtractor;

struct OrderEvent {
    order_id: String,
    customer_id: String,
    warehouse_id: String,
    timestamp: DateTime<Utc>,
}

impl SessionKeyExtractor for OrderEvent {
    fn get_metadata(&self, key: &str) -> Option<String> {
        match key {
            "order_id" => Some(self.order_id.clone()),
            "customer_id" => Some(self.customer_id.clone()),
            "warehouse_id" => Some(self.warehouse_id.clone()),
            "timestamp" => Some(self.timestamp.to_rfc3339()),
            _ => None,
        }
    }

    fn list_metadata_keys(&self) -> Vec<String> {
        vec![
            "order_id".to_string(),
            "customer_id".to_string(),
            "warehouse_id".to_string(),
            "timestamp".to_string(),
        ]
    }
}
```

**Example Implementation** (IoT):

```rust
struct TelemetryEvent {
    device_id: String,
    sensor_type: String,
    reading: f64,
}

impl SessionKeyExtractor for TelemetryEvent {
    fn get_metadata(&self, key: &str) -> Option<String> {
        match key {
            "device_id" => Some(self.device_id.clone()),
            "sensor_type" => Some(self.sensor_type.clone()),
            "reading" => Some(self.reading.to_string()),
            _ => None,
        }
    }
}
```

### SessionKeyGenerator


Strategy trait for generating session keys from message metadata. Different implementations provide different ordering semantics.

**Trait Definition**:

```rust
pub trait SessionKeyGenerator: Send + Sync {
    /// Generate a session key for the given message
    ///
    /// Returns `None` if the message should not be session-ordered,
    /// allowing concurrent processing without ordering constraints
    fn generate_key(&self, extractor: &dyn SessionKeyExtractor) -> Option<SessionId>;
}
```

**Design Principles**:

- **Strategy Pattern**: Different strategies provide different grouping semantics
- **Optional Ordering**: Returning `None` allows concurrent processing
- **Composable**: Strategies can be combined (see FallbackStrategy)
- **Thread-Safe**: `Send + Sync` for use in async contexts

**Example Implementation**:

```rust
use queue_runtime::sessions::{SessionKeyGenerator, SessionKeyExtractor};
use queue_runtime::message::SessionId;

struct OrderSessionStrategy;

impl SessionKeyGenerator for OrderSessionStrategy {
    fn generate_key(&self, extractor: &dyn SessionKeyExtractor) -> Option<SessionId> {
        extractor.get_metadata("order_id")
            .and_then(|id| SessionId::new(format!("order-{}", id)).ok())
    }
}
```

## Pre-Built Strategies


### SingleFieldStrategy


Generates session keys from a single metadata field with optional prefix.

**Use Cases**:

- Order by entity ID
- Group by user ID
- Partition by tenant ID

**Construction**:

```rust
/// Create single field strategy
pub fn new(field_name: &str, prefix: Option<&str>) -> Self;
```

**Example**:

```rust
use queue_runtime::sessions::SingleFieldStrategy;

// Session keys like "order-12345"
let strategy = SingleFieldStrategy::new("order_id", Some("order"));

// Session keys like "12345" (no prefix)
let strategy = SingleFieldStrategy::new("order_id", None);
```

**Behavior**:

- Extracts single field from message
- Adds optional prefix
- Returns `None` if field is missing
- Validates session ID format

**Usage Scenarios**:

```rust
// E-commerce: Order by customer
let customer_strategy = SingleFieldStrategy::new("customer_id", Some("customer"));

// IoT: Order by device
let device_strategy = SingleFieldStrategy::new("device_id", Some("device"));

// Multi-tenant: Order by tenant
let tenant_strategy = SingleFieldStrategy::new("tenant_id", Some("tenant"));
```

### CompositeKeyStrategy


Generates session keys by composing multiple metadata fields with a separator.

**Use Cases**:

- Hierarchical grouping (tenant + resource)
- Multi-dimensional ordering (region + customer)
- Compound keys (owner + repo + entity)

**Construction**:

```rust
/// Create composite key strategy
pub fn new(fields: Vec<String>, separator: &str) -> Self;
```

**Example**:

```rust
use queue_runtime::sessions::CompositeKeyStrategy;

// Session keys like "tenant-123-resource-456"
let strategy = CompositeKeyStrategy::new(
    vec!["tenant_id".to_string(), "resource_id".to_string()],
    "-"
);

// Session keys like "us-west-2/customer-789"
let strategy = CompositeKeyStrategy::new(
    vec!["region".to_string(), "customer_id".to_string()],
    "/"
);
```

**Behavior**:

- Extracts all specified fields in order
- Returns `None` if ANY field is missing
- Joins field values with separator
- Validates final session ID format

**Usage Scenarios**:

```rust
// Multi-tenant SaaS: tenant + user
let strategy = CompositeKeyStrategy::new(
    vec!["tenant_id".to_string(), "user_id".to_string()],
    "-"
);

// E-commerce: warehouse + order
let strategy = CompositeKeyStrategy::new(
    vec!["warehouse_id".to_string(), "order_id".to_string()],
    "-"
);

// GitHub events: owner + repo + entity_type + entity_id
let strategy = CompositeKeyStrategy::new(
    vec!["owner".to_string(), "repo".to_string(), "entity_type".to_string(), "entity_id".to_string()],
    "/"
);
```

### NoOrderingStrategy


Disables session-based ordering, allowing all messages to be processed concurrently.

**Use Cases**:

- Stateless operations
- Notification delivery
- Independent message processing
- High-throughput scenarios

**Example**:

```rust
use queue_runtime::sessions::NoOrderingStrategy;

let strategy = NoOrderingStrategy;

// Always returns None - no ordering
let session_id = strategy.generate_key(&message); // None
```

**Behavior**:

- Always returns `None`
- All messages can be processed concurrently
- No ordering guarantees
- Maximum throughput

### FallbackStrategy


Tries multiple generators in order, using the first success. Provides fine-grained ordering with coarser fallbacks.

**Use Cases**:

- Entity-level ordering with repository fallback
- Specific-to-general ordering hierarchies
- Graceful degradation of ordering

**Construction**:

```rust
/// Create fallback strategy with ordered generators
pub fn new(generators: Vec<Box<dyn SessionKeyGenerator>>) -> Self;
```

**Example**:

```rust
use queue_runtime::sessions::{FallbackStrategy, SingleFieldStrategy, CompositeKeyStrategy};

// Try order-specific key, fall back to customer-level key
let primary = SingleFieldStrategy::new("order_id", Some("order"));
let fallback = SingleFieldStrategy::new("customer_id", Some("customer"));

let strategy = FallbackStrategy::new(vec![
    Box::new(primary),
    Box::new(fallback),
]);
```

**Behavior**:

- Tries each generator in order
- Returns first non-None result
- Returns None if all generators return None
- Allows fine-grained ordering with coarse fallback

**Usage Scenarios**:

```rust
// E-commerce: Order by line item, fall back to order, fall back to customer
let strategy = FallbackStrategy::new(vec![
    Box::new(SingleFieldStrategy::new("line_item_id", Some("item"))),
    Box::new(SingleFieldStrategy::new("order_id", Some("order"))),
    Box::new(SingleFieldStrategy::new("customer_id", Some("customer"))),
]);

// GitHub: PR-specific, fall back to repo-level
let strategy = FallbackStrategy::new(vec![
    Box::new(CompositeKeyStrategy::new(
        vec!["owner".to_string(), "repo".to_string(), "pr_number".to_string()],
        "/"
    )),
    Box::new(CompositeKeyStrategy::new(
        vec!["owner".to_string(), "repo".to_string()],
        "/"
    )),
]);
```

## Usage Patterns


### Basic Usage


```rust
use queue_runtime::sessions::{SessionKeyGenerator, SessionKeyExtractor, SingleFieldStrategy};
use queue_runtime::message::{Message, SessionId};
use bytes::Bytes;

// Your domain message type
struct OrderEvent {
    order_id: String,
    data: Vec<u8>,
}

// Implement metadata extraction
impl SessionKeyExtractor for OrderEvent {
    fn get_metadata(&self, key: &str) -> Option<String> {
        match key {
            "order_id" => Some(self.order_id.clone()),
            _ => None,
        }
    }
}

// Create strategy
let strategy = SingleFieldStrategy::new("order_id", Some("order"));

// Generate session key
let event = OrderEvent {
    order_id: "12345".to_string(),
    data: vec![1, 2, 3],
};

if let Some(session_id) = strategy.generate_key(&event) {
    // Create message with session ID for ordered processing
    let message = Message::new(Bytes::from(event.data))
        .with_session_id(session_id);

    // Send message
    client.send_message(&queue, message).await?;
}
```

### Multi-Tenant Application


```rust
use queue_runtime::sessions::CompositeKeyStrategy;

// Session keys like "tenant-123-resource-456"
let strategy = CompositeKeyStrategy::new(
    vec!["tenant_id".to_string(), "resource_id".to_string()],
    "-"
);

// Messages for same tenant+resource ordered
// Different tenant/resource combinations processed concurrently
```

### Optional Ordering


```rust
use queue_runtime::sessions::{FallbackStrategy, SingleFieldStrategy, NoOrderingStrategy};

// Try entity-specific ordering, fall back to no ordering
let strategy = FallbackStrategy::new(vec![
    Box::new(SingleFieldStrategy::new("entity_id", Some("entity"))),
    Box::new(NoOrderingStrategy),
]);

// If entity_id exists: ordered by entity
// If entity_id missing: concurrent processing
```

### Time-Partitioned Sessions


Applications can implement time-based partitioning to prevent hot sessions:

```rust
use queue_runtime::sessions::{SessionKeyGenerator, SessionKeyExtractor};
use queue_runtime::message::SessionId;
use chrono::{DateTime, Utc, Timelike};

struct TimePartitionedStrategy {
    base_strategy: Box<dyn SessionKeyGenerator>,
    partition_hours: u32,
}

impl SessionKeyGenerator for TimePartitionedStrategy {
    fn generate_key(&self, extractor: &dyn SessionKeyExtractor) -> Option<SessionId> {
        // Get base session key
        let base_key = self.base_strategy.generate_key(extractor)?;

        // Add time partition
        let now = Utc::now();
        let partition = now.hour() / self.partition_hours;

        // Create partitioned key
        SessionId::new(format!("{}-p{}", base_key.as_str(), partition)).ok()
    }
}

// Usage: sessions rotate every 4 hours
let strategy = TimePartitionedStrategy {
    base_strategy: Box::new(SingleFieldStrategy::new("order_id", Some("order"))),
    partition_hours: 4,
};

// Produces: "order-12345-p0", "order-12345-p1", etc.
// Prevents single session from growing too large
```

## SessionLifecycleManager


Manager for session metadata and state tracking. **Note**: This is currently just a placeholder type in the implementation.

**Purpose** (Planned):

- Track active sessions
- Monitor session health (message rate, age)
- Detect stalled sessions
- Implement session timeout policies
- Provide session metrics

**Current Status**: Implementation pending - framework in place for future development.

## Design Principles


### Domain-Agnostic


This module makes **zero assumptions** about message content or business domain:

- No GitHub-specific types or logic
- No assumed message structure
- Works with any domain (e-commerce, IoT, finance, etc.)
- Applications implement `SessionKeyExtractor` for their domain

### Flexible Strategy Pattern


Different applications need different ordering semantics:

- **Single-field**: Simple entity-based ordering
- **Composite**: Hierarchical or multi-dimensional ordering
- **Fallback**: Fine-grained with coarse fallback
- **No ordering**: Concurrent processing
- **Custom**: Applications implement `SessionKeyGenerator` trait

### Composable


Strategies can be combined:

- Fallback chains provide multiple ordering levels
- Custom strategies wrap pre-built strategies
- Time-partitioning can wrap any strategy
- Applications build complex ordering logic from simple primitives

## Behavioral Assertions


### SessionKeyExtractor Assertions


1. **get_metadata returns None for unknown keys**: Messages don't error on missing fields
2. **get_metadata returns consistent values**: Same key returns same value (idempotent)
3. **list_metadata_keys lists all available keys**: Complete metadata enumeration

### SessionKeyGenerator Assertions


1. **Same message produces same session key**: Deterministic key generation (idempotent)
2. **Valid session IDs only**: Generated keys must pass SessionId validation
3. **None means concurrent processing**: Returning None allows parallel execution
4. **Generators are thread-safe**: Can be shared across async tasks (Send + Sync)

### Strategy-Specific Assertions


1. **SingleFieldStrategy returns None if field missing**: Graceful handling of missing data
2. **CompositeKeyStrategy requires all fields**: Returns None if any field missing
3. **FallbackStrategy tries in order**: First success returned, not all generators tried
4. **NoOrderingStrategy always returns None**: Guarantees concurrent processing

## Testing Strategy


### Unit Testing


- Test each strategy with mock messages
- Test edge cases (missing fields, invalid characters)
- Test SessionId validation in generated keys
- Test strategy composition (Fallback)

### Property-Based Testing


- Generate random metadata combinations
- Verify deterministic key generation
- Test SessionId validation with generated keys
- Verify thread safety with concurrent generation

### Domain Integration Testing


- Test strategies with real domain messages
- Verify ordering semantics in queue operations
- Test fallback behavior under various conditions
- Measure performance impact of session generation

## Usage Recommendations


### When to Use Sessions


**Use sessions when**:

- Messages for same entity must be processed in order
- State transitions require ordering (FSMs)
- Exactly-once semantics required at entity level
- Distributed coordination needed

**Don't use sessions when**:

- Messages are independent (stateless operations)
- High throughput is critical
- Order doesn't matter (notifications, alerts)
- Messages can be processed in parallel

### Choosing a Strategy


**SingleFieldStrategy**:

- Simple entity-based ordering
- One primary grouping dimension
- Clear entity identification

**CompositeKeyStrategy**:

- Hierarchical data (tenant → user → resource)
- Multi-dimensional ordering
- Compound business keys

**FallbackStrategy**:

- Fine-grained ordering when possible
- Graceful degradation to coarser ordering
- Mixed message types with different metadata

**NoOrderingStrategy**:

- Stateless operations
- Maximum throughput needed
- Order explicitly not required

**Custom Strategy**:

- Domain-specific ordering rules
- Complex business logic
- Time-based partitioning
- Dynamic ordering decisions

## Future Enhancements


Potential additions to this module:

1. **SessionLifecycleManager Implementation**: Active session tracking and health monitoring
2. **Dynamic Strategies**: Strategy selection based on message content
3. **Session Metrics**: Built-in instrumentation for session performance
4. **Time-Based Partitioning**: Pre-built time-partitioning wrapper
5. **Session Affinity**: Hint-based session routing to same worker
6. **Session State Storage**: External state store integration for stateful sessions