fluxion-exec 0.7.0

Async stream subscribers and execution utilities for fluxion
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# fluxion-exec


> **Part of [Fluxion]../README.md** - A reactive stream processing library for Rust

Async stream execution utilities that enable processing streams with async handlers, providing fine-grained control over concurrency, cancellation, and error handling.

[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](../LICENSE)
[![Rust](https://img.shields.io/badge/rust-1.75%2B-orange.svg)](https://www.rust-lang.org/)

## Overview


`fluxion-exec` provides two powerful execution patterns for consuming async streams:

- **`subscribe`** - Sequential processing where every item is processed to completion
- **`subscribe_latest`** - Latest-value processing with automatic cancellation of outdated work

These utilities solve the common problem of how to process stream items with async functions while controlling concurrency, managing cancellation, and handling errors gracefully.

## Table of Contents


- [Features]#features
- [Installation]#installation
- [Quick Start]#quick-start
- [Core Concepts]#core-concepts
- [Execution Patterns]#execution-patterns
  - [subscribe - Sequential Processing]#subscribe---sequential-processing
  - [subscribe_latest - Latest-Value Processing]#subscribe_latest---latest-value-processing
- [Detailed Examples]#detailed-examples
- [Use Cases]#use-cases
- [Performance Characteristics]#performance-characteristics
- [Error Handling]#error-handling
- [Cancellation]#cancellation
- [Common Patterns]#common-patterns
- [Anti-Patterns]#anti-patterns
- [Comparison with Alternatives]#comparison-with-alternatives
- [Troubleshooting]#troubleshooting
- [API Reference]#api-reference
- [License]#license

## Features


✨ **Two Execution Modes**
- Sequential processing - process every item in order
- Latest-value processing - skip intermediate values, process only latest

🎯 **Flexible Error Handling**
- Custom error callbacks
- Error collection and propagation
- Continue processing on errors

🚀 **Async-First Design**
- Built on tokio runtime
- Spawns background tasks for concurrent execution
- Non-blocking stream consumption

⚡ **Cancellation Support**
- Built-in `CancellationToken` integration
- Automatic cancellation of outdated work (in `subscribe_latest`)
- Graceful shutdown support

🔧 **Extension Trait Pattern**
- Works with any `Stream` implementation
- Compose with other stream operators
- Type-safe and ergonomic API

## Installation


Add to your `Cargo.toml`:

```toml
[dependencies]
fluxion-exec = "0.5"
tokio = { version = "1.48", features = ["rt", "sync", "macros"] }
tokio-stream = "0.1"
futures = "0.3"
```

## Quick Start


The following sections contain a wide range of examples and suggestions. These are indicative and should not be expected to compile as they are.
Check the following files for genuine runnable examples that can be used as they are:
 - [subscribe]./tests/subscribe_tests.rs
 - [subscribe_latest]./tests/subscribe_latest_tests.rs

### Sequential Processing


```rust

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (tx, rx) = unbounded_channel();
    let stream = UnboundedReceiverStream::new(rx);

    // Spawn processor
    tokio::spawn(async move {
        stream
            .subscribe(
                |item, _ctx| async move {
                    println!("Processing: {}", item);
                    // Simulate async work
                    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                    Ok::<_, std::io::Error>(())
                },
                |_| {},  // No error callback
                None     // No cancellation token
            )
            .await
    });

    // Send items
    tx.send(1)?;
    tx.send(2)?;
    tx.send(3)?;

    Ok(())
}
```

### Latest-Value Processing


```rust

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (tx, rx) = unbounded_channel();
    let stream = UnboundedReceiverStream::new(rx);

    tokio::spawn(async move {
        stream
            .subscribe_latest(
                |item, token| async move {
                    // Check cancellation periodically
                    for i in 0..10 {
                        if token.is_cancelled() {
                            println!("Cancelled processing {}", item);
                            return Ok(());
                        }
                        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
                    }
                    println!("Completed: {}", item);
                    Ok::<_, std::io::Error>(())
                },
                None,
                None
            )
            .await
    });

    // Rapidly send items - intermediate ones will be skipped
    tx.send(1)?;
    tx.send(2)?;
    tx.send(3)?;
    tx.send(4)?;

    Ok(())
}
```

## Core Concepts


### Subscription


A subscription attaches an async handler to a stream and processes items until the stream ends or a cancellation token is triggered.

### Sequential Execution


With `subscribe`, items are processed one at a time. Each item's handler must complete before the next item is processed. This guarantees:
- Every item is processed
- Processing order is maintained
- No concurrent execution of handlers

### Latest-Value Processing


With `subscribe_latest`, only the most recent value is processed. When new items arrive during processing:
- Current processing continues
- Latest item is queued
- Intermediate items are discarded
- After completion, the latest queued item is processed

This is ideal for scenarios where:
- Only current state matters
- Old values become irrelevant
- Expensive operations should skip stale data

## Execution Patterns


### subscribe - Sequential Processing


**Process every item in order with async handlers.**

```rust

stream.subscribe(
    |item, cancellation_token| async move {
        // Your async processing logic
        process_item(item).await?;
        Ok::<_, MyError>(())
    },
    |error| eprintln!("Error: {:?}", error), // Error callback
    Some(cancellation_token) // Optional cancellation
).await?;
```

**When to use:**
- Every item must be processed (e.g., database writes, event logging)
- Processing order matters
- Side effects must occur for each item
- Work cannot be skipped

**Examples:**
- Writing audit logs
- Processing financial transactions
- Sending notifications
- Persisting events to database

### subscribe_latest - Latest-Value Processing


**Process only the latest value, canceling work for outdated items.**

```rust

stream.subscribe_latest(
    |item, cancellation_token| async move {
        // Check cancellation periodically in long-running tasks
        for chunk in work_chunks {
            if cancellation_token.is_cancelled() {
                return Ok(()); // Gracefully exit
            }
            process_chunk(chunk).await?;
        }
        Ok::<_, MyError>(())
    },
    Some(|error| eprintln!("Error: {:?}", error)),
    Some(cancellation_token)
).await?;
```

**When to use:**
- Only latest value matters (e.g., UI rendering, auto-save)
- Old values become irrelevant
- Expensive operations should skip intermediate values
- Real-time updates

**Examples:**
- Rendering UI with latest state
- Search-as-you-type queries
- Live preview updates
- Auto-saving current document

## Detailed Examples


### Example 1: Database Event Processing


Process every event sequentially and persist to database:

```rust

#[derive(Debug, Serialize, Deserialize)]

struct Event {
    id: u64,
    data: String,
}

async fn save_to_db(event: &Event) -> Result<(), Box<dyn std::error::Error>> {
    // Database operation
    println!("Saving event {} to database", event.id);
    tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
    Ok(())
}

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
    let stream = tokio_stream::wrappers::UnboundedReceiverStream::new(rx);

    let handle = tokio::spawn(async move {
        stream
            .subscribe(
                |event, _| async move {
                    save_to_db(&event).await?;
                    Ok::<_, Box<dyn std::error::Error>>(())
                },
                |err| eprintln!("Failed to save event: {}", err),
                None
            )
            .await
    });

    // Generate events
    for i in 0..10 {
        tx.send(Event { id: i, data: format!("Event {}", i) })?;
    }
    drop(tx);

    handle.await??;
    Ok(())
}
```

### Example 2: Search-As-You-Type


Cancel outdated searches when new queries arrive:

```rust

async fn search_api(query: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
    println!("Searching for: {}", query);
    tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
    Ok(vec![format!("Result for {}", query)])
}

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (tx, rx) = futures::channel::mpsc::unbounded();
    let stream = tokio_stream::wrappers::UnboundedReceiverStream::new(rx);

    let handle = tokio::spawn(async move {
        stream
            .subscribe_latest(
                |query: String, token| async move {
                    if token.is_cancelled() {
                        println!("Query '{}' cancelled", query);
                        return Ok(());
                    }

                    let results = search_api(&query).await?;

                    if !token.is_cancelled() {
                        println!("Results for '{}': {:?}", query, results);
                    }

                    Ok::<_, Box<dyn std::error::Error>>(())
                },
                None,
                None
            )
            .await
    });

    // User types rapidly
    tx.send("r".to_string())?;
    tx.send("ru".to_string())?;
    tx.send("rus".to_string())?;
    tx.send("rust".to_string())?; // Only this search completes
    drop(tx);

    handle.await??;
    Ok(())
}
```

### Example 3: Error Handling and Recovery


```rust

#[derive(Debug)]

struct ProcessingError(String);
impl core::fmt::Display for ProcessingError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "ProcessingError: {}", self.0)
    }
}
impl std::error::Error for ProcessingError {}

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (tx, rx) = futures::channel::mpsc::unbounded();
    let stream = tokio_stream::wrappers::UnboundedReceiverStream::new(rx);

    let error_count = Arc::new(Mutex::new(0));
    let error_count_clone = error_count.clone();

    let handle = tokio::spawn(async move {
        stream
            .subscribe(
                |item: i32, _| async move {
                    if item % 3 == 0 {
                        return Err(ProcessingError(format!("Cannot process {}", item)));
                    }
                    println!("Processed: {}", item);
                    Ok(())
                },
                move |err| {
                    eprintln!("Error occurred: {}", err);
                    let count = error_count_clone.clone();
                    tokio::spawn(async move {
                        *count.lock().await += 1;
                    });
                },
                None
            )
            .await
    });

    for i in 1..=10 {
        tx.send(i)?;
    }
    drop(tx);

    handle.await??;
    println!("Total errors: {}", *error_count.lock().await);
    Ok(())
}
```

### Example 4: Graceful Shutdown with Cancellation


```rust

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (tx, rx) = futures::channel::mpsc::unbounded();
    let stream = tokio_stream::wrappers::UnboundedReceiverStream::new(rx);
    let cancel_token = CancellationToken::new();
    let cancel_clone = cancel_token.clone();

    // Spawn shutdown handler
    tokio::spawn(async move {
        tokio::signal::ctrl_c().await.ok();
        println!("Shutting down gracefully...");
        cancel_clone.cancel();
    });

    let handle = tokio::spawn(async move {
        stream
            .subscribe(
                |item: i32, token| async move {
                    if token.is_cancelled() {
                        println!("Processing cancelled for item {}", item);
                        return Ok(());
                    }

                    println!("Processing: {}", item);
                    tokio::time::sleep(Duration::from_millis(100)).await;
                    Ok::<_, std::io::Error>(())
                },
                |_| {},
                Some(cancel_token)
            )
            .await
    });

    for i in 0..100 {
        if tx.send(i).is_err() {
            break;
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }

    handle.await??;
    Ok(())
}
```

## Use Cases


### Sequential Processing (`subscribe`)


| Use Case | Description |
|----------|-------------|
| **Event Logging** | Write every event to logs/database |
| **Transaction Processing** | Process financial transactions in order |
| **Message Queue Consumer** | Consume and acknowledge every message |
| **Audit Trail** | Maintain complete audit history |
| **Batch ETL** | Extract, transform, load data sequentially |
| **Notification Service** | Send every notification |
| **File Processing** | Process every file in a directory |

### Latest-Value Processing (`subscribe_latest`)


| Use Case | Description |
|----------|-------------|
| **UI Rendering** | Render only the latest application state |
| **Auto-Save** | Save current document (skip intermediate edits) |
| **Live Preview** | Update preview with latest content |
| **Search Suggestions** | Show results for latest query only |
| **Real-time Dashboard** | Display current metrics |
| **Configuration Reload** | Apply latest config changes |
| **Debounced API Calls** | Call API with latest parameters |

## Performance Characteristics


### Sequential Processing (`subscribe`)


- **Latency**: $O(n \times t)$ where $n$ is number of items, $t$ is processing time
- **Throughput**: Limited by handler execution time
- **Memory**: $O(1)$ - processes one item at a time
- **Ordering**: Strict sequential order maintained
- **Guarantees**: Every item processed exactly once

**Best for**: Correctness and completeness over speed

### Latest-Value Processing (`subscribe_latest`)


- **Latency**: $O(t)$ for latest item (intermediate items skipped)
- **Throughput**: Higher than sequential (skips work)
- **Memory**: $O(1)$ - one active task, one queued item
- **Ordering**: Processes latest available
- **Guarantees**: At most 2 items in flight (current + latest)

**Best for**: Responsiveness and efficiency over completeness

## Error Handling


### With Error Callback


Errors are passed to the callback, processing continues:

```rust
stream.subscribe(
    |item, _| async move {
        risky_operation(item).await?;
        Ok::<_, MyError>(())
    },
    |error| {
        eprintln!("Error: {}", error);
        // Log to monitoring service
        // Increment error counter
        // etc.
    },
    None
).await?; // Returns Ok(()) even if individual items failed
```

### Ignoring Errors


If you want to ignore errors and continue processing:

```rust
use fluxion_exec::ignore_errors;

stream.subscribe(handler, ignore_errors, None).await?;
// All items processed, errors are silently ignored
```

### Fail-Fast Pattern


Return error immediately to stop processing:

```rust
stream.subscribe(
    |item, _| async move {
        critical_operation(item).await?;
        Ok::<_, CriticalError>(())
    },
    None,
    None
).await?; // Stops on first error
```

## Cancellation


### Using CancellationToken


```rust

let cancel_token = CancellationToken::new();
let cancel_clone = cancel_token.clone();

// Start processing
let handle = tokio::spawn(async move {
    stream.subscribe(
        |item, token| async move {
            if token.is_cancelled() {
                return Ok(()); // Exit early
            }
            process(item).await
        },
        Some(cancel_token),
        None
    ).await
});

// Cancel from another task
tokio::spawn(async move {
    tokio::time::sleep(Duration::from_secs(10)).await;
    cancel_clone.cancel();
});
```

### Automatic Cancellation in `subscribe_latest`


The cancellation token passed to handlers is automatically cancelled when newer items arrive:

```rust
stream.subscribe_latest(
    |item, token| async move {
        for i in 0..100 {
            if token.is_cancelled() {
                println!("Item {} cancelled", item);
                return Ok(());
            }
            // Do work...
        }
        Ok(())
    },
    None,
    None
).await?;
```

## Common Patterns


### Pattern: Retry on Failure


```rust

stream.subscribe(
    |item, _| async move {
        let mut attempts = 0;
        loop {
            match process_with_retry(&item).await {
                Ok(()) => return Ok(()),
                Err(e) if attempts < 3 => {
                    attempts += 1;
                    eprintln!("Retry {} for item {:?}: {}", attempts, item, e);
                    tokio::time::sleep(Duration::from_millis(100 * attempts)).await;
                }
                Err(e) => return Err(e),
            }
        }
    },
    |_| {},
    None
).await?;
```

### Pattern: Rate Limiting


```rust

let last_process = Arc::new(Mutex::new(Instant::now()));

stream.subscribe(
    move |item, _| {
        let last = last_process.clone();
        async move {
            let mut last_instant = last.lock().await;
            let elapsed = last_instant.elapsed();
            if elapsed < Duration::from_millis(100) {
                tokio::time::sleep(Duration::from_millis(100) - elapsed).await;
            }
            *last_instant = Instant::now();
            drop(last_instant);

            process(item).await
        }
    },
    |_| {},
    None
).await?;
```

### Pattern: Batch Processing


```rust

stream
    .chunks(100)  // Batch 100 items
    .subscribe(
        |batch, _| async move {
            process_batch(&batch).await?;
            Ok::<_, MyError>(())
        },
        |_| {},
        None
    )
    .await?;
```

### Pattern: Conditional Processing


```rust
stream
    .filter(|item| futures::future::ready(item.is_important()))
    .subscribe(
        |item, _| async move {
            process_important(item).await
        },
        |_| {},
        None
    )
    .await?;
```

## Anti-Patterns


### ❌ Don't: Use `subscribe_latest` for Critical Work


```rust
// BAD: Payment processing might be skipped!
payment_stream.subscribe_latest(
    |payment, _| async move {
        process_payment(payment).await  // Could be cancelled!
    },
    None,
    None
).await?;
```

✅ **Good**: Use `subscribe` for work that must complete:

```rust
payment_stream.subscribe(
    |payment, _| async move {
        process_payment(payment).await  // Every payment processed
    },
    |_| {},
    None
).await?;
```

### ❌ Don't: Block in Async Handlers


```rust
// BAD: Blocking operations stall the executor
stream.subscribe(
    |item, _| async move {
        std::thread::sleep(Duration::from_secs(1));  // Blocks executor!
        Ok(())
    },
    None,
    None
).await?;
```

✅ **Good**: Use async operations:

```rust
stream.subscribe(
    |item, _| async move {
        tokio::time::sleep(Duration::from_secs(1)).await;  // Non-blocking
        Ok(())
    },
    |_| {},
    None
).await?;
```

### ❌ Don't: Perform CPU-Intensive Work on Async Runtime


```rust
// BAD: CPU work blocks async tasks
stream.subscribe(
    |data, _| async move {
        expensive_computation(data);  // Blocks!
        Ok(())
    },
    |_| {},
    None
).await?;
```

✅ **Good**: Use `spawn_blocking` for CPU work:

```rust
stream.subscribe(
    |data, _| async move {
        tokio::task::spawn_blocking(move || {
            expensive_computation(data)
        }).await??;
        Ok::<_, Box<dyn std::error::Error>>(())
    },
    |_| {},
    None
).await?;
```

### ❌ Don't: Ignore Cancellation Tokens


```rust
// BAD: Long-running work that can't be cancelled
stream.subscribe_latest(
    |item, _token| async move {  // Token ignored!
        for i in 0..1000000 {
            expensive_step(i).await;
        }
        Ok(())
    },
    None,
    None
).await?;
```

✅ **Good**: Check cancellation periodically:

```rust
stream.subscribe_latest(
    |item, token| async move {
        for i in 0..1000000 {
            if token.is_cancelled() {
                return Ok(());  // Exit early
            }
            expensive_step(i).await;
        }
        Ok(())
    },
    None,
    None
).await?;
```

## Comparison with Alternatives


### vs `futures::StreamExt::for_each`


| Feature | `subscribe` | `for_each` |
|---------|-------------------|------------|
| **Execution** | Spawns tasks | Inline execution |
| **Cancellation** | Built-in token support | Manual |
| **Error handling** | Callbacks + collection | Returns first error |
| **Concurrency** | Configurable | Sequential only |

### vs `futures::StreamExt::buffer_unordered`


| Feature | `subscribe` | `buffer_unordered` |
|---------|-------------------|--------------------|
| **Ordering** | Sequential | Unordered |
| **Concurrency** | One at a time | N concurrent |
| **Backpressure** | Automatic | Manual (buffer size) |
| **Use case** | Sequential processing | Parallel processing |

### vs Manual Task Spawning


| Feature | `subscribe_latest` | Manual spawning |
|---------|-------------------------|------------------|
| **Cancellation** | Automatic | Manual |
| **Latest-value** | Built-in | Manual tracking |
| **Error handling** | Integrated | Manual |
| **Cleanup** | Automatic | Manual |

## Troubleshooting


### Problem: Handler Never Completes


**Symptoms**: Stream processing hangs indefinitely

**Causes**:
- Awaiting on an infinite stream without cancellation
- Deadlock in handler
- Blocking operation in async context

**Solutions**:
```rust
// Add timeout
use tokio::time::{timeout, Duration};

let result = timeout(
    Duration::from_secs(30),
    stream.subscribe(handler, None, None)
).await??;

// Use cancellation token
let cancel = CancellationToken::new();
tokio::spawn(async move {
    tokio::time::sleep(Duration::from_secs(30)).await;
    cancel.cancel();
});
```

### Problem: High Memory Usage


**Symptoms**: Memory grows unbounded during processing

**Causes**:
- Stream produces items faster than they can be processed
- Large items held in memory
- Memory leaks in handler

**Solutions**:
```rust
// Add backpressure with bounded channels
let (tx, rx) = futures::channel::mpsc::channel(100); // Bounded

// Use subscribe_latest to skip items
stream.subscribe_latest(handler, None, None).await?;

// Process in batches and clear
stream.chunks(100).subscribe(
    |mut batch, _| async move {
        process(&batch).await?;
        batch.clear(); // Free memory
        Ok(())
    },
    None,
    None
).await?;
```

### Problem: Errors Not Propagated


**Symptoms**: Errors occur but are silently ignored

**Cause**: Error callback provided, but errors not handled

**Solution**:
```rust
let errors = Arc::new(Mutex::new(Vec::new()));
let errors_clone = errors.clone();

stream.subscribe(
    handler,
    None,
    Some(move |err| {
        let errors = errors_clone.clone();
        tokio::spawn(async move {
            errors.lock().await.push(err.to_string());
        });
    })
).await?;

// Check errors after processing
let all_errors = errors.lock().await;
if !all_errors.is_empty() {
    eprintln!("Errors occurred: {:?}", all_errors);
}
```

### Problem: Items Processed Out of Order


**Symptoms**: Items appear in unexpected order

**Cause**: Using concurrent processing patterns

**Solution**: Use `subscribe` (strictly sequential) instead of `buffer_unordered` or parallel patterns

## API Reference


See the [full API documentation](https://docs.rs/fluxion-exec) for detailed type signatures and additional examples.

### Core Traits


- [`SubscribeExt`]https://docs.rs/fluxion-exec/latest/fluxion_exec/trait.SubscribeExt.html - Sequential processing
- [`SubscribeLatestExt`]https://docs.rs/fluxion-exec/latest/fluxion_exec/trait.SubscribeLatestExt.html - Latest-value processing

### Related Crates


- [`fluxion-stream`]../fluxion-stream - Stream composition operators
- [`fluxion-core`]../fluxion-core - Core types and errors

## License


Licensed under the Apache License, Version 2.0. See [LICENSE](../LICENSE) for details.

Copyright 2025 Umberto Gotti