stillwater 1.0.1

Pragmatic effect composition and validation for Rust - pure core, imperative shell
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
//! Retry Patterns Example
//!
//! Demonstrates retry and resilience patterns for Effect-based computations.
//! Shows practical patterns including:
//! - Basic retry with different backoff strategies
//! - Conditional retry (retry_if)
//! - Retry with observability hooks
//! - Timeout handling
//! - Combining retry with timeout for robust I/O operations

use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;

use stillwater::effect::prelude::*;
use stillwater::{RetryPolicy, TimeoutError};

// ==================== Basic Retry ====================

/// Example 1: Basic retry with exponential backoff
///
/// Demonstrates retrying an operation that fails transiently.
async fn example_basic_retry() {
    println!("\n=== Example 1: Basic Retry ===");

    // Track the number of attempts
    let attempts = Arc::new(AtomicU32::new(0));

    let effect = retry(
        {
            let attempts = attempts.clone();
            move || {
                let attempts = attempts.clone();
                from_async(move |_: &()| {
                    let attempts = attempts.clone();
                    async move {
                        let n = attempts.fetch_add(1, Ordering::SeqCst);
                        println!("  Attempt {}", n + 1);
                        if n < 2 {
                            Err("transient failure")
                        } else {
                            Ok("success!")
                        }
                    }
                })
            }
        },
        RetryPolicy::exponential(Duration::from_millis(100)).with_max_retries(5),
    );

    let result = effect.run_standalone().await;

    match result {
        Ok(success) => {
            let attempts = success.attempts;
            let value = success.into_value();
            println!("Success after {} attempts: {}", attempts, value);
        }
        Err(exhausted) => {
            println!(
                "Failed after {} attempts: {}",
                exhausted.attempts, exhausted.final_error
            );
        }
    }
}

// ==================== Different Backoff Strategies ====================

/// Example 2: Comparing different backoff strategies
///
/// Shows how delay increases with different strategies.
async fn example_backoff_strategies() {
    println!("\n=== Example 2: Backoff Strategies ===");

    // Constant delay
    let constant = RetryPolicy::constant(Duration::from_millis(100)).with_max_retries(5);
    println!("Constant delays:");
    for i in 0..5 {
        if let Some(d) = constant.delay_for_attempt(i) {
            println!("  Attempt {}: {:?}", i + 1, d);
        }
    }

    // Linear delay
    let linear = RetryPolicy::linear(Duration::from_millis(100)).with_max_retries(5);
    println!("\nLinear delays:");
    for i in 0..5 {
        if let Some(d) = linear.delay_for_attempt(i) {
            println!("  Attempt {}: {:?}", i + 1, d);
        }
    }

    // Exponential delay
    let exponential = RetryPolicy::exponential(Duration::from_millis(100)).with_max_retries(5);
    println!("\nExponential delays:");
    for i in 0..5 {
        if let Some(d) = exponential.delay_for_attempt(i) {
            println!("  Attempt {}: {:?}", i + 1, d);
        }
    }

    // Fibonacci delay
    let fibonacci = RetryPolicy::fibonacci(Duration::from_millis(100)).with_max_retries(5);
    println!("\nFibonacci delays:");
    for i in 0..5 {
        if let Some(d) = fibonacci.delay_for_attempt(i) {
            println!("  Attempt {}: {:?}", i + 1, d);
        }
    }
}

// ==================== Conditional Retry ====================

/// Example 3: Retry only on specific errors
///
/// Demonstrates retry_if to distinguish transient from permanent errors.
async fn example_conditional_retry() {
    println!("\n=== Example 3: Conditional Retry ===");

    #[derive(Debug, Clone, PartialEq)]
    enum AppError {
        Transient(String),
        Permanent(String),
    }

    let attempts = Arc::new(AtomicU32::new(0));

    // This effect returns a Permanent error - should NOT retry
    let effect = retry_if(
        {
            let attempts = attempts.clone();
            move || {
                let attempts = attempts.clone();
                from_async(move |_: &()| {
                    let attempts = attempts.clone();
                    async move {
                        attempts.fetch_add(1, Ordering::SeqCst);
                        println!("  Attempting...");
                        Err::<(), _>(AppError::Permanent("invalid credentials".to_string()))
                    }
                })
            }
        },
        RetryPolicy::constant(Duration::from_millis(100)).with_max_retries(5),
        |err| matches!(err, AppError::Transient(_)),
    );

    let result = effect.run_standalone().await;
    println!("Permanent error (no retries): {:?}", result.unwrap_err());
    println!("Total attempts: {}", attempts.load(Ordering::SeqCst));

    // Reset attempts
    attempts.store(0, Ordering::SeqCst);

    // This effect returns Transient errors then succeeds
    let effect = retry_if(
        {
            let attempts = attempts.clone();
            move || {
                let attempts = attempts.clone();
                from_async(move |_: &()| {
                    let attempts = attempts.clone();
                    async move {
                        let n = attempts.fetch_add(1, Ordering::SeqCst);
                        println!("  Attempt {}", n + 1);
                        if n < 2 {
                            Err(AppError::Transient("connection timeout".to_string()))
                        } else {
                            Ok("connected!")
                        }
                    }
                })
            }
        },
        RetryPolicy::constant(Duration::from_millis(50)).with_max_retries(5),
        |err| matches!(err, AppError::Transient(_)),
    );

    let result = effect.run_standalone().await;
    println!("\nTransient errors then success: {:?}", result.unwrap());
    println!("Total attempts: {}", attempts.load(Ordering::SeqCst));
}

// ==================== Retry with Observability ====================

/// Example 4: Retry with hooks for logging/metrics
///
/// Demonstrates retry_with_hooks for observability.
async fn example_retry_with_hooks() {
    println!("\n=== Example 4: Retry with Hooks ===");

    let attempts = Arc::new(AtomicU32::new(0));

    let effect = retry_with_hooks(
        {
            let attempts = attempts.clone();
            move || {
                let attempts = attempts.clone();
                from_async(move |_: &()| {
                    let attempts = attempts.clone();
                    async move {
                        let n = attempts.fetch_add(1, Ordering::SeqCst);
                        if n < 3 {
                            Err(format!("error on attempt {}", n + 1))
                        } else {
                            Ok("finally succeeded!")
                        }
                    }
                })
            }
        },
        RetryPolicy::exponential(Duration::from_millis(50)).with_max_retries(5),
        |event| {
            // This hook is called before each retry
            println!(
                "  [HOOK] Attempt {} failed with: {:?}",
                event.attempt, event.error
            );
            if let Some(delay) = event.next_delay {
                println!("         Waiting {:?} before retry...", delay);
            } else {
                println!("         No more retries!");
            }
            println!("         Total elapsed: {:?}", event.elapsed);
        },
    );

    let result = effect.run_standalone().await;

    match result {
        Ok(success) => {
            let attempts = success.attempts;
            let value = success.into_value();
            println!("\nSuccess after {} attempts: {}", attempts, value);
        }
        Err(exhausted) => {
            println!(
                "\nFailed after {} attempts: {}",
                exhausted.attempts, exhausted.final_error
            );
        }
    }
}

// ==================== Timeout ====================

/// Example 5: Effect with timeout
///
/// Demonstrates with_timeout for operations that may hang.
async fn example_timeout() {
    println!("\n=== Example 5: Timeout ===");

    // Effect that takes too long
    let slow_effect = with_timeout(
        from_async(|_: &()| async {
            println!("  Starting slow operation...");
            tokio::time::sleep(Duration::from_secs(10)).await;
            Ok::<_, String>("done")
        }),
        Duration::from_millis(100),
    );

    println!("Running with 100ms timeout:");
    match slow_effect.run_standalone().await {
        Ok(value) => println!("  Completed: {}", value),
        Err(TimeoutError::Timeout { duration }) => {
            println!("  Timed out after {:?}", duration);
        }
        Err(TimeoutError::Inner(e)) => println!("  Inner error: {}", e),
    }

    // Effect that completes in time
    let fast_effect = with_timeout(
        from_async(|_: &()| async {
            println!("\n  Starting fast operation...");
            tokio::time::sleep(Duration::from_millis(10)).await;
            Ok::<_, String>("done quickly!")
        }),
        Duration::from_millis(100),
    );

    println!("Running with 100ms timeout:");
    match fast_effect.run_standalone().await {
        Ok(value) => println!("  Completed: {}", value),
        Err(TimeoutError::Timeout { duration }) => {
            println!("  Timed out after {:?}", duration);
        }
        Err(TimeoutError::Inner(e)) => println!("  Inner error: {}", e),
    }
}

// ==================== Retry with Per-Attempt Timeout ====================

/// Example 6: Combining retry with timeout
///
/// Demonstrates the recommended pattern for robust I/O operations:
/// - Per-attempt timeout to catch hanging operations
/// - Retry with backoff for transient failures
/// - Overall timeout for the entire retry sequence
async fn example_retry_with_timeout() {
    println!("\n=== Example 6: Retry with Timeout ===");

    let attempts = Arc::new(AtomicU32::new(0));

    // Each attempt has its own timeout, and we retry on timeout
    let effect = retry(
        {
            let attempts = attempts.clone();
            move || {
                let attempts = attempts.clone();
                with_timeout(
                    from_async(move |_: &()| {
                        let attempts = attempts.clone();
                        async move {
                            let n = attempts.fetch_add(1, Ordering::SeqCst);
                            println!("  Attempt {} starting...", n + 1);

                            if n < 2 {
                                // First two attempts are slow (will timeout)
                                tokio::time::sleep(Duration::from_millis(500)).await;
                            }
                            // Third attempt is fast
                            tokio::time::sleep(Duration::from_millis(10)).await;
                            Ok::<_, String>("connected!")
                        }
                    }),
                    Duration::from_millis(100),
                )
                .map_err(|e| format!("{}", e))
            }
        },
        RetryPolicy::constant(Duration::from_millis(50)).with_max_retries(5),
    );

    let result = effect.run_standalone().await;

    match result {
        Ok(success) => {
            let attempts = success.attempts;
            let value = success.into_value();
            println!("\nSuccess after {} attempts: {}", attempts, value);
        }
        Err(exhausted) => {
            println!(
                "\nFailed after {} attempts: {}",
                exhausted.attempts, exhausted.final_error
            );
        }
    }
}

// ==================== Max Delay Cap ====================

/// Example 7: Using max_delay to cap exponential growth
///
/// Demonstrates how max_delay prevents delays from growing too large.
async fn example_max_delay() {
    println!("\n=== Example 7: Max Delay Cap ===");

    let policy = RetryPolicy::exponential(Duration::from_millis(100))
        .with_max_retries(10)
        .with_max_delay(Duration::from_millis(500));

    println!("Exponential backoff with 500ms cap:");
    for i in 0..10 {
        if let Some(d) = policy.delay_for_attempt(i) {
            println!("  Attempt {}: {:?}", i + 1, d);
        }
    }
}

// ==================== Real-World Pattern: HTTP Client ====================

/// Example 8: Simulated HTTP client with retry
///
/// Demonstrates a realistic pattern for API calls.
async fn example_http_pattern() {
    println!("\n=== Example 8: HTTP Client Pattern ===");

    // Simulated HTTP response
    #[derive(Debug, Clone)]
    enum HttpError {
        Timeout,
        ServerError(u16),
        ClientError(u16),
    }

    impl std::fmt::Display for HttpError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                HttpError::Timeout => write!(f, "request timed out"),
                HttpError::ServerError(code) => write!(f, "server error: {}", code),
                HttpError::ClientError(code) => write!(f, "client error: {}", code),
            }
        }
    }

    // Only retry on timeouts and server errors, not client errors
    fn is_retryable(err: &HttpError) -> bool {
        matches!(err, HttpError::Timeout | HttpError::ServerError(_))
    }

    let attempts = Arc::new(AtomicU32::new(0));

    // Simulate an API that fails twice with server errors then succeeds
    let effect = retry_if(
        {
            let attempts = attempts.clone();
            move || {
                let attempts = attempts.clone();
                from_async(move |_: &()| {
                    let attempts = attempts.clone();
                    async move {
                        let n = attempts.fetch_add(1, Ordering::SeqCst);
                        println!("  HTTP request attempt {}", n + 1);

                        match n {
                            0 => Err(HttpError::ServerError(503)), // Service Unavailable
                            1 => Err(HttpError::Timeout),
                            _ => Ok("{ \"status\": \"ok\" }"),
                        }
                    }
                })
            }
        },
        RetryPolicy::exponential(Duration::from_millis(100))
            .with_max_retries(5)
            .with_max_delay(Duration::from_secs(2)),
        is_retryable,
    );

    let result = effect.run_standalone().await;

    match result {
        Ok(body) => println!("\nResponse: {}", body),
        Err(e) => println!("\nRequest failed: {}", e),
    }

    // Now demonstrate that client errors (4xx) are NOT retried
    println!("\n--- Client Error (should NOT retry) ---");
    let attempts = Arc::new(AtomicU32::new(0));

    let effect = retry_if(
        {
            let attempts = attempts.clone();
            move || {
                let attempts = attempts.clone();
                from_async(move |_: &()| {
                    let attempts = attempts.clone();
                    async move {
                        attempts.fetch_add(1, Ordering::SeqCst);
                        println!("  HTTP request attempt");
                        // Client error - bad request, should NOT be retried
                        Err::<&str, _>(HttpError::ClientError(400))
                    }
                })
            }
        },
        RetryPolicy::exponential(Duration::from_millis(100))
            .with_max_retries(5)
            .with_max_delay(Duration::from_secs(2)),
        is_retryable,
    );

    let result = effect.run_standalone().await;

    match result {
        Ok(body) => println!("\nResponse: {}", body),
        Err(e) => println!("\nRequest failed (no retries for client error): {}", e),
    }
    println!("Total attempts: {}", attempts.load(Ordering::SeqCst));
}

#[tokio::main]
async fn main() {
    println!("======================================");
    println!("       Retry Patterns Example         ");
    println!("======================================");

    example_basic_retry().await;
    example_backoff_strategies().await;
    example_conditional_retry().await;
    example_retry_with_hooks().await;
    example_timeout().await;
    example_retry_with_timeout().await;
    example_max_delay().await;
    example_http_pattern().await;

    println!("\n======================================");
    println!("           Examples Complete           ");
    println!("======================================");
}