wasm-sandbox 0.4.1

A secure WebAssembly sandbox with dead-simple ease of use, progressive complexity APIs, and comprehensive safety controls
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
# Streaming Execution Guide


📖 **[← Back to Documentation]../README.md** | 🏠 **[← Main README]../../README.md** | 🚀 **[API Reference]https://docs.rs/wasm-sandbox**

Handle large data processing with streaming patterns, memory-efficient techniques, and backpressure management in wasm-sandbox.

## Overview


Streaming execution enables:

- **Memory Efficiency** - Process data larger than available memory
- **Low Latency** - Start processing before all data arrives
- **Backpressure** - Handle flow control automatically
- **Fault Tolerance** - Resume processing from checkpoints

## Quick Start


```rust
use wasm_sandbox::{StreamingSandbox, StreamConfig};
use tokio_stream::{Stream, StreamExt};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let sandbox = StreamingSandbox::builder()
        .buffer_size(64 * 1024)  // 64KB buffer
        .max_concurrency(4)
        .enable_backpressure(true)
        .build()
        .await?;

    // Create streaming processor
    let mut processor = sandbox.create_stream_processor("data_transformer.wasm").await?;

    // Process data stream
    let input_stream = create_data_stream();
    let output_stream = processor.process_stream(input_stream).await?;

    // Consume results
    tokio::pin!(output_stream);
    while let Some(result) = output_stream.next().await {
        match result {
            Ok(data) => println!("Processed: {:?}", data),
            Err(e) => eprintln!("Error: {}", e),
        }
    }

    Ok(())
}

async fn create_data_stream() -> impl Stream<Item = Vec<u8>> {
    tokio_stream::iter(vec![
        vec![1, 2, 3],
        vec![4, 5, 6],
        vec![7, 8, 9],
    ])
}
```

## Stream Processing Architecture


```rust
pub struct StreamProcessor {
    sandbox: WasmSandbox,
    config: StreamConfig,
    buffer_manager: BufferManager,
    flow_controller: FlowController,
    checkpointer: Checkpointer,
}

#[derive(Debug, Clone)]

pub struct StreamConfig {
    pub buffer_size: usize,
    pub max_concurrency: usize,
    pub enable_backpressure: bool,
    pub checkpoint_interval: Duration,
    pub recovery_strategy: RecoveryStrategy,
    pub flow_control: FlowControlConfig,
}

impl StreamProcessor {
    pub async fn process_stream<I, O>(&mut self, input: I) -> Result<impl Stream<Item = Result<O, StreamError>>, StreamError>
    where
        I: Stream<Item = Vec<u8>> + Send + 'static,
        O: for<'de> serde::Deserialize<'de> + Send + 'static,
    {
        let (tx, rx) = tokio::sync::mpsc::channel(self.config.buffer_size);
        
        // Start processing task
        let processor_handle = self.start_processing_task(input, tx).await?;
        
        // Return output stream
        Ok(tokio_stream::wrappers::ReceiverStream::new(rx))
    }

    async fn start_processing_task<I>(&self, mut input: I, output: tokio::sync::mpsc::Sender<Result<Vec<u8>, StreamError>>) -> Result<tokio::task::JoinHandle<()>, StreamError>
    where
        I: Stream<Item = Vec<u8>> + Send + 'static,
    {
        let sandbox = self.sandbox.clone();
        let config = self.config.clone();
        let mut checkpointer = self.checkpointer.clone();
        let flow_controller = self.flow_controller.clone();

        let handle = tokio::spawn(async move {
            let mut buffer = Vec::new();
            let mut chunk_count = 0u64;

            tokio::pin!(input);
            
            while let Some(chunk) = input.next().await {
                // Check backpressure
                if config.enable_backpressure {
                    flow_controller.wait_for_capacity().await;
                }

                buffer.extend_from_slice(&chunk);

                // Process when buffer is full or stream ends
                if buffer.len() >= config.buffer_size {
                    match Self::process_chunk(&sandbox, &buffer).await {
                        Ok(result) => {
                            if output.send(Ok(result)).await.is_err() {
                                break; // Receiver dropped
                            }
                        }
                        Err(e) => {
                            if output.send(Err(e)).await.is_err() {
                                break;
                            }
                        }
                    }

                    buffer.clear();
                    chunk_count += 1;

                    // Checkpoint periodically
                    if chunk_count % 100 == 0 {
                        if let Err(e) = checkpointer.save_checkpoint(chunk_count).await {
                            eprintln!("Checkpoint save failed: {}", e);
                        }
                    }
                }
            }

            // Process remaining data
            if !buffer.is_empty() {
                if let Ok(result) = Self::process_chunk(&sandbox, &buffer).await {
                    let _ = output.send(Ok(result)).await;
                }
            }
        });

        Ok(handle)
    }

    async fn process_chunk(sandbox: &WasmSandbox, data: &[u8]) -> Result<Vec<u8>, StreamError> {
        sandbox.call("process_chunk", data).await
            .map_err(StreamError::ProcessingFailed)
    }
}
```

## Memory Management


```rust
pub struct BufferManager {
    pools: Vec<MemoryPool>,
    current_pool: usize,
    allocation_strategy: AllocationStrategy,
}

impl BufferManager {
    pub fn new(config: &BufferConfig) -> Self {
        let mut pools = Vec::new();
        
        // Create multiple memory pools for different sizes
        pools.push(MemoryPool::new(1024, 100));      // 1KB buffers
        pools.push(MemoryPool::new(64 * 1024, 50));  // 64KB buffers
        pools.push(MemoryPool::new(1024 * 1024, 10)); // 1MB buffers

        Self {
            pools,
            current_pool: 0,
            allocation_strategy: config.allocation_strategy,
        }
    }

    pub fn allocate(&mut self, size: usize) -> Result<Buffer, AllocationError> {
        // Find appropriate pool
        let pool_index = self.find_suitable_pool(size)?;
        
        // Allocate from pool
        self.pools[pool_index].allocate()
            .ok_or(AllocationError::PoolExhausted)
    }

    pub fn deallocate(&mut self, buffer: Buffer) {
        if let Some(pool) = self.pools.get_mut(buffer.pool_index) {
            pool.deallocate(buffer);
        }
    }
}

pub struct MemoryPool {
    buffer_size: usize,
    available: Vec<Buffer>,
    total_allocated: usize,
    max_buffers: usize,
}

impl MemoryPool {
    pub fn allocate(&mut self) -> Option<Buffer> {
        if let Some(buffer) = self.available.pop() {
            Some(buffer)
        } else if self.total_allocated < self.max_buffers {
            // Allocate new buffer
            let data = vec![0u8; self.buffer_size];
            self.total_allocated += 1;
            Some(Buffer {
                data: data.into_boxed_slice(),
                pool_index: 0, // Set by BufferManager
            })
        } else {
            None
        }
    }
}
```

## Flow Control and Backpressure


```rust
pub struct FlowController {
    capacity: Arc<Semaphore>,
    metrics: FlowMetrics,
    config: FlowControlConfig,
}

#[derive(Debug, Clone)]

pub struct FlowControlConfig {
    pub max_in_flight: usize,
    pub low_watermark: usize,
    pub high_watermark: usize,
    pub backpressure_strategy: BackpressureStrategy,
}

#[derive(Debug, Clone)]

pub enum BackpressureStrategy {
    Block,
    Drop,
    Sample,
    Adaptive,
}

impl FlowController {
    pub async fn wait_for_capacity(&self) {
        match self.config.backpressure_strategy {
            BackpressureStrategy::Block => {
                let _permit = self.capacity.acquire().await.unwrap();
                // Permit is automatically released when dropped
            }
            BackpressureStrategy::Drop => {
                if self.capacity.available_permits() == 0 {
                    self.metrics.record_drop();
                    return;
                }
            }
            BackpressureStrategy::Adaptive => {
                self.adaptive_backpressure().await;
            }
            _ => {}
        }
    }

    async fn adaptive_backpressure(&self) {
        let current_load = self.calculate_current_load();
        
        if current_load > 0.8 {
            // High load - increase backpressure
            tokio::time::sleep(Duration::from_millis(10)).await;
        } else if current_load < 0.3 {
            // Low load - reduce backpressure
            // Continue without delay
        } else {
            // Medium load - moderate backpressure
            tokio::time::sleep(Duration::from_millis(1)).await;
        }
    }
}
```

## Checkpointing and Recovery


```rust
pub struct Checkpointer {
    storage: CheckpointStorage,
    interval: Duration,
    last_checkpoint: Option<Instant>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]

pub struct Checkpoint {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub position: StreamPosition,
    pub state: ProcessorState,
    pub metadata: HashMap<String, serde_json::Value>,
}

impl Checkpointer {
    pub async fn save_checkpoint(&mut self, position: u64) -> Result<(), CheckpointError> {
        let now = Instant::now();
        
        if let Some(last) = self.last_checkpoint {
            if now.duration_since(last) < self.interval {
                return Ok(()); // Too soon for next checkpoint
            }
        }

        let checkpoint = Checkpoint {
            timestamp: chrono::Utc::now(),
            position: StreamPosition::Offset(position),
            state: self.capture_processor_state().await?,
            metadata: HashMap::new(),
        };

        self.storage.save(checkpoint).await?;
        self.last_checkpoint = Some(now);
        
        Ok(())
    }

    pub async fn restore_from_checkpoint(&self) -> Result<Option<Checkpoint>, CheckpointError> {
        self.storage.load_latest().await
    }
}

pub trait CheckpointStorage: Send + Sync {
    async fn save(&self, checkpoint: Checkpoint) -> Result<(), CheckpointError>;
    async fn load_latest(&self) -> Result<Option<Checkpoint>, CheckpointError>;
    async fn list_checkpoints(&self) -> Result<Vec<CheckpointInfo>, CheckpointError>;
}

pub struct FileCheckpointStorage {
    directory: PathBuf,
}

impl CheckpointStorage for FileCheckpointStorage {
    async fn save(&self, checkpoint: Checkpoint) -> Result<(), CheckpointError> {
        let filename = format!("checkpoint_{}.json", checkpoint.timestamp.timestamp());
        let path = self.directory.join(filename);
        
        let data = serde_json::to_vec_pretty(&checkpoint)?;
        tokio::fs::write(path, data).await?;
        
        Ok(())
    }
}
```

## Performance Optimization


```rust
pub struct StreamOptimizer {
    profiler: StreamProfiler,
    adaptive_config: AdaptiveConfig,
}

impl StreamOptimizer {
    pub async fn optimize_stream(&mut self, processor: &mut StreamProcessor) -> Result<(), OptimizationError> {
        let metrics = self.profiler.collect_metrics().await;
        
        // Analyze bottlenecks
        if metrics.memory_pressure > 0.8 {
            self.optimize_memory_usage(processor).await?;
        }
        
        if metrics.cpu_utilization < 0.5 {
            self.increase_concurrency(processor).await?;
        }
        
        if metrics.backpressure_events > 100 {
            self.adjust_flow_control(processor).await?;
        }
        
        Ok(())
    }

    async fn optimize_memory_usage(&self, processor: &mut StreamProcessor) -> Result<(), OptimizationError> {
        // Reduce buffer sizes
        processor.config.buffer_size = (processor.config.buffer_size * 3) / 4;
        
        // Enable more aggressive garbage collection
        processor.enable_aggressive_gc();
        
        Ok(())
    }
}
```

Next: **[Development Tools Integration](development-tools.md)** - IDE and debugging support

---

**Streaming Excellence:** Process unlimited data efficiently with memory-conscious streaming patterns.