streamweave 0.10.1

Composable, async, stream-first computation in pure Rust
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
//! # Retry Node
//!
//! A transform node that retries failed operations with exponential backoff.
//!
//! ## Ports
//!
//! - **Input**: `"configuration"` - Receives configuration updates (currently unused, for consistency)
//! - **Input**: `"in"` - Receives data items to process
//! - **Input**: `"max_retries"` - Receives the maximum number of retry attempts (numeric value)
//! - **Output**: `"out"` - Sends successful results after retries
//! - **Output**: `"error"` - Sends errors that occur after all retries are exhausted
//!
//! ## Behavior
//!
//! The node processes each item from the "in" port by:
//! 1. Applying the retry function (from configuration) to the item
//! 2. If successful, sending the result to "out"
//! 3. If it fails, retrying up to max_retries times with exponential backoff
//! 4. After all retries exhausted, sending the error to "error" port
//!
//! Exponential backoff: delay = base_delay * (2 ^ retry_count)
//! Base delay is 100ms by default.

use crate::node::{InputStreams, Node, NodeExecutionError, OutputStreams};
use crate::nodes::common::BaseNode;
use async_trait::async_trait;
use futures::stream;
use std::any::Any;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::time::sleep;
use tokio_stream::{StreamExt, wrappers::ReceiverStream};

/// Trait for asynchronous retry functions used by RetryNode.
///
/// Implementations of this trait define how to process input data.
/// The function receives an `Arc<dyn Any + Send + Sync>` and returns
/// either `Ok(result)` on success or `Err(error)` on failure.
#[async_trait]
pub trait RetryFunction: Send + Sync {
  /// Applies the retry function to the input value.
  ///
  /// # Arguments
  ///
  /// * `value` - The input value wrapped in `Arc<dyn Any + Send + Sync>`
  ///
  /// # Returns
  ///
  /// `Ok(result)` if processing succeeds, or `Err(error)` if it fails.
  async fn apply(
    &self,
    value: Arc<dyn Any + Send + Sync>,
  ) -> Result<Arc<dyn Any + Send + Sync>, Arc<dyn Any + Send + Sync>>;
}

/// Configuration for RetryNode that defines the retry function.
pub type RetryConfig = Arc<dyn RetryFunction>;

/// Wrapper type that implements RetryFunction for async closures.
struct RetryFunctionWrapper<F> {
  /// The async function to wrap.
  function: F,
}

#[async_trait]

impl<F, Fut> RetryFunction for RetryFunctionWrapper<F>
where
  F: Fn(Arc<dyn Any + Send + Sync>) -> Fut + Send + Sync,
  Fut: std::future::Future<Output = Result<Arc<dyn Any + Send + Sync>, Arc<dyn Any + Send + Sync>>>
    + Send,
{
  async fn apply(
    &self,
    value: Arc<dyn Any + Send + Sync>,
  ) -> Result<Arc<dyn Any + Send + Sync>, Arc<dyn Any + Send + Sync>> {
    (self.function)(value).await
  }
}

/// Helper function to create a RetryConfig from an async closure.
pub fn retry_config<F, Fut>(function: F) -> RetryConfig
where
  F: Fn(Arc<dyn Any + Send + Sync>) -> Fut + Send + Sync + 'static,
  Fut: std::future::Future<Output = Result<Arc<dyn Any + Send + Sync>, Arc<dyn Any + Send + Sync>>>
    + Send
    + 'static,
{
  Arc::new(RetryFunctionWrapper {
    function: move |v| {
      Box::pin((function)(v))
        as Pin<
          Box<
            dyn std::future::Future<
                Output = Result<Arc<dyn Any + Send + Sync>, Arc<dyn Any + Send + Sync>>,
              > + Send,
          >,
        >
    },
  })
}

/// Helper function to extract usize from various numeric types.
fn get_usize(value: &Arc<dyn Any + Send + Sync>) -> Result<usize, String> {
  if let Ok(arc_usize) = value.clone().downcast::<usize>() {
    Ok(*arc_usize)
  } else if let Ok(arc_i32) = value.clone().downcast::<i32>() {
    if *arc_i32 < 0 {
      return Err("Max retries cannot be negative".to_string());
    }
    (*arc_i32)
      .try_into()
      .map_err(|_| "Max retries too large".to_string())
  } else if let Ok(arc_i64) = value.clone().downcast::<i64>() {
    if *arc_i64 < 0 {
      return Err("Max retries cannot be negative".to_string());
    }
    (*arc_i64)
      .try_into()
      .map_err(|_| "Max retries too large".to_string())
  } else if let Ok(arc_u32) = value.clone().downcast::<u32>() {
    (*arc_u32)
      .try_into()
      .map_err(|_| "Max retries too large".to_string())
  } else if let Ok(arc_u64) = value.clone().downcast::<u64>() {
    (*arc_u64)
      .try_into()
      .map_err(|_| "Max retries too large".to_string())
  } else {
    Err(format!(
      "Unsupported type for max_retries: {} (must be numeric)",
      std::any::type_name_of_val(&**value)
    ))
  }
}

/// Enum to tag messages from different input ports for merging.
#[derive(Debug, PartialEq)]
#[allow(dead_code)]
enum InputPort {
  /// Configuration port.
  Config,
  /// Input data port.
  In,
  /// Maximum retries port.
  MaxRetries,
}

/// A node that retries failed operations with exponential backoff.
///
/// The node processes each item from the "in" port by applying the retry function.
/// If the function fails, it retries up to max_retries times with exponential backoff.
/// Base delay is 100ms, and each retry doubles the delay: 100ms, 200ms, 400ms, etc.
pub struct RetryNode {
  /// Base node functionality.
  pub(crate) base: BaseNode,
  /// Current configuration state.
  current_config: Arc<Mutex<Option<RetryConfig>>>,
  /// Base delay in milliseconds for exponential backoff.
  base_delay_ms: u64,
}

impl RetryNode {
  /// Creates a new RetryNode with the given name and base delay.
  ///
  /// # Arguments
  ///
  /// * `name` - The name of the node.
  /// * `base_delay_ms` - Base delay in milliseconds for exponential backoff (default: 100ms).
  ///
  /// # Returns
  ///
  /// A new `RetryNode` instance.
  ///
  /// # Example
  ///
  /// ```rust,no_run
  /// use streamweave::nodes::advanced::RetryNode;
  ///
  /// let node = RetryNode::new("retry".to_string(), 100);
  /// // Creates ports: configuration, in, max_retries → out, error
  /// ```
  pub fn new(name: String, base_delay_ms: u64) -> Self {
    Self {
      base: BaseNode::new(
        name,
        vec![
          "configuration".to_string(),
          "in".to_string(),
          "max_retries".to_string(),
        ],
        vec!["out".to_string(), "error".to_string()],
      ),
      current_config: Arc::new(Mutex::new(None)),
      base_delay_ms,
    }
  }

  /// Returns whether the node has a configuration set.
  pub fn has_config(&self) -> bool {
    self
      .current_config
      .try_lock()
      .map(|g| g.is_some())
      .unwrap_or(false)
  }
}

#[async_trait]
#[allow(clippy::type_complexity)]
impl Node for RetryNode {
  fn name(&self) -> &str {
    self.base.name()
  }

  fn set_name(&mut self, name: &str) {
    self.base.set_name(name);
  }

  fn input_port_names(&self) -> &[String] {
    self.base.input_port_names()
  }

  fn output_port_names(&self) -> &[String] {
    self.base.output_port_names()
  }

  fn has_input_port(&self, name: &str) -> bool {
    self.base.has_input_port(name)
  }

  fn has_output_port(&self, name: &str) -> bool {
    self.base.has_output_port(name)
  }

  fn execute(
    &self,
    mut inputs: InputStreams,
  ) -> Pin<
    Box<dyn std::future::Future<Output = Result<OutputStreams, NodeExecutionError>> + Send + '_>,
  > {
    let config_state = Arc::clone(&self.current_config);
    let base_delay_ms = self.base_delay_ms;

    Box::pin(async move {
      // Extract input streams
      let config_stream = inputs.remove("configuration");
      let in_stream = inputs.remove("in").ok_or("Missing 'in' input")?;
      let max_retries_stream = inputs
        .remove("max_retries")
        .ok_or("Missing 'max_retries' input")?;

      // Tag streams to distinguish inputs
      let in_stream = in_stream.map(|item| (InputPort::In, item));
      let max_retries_stream = max_retries_stream.map(|item| (InputPort::MaxRetries, item));

      // Merge streams - include config stream if present
      let mut streams: Vec<
        Pin<Box<dyn futures::Stream<Item = (InputPort, Arc<dyn Any + Send + Sync>)> + Send>>,
      > = vec![
        Box::pin(in_stream)
          as Pin<Box<dyn futures::Stream<Item = (InputPort, Arc<dyn Any + Send + Sync>)> + Send>>,
        Box::pin(max_retries_stream)
          as Pin<Box<dyn futures::Stream<Item = (InputPort, Arc<dyn Any + Send + Sync>)> + Send>>,
      ];

      if let Some(config_stream) = config_stream {
        let config_stream = config_stream.map(|item| (InputPort::Config, item));
        streams.push(Box::pin(config_stream)
          as Pin<
            Box<dyn futures::Stream<Item = (InputPort, Arc<dyn Any + Send + Sync>)> + Send>,
          >);
      }

      let merged_stream = stream::select_all(streams);

      // Create output channels
      let (out_tx, out_rx) = tokio::sync::mpsc::channel(10);
      let (error_tx, error_rx) = tokio::sync::mpsc::channel(10);

      // Process the merged stream
      let out_tx_clone = out_tx.clone();
      let error_tx_clone = error_tx.clone();

      tokio::spawn(async move {
        let mut merged_stream = merged_stream;
        let mut current_config: Option<RetryConfig> = None;
        let mut max_retries: Option<usize> = None;
        let mut pending_items: Vec<Arc<dyn Any + Send + Sync>> = Vec::new();

        while let Some((port, item)) = merged_stream.next().await {
          match port {
            InputPort::Config => {
              // Update configuration
              if let Ok(arc_arc_fn) = item.clone().downcast::<Arc<Arc<dyn RetryFunction>>>() {
                current_config = Some(Arc::clone(&**arc_arc_fn));
                let mut config = config_state.lock().await;
                *config = Some(Arc::clone(&**arc_arc_fn));
              } else if let Ok(arc_function) = item.clone().downcast::<Arc<dyn RetryFunction>>() {
                current_config = Some(Arc::clone(&*arc_function));
                let mut config = config_state.lock().await;
                *config = Some(Arc::clone(&*arc_function));
              }
            }
            InputPort::MaxRetries => {
              // Update max_retries
              match get_usize(&item) {
                Ok(count) => {
                  max_retries = Some(count);
                  // Process any pending items now that we have max_retries
                  if let Some(current_max_retries) = max_retries
                    && let Some(ref retry_fn) = current_config
                  {
                    for pending_item in pending_items.drain(..) {
                      process_item_with_retry(
                        &pending_item,
                        &Some(retry_fn.clone()),
                        current_max_retries,
                        base_delay_ms,
                        &out_tx_clone,
                        &error_tx_clone,
                      )
                      .await;
                    }
                  }
                }
                Err(e) => {
                  let error_arc = Arc::new(e) as Arc<dyn Any + Send + Sync>;
                  let _ = error_tx_clone.send(error_arc).await;
                }
              }
            }
            InputPort::In => {
              // Process data item
              if let Some(current_max_retries) = max_retries {
                if let Some(ref retry_fn) = current_config {
                  process_item_with_retry(
                    &item,
                    &Some(retry_fn.clone()),
                    current_max_retries,
                    base_delay_ms,
                    &out_tx_clone,
                    &error_tx_clone,
                  )
                  .await;
                } else {
                  // No config set yet - buffer item
                  pending_items.push(item);
                }
              } else {
                // No max_retries set yet - buffer item
                pending_items.push(item);
              }
            }
          }
        }
      });

      // Convert channels to streams
      let mut outputs = HashMap::new();
      outputs.insert(
        "out".to_string(),
        Box::pin(ReceiverStream::new(out_rx))
          as Pin<Box<dyn tokio_stream::Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
      );
      outputs.insert(
        "error".to_string(),
        Box::pin(ReceiverStream::new(error_rx))
          as Pin<Box<dyn tokio_stream::Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
      );

      Ok(outputs)
    })
  }
}

/// Helper function to process an item with retry logic and exponential backoff.
async fn process_item_with_retry(
  item: &Arc<dyn Any + Send + Sync>,
  config: &Option<RetryConfig>,
  max_retries: usize,
  base_delay_ms: u64,
  out_tx: &tokio::sync::mpsc::Sender<Arc<dyn Any + Send + Sync>>,
  error_tx: &tokio::sync::mpsc::Sender<Arc<dyn Any + Send + Sync>>,
) {
  if let Some(retry_fn) = config {
    #[allow(unused_assignments)]
    let mut last_error: Option<Arc<dyn Any + Send + Sync>> = None;

    // Try initial attempt
    match retry_fn.apply(item.clone()).await {
      Ok(result) => {
        let _ = out_tx.send(result).await;
        return;
      }
      Err(error) => {
        last_error = Some(error);
      }
    }

    // Retry with exponential backoff
    for retry_count in 0..max_retries {
      // Calculate exponential backoff delay: base_delay * (2 ^ retry_count)
      let delay_ms = base_delay_ms * (1 << retry_count);
      let delay = Duration::from_millis(delay_ms);

      // Wait before retrying
      sleep(delay).await;

      // Retry the operation
      match retry_fn.apply(item.clone()).await {
        Ok(result) => {
          let _ = out_tx.send(result).await;
          return;
        }
        Err(error) => {
          last_error = Some(error);
        }
      }
    }

    // All retries exhausted - send error
    if let Some(error) = last_error {
      let _ = error_tx.send(error).await;
    } else {
      let error_msg = Arc::new("All retries exhausted".to_string()) as Arc<dyn Any + Send + Sync>;
      let _ = error_tx.send(error_msg).await;
    }
  } else {
    // No config set - send error
    let error_msg =
      Arc::new("No retry function configured".to_string()) as Arc<dyn Any + Send + Sync>;
    let _ = error_tx.send(error_msg).await;
  }
}