waitup 1.1.1

Wait for TCP ports and HTTP endpoints to be available. Essential for Docker, K8s, and CI/CD pipelines to ensure services are ready before proceeding.
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
//! Async traits for extensible connection strategies and target checking.
//!
//! This module provides async traits that allow for custom implementations
//! of connection checking, retry strategies, and concurrency patterns.

use async_trait::async_trait;
use core::time::Duration;

use crate::types::{Target, TargetResult, WaitConfig, WaitResult};
use crate::{Result, WaitForError};

/// Async trait for checking target availability
///
/// This allows for custom implementations of target checking logic,
/// including mocking for testing, custom protocols, or specialized health checks.
#[async_trait]
pub trait AsyncTargetChecker: Send + Sync {
    /// Check if a target is available
    async fn check_target(&self, target: &Target, config: &WaitConfig) -> Result<()>;

    /// Get a human-readable name for this checker
    fn name(&self) -> &'static str;
}

/// Async trait for retry strategies
///
/// This allows for custom retry logic, exponential backoff algorithms,
/// jitter, and other advanced retry patterns.
#[async_trait]
pub trait AsyncRetryStrategy: Send + Sync {
    /// Calculate the next retry interval
    fn next_interval(&mut self, attempt: u32, last_interval: Duration) -> Duration;

    /// Check if we should continue retrying
    fn should_retry(
        &self,
        attempt: u32,
        elapsed: Duration,
        max_retries: Option<u32>,
        timeout: Duration,
    ) -> bool;

    /// Reset strategy state for a new target
    fn reset(&mut self);

    /// Get strategy name for debugging
    fn name(&self) -> &'static str;
}

/// Async trait for connection strategies
///
/// This allows for custom concurrency patterns beyond the built-in "all" and "any" strategies.
#[async_trait]
pub trait AsyncConnectionStrategy: Send + Sync {
    /// Execute the connection strategy for multiple targets
    async fn execute(
        &self,
        targets: &[Target],
        checker: &dyn AsyncTargetChecker,
        config: &WaitConfig,
    ) -> Result<WaitResult>;

    /// Get strategy name
    fn name(&self) -> &'static str;

    /// Execute strategy with streaming results for real-time progress
    ///
    /// Note: This is a simpler approach that returns a future of results.
    /// For true streaming behavior, use the execute method with custom logic.
    #[inline]
    async fn execute_streaming(
        &self,
        targets: &[Target],
        checker: &dyn AsyncTargetChecker,
        config: &WaitConfig,
    ) -> Result<Vec<TargetResult>> {
        // Default implementation that just executes normally
        match self.execute(targets, checker, config).await {
            Ok(wait_result) => Ok(wait_result.target_results),
            Err(e) => Err(e),
        }
    }
}

/// Default implementation of `AsyncTargetChecker` using the existing connection logic
pub struct DefaultTargetChecker;

#[async_trait]
impl AsyncTargetChecker for DefaultTargetChecker {
    #[inline]
    async fn check_target(&self, target: &Target, config: &WaitConfig) -> Result<()> {
        crate::connection::try_connect_target(target, config).await
    }

    #[inline]
    fn name(&self) -> &'static str {
        "default"
    }
}

/// Exponential backoff retry strategy
pub struct ExponentialBackoffStrategy {
    multiplier: f64,
    max_interval: Duration,
}

impl ExponentialBackoffStrategy {
    #[must_use]
    #[inline]
    /// Creates a new exponential backoff strategy
    pub const fn new(multiplier: f64, max_interval: Duration) -> Self {
        Self {
            multiplier,
            max_interval,
        }
    }
}

impl Default for ExponentialBackoffStrategy {
    #[inline]
    fn default() -> Self {
        Self::new(1.5, Duration::from_secs(30))
    }
}

#[async_trait]
impl AsyncRetryStrategy for ExponentialBackoffStrategy {
    #[inline]
    fn next_interval(&mut self, _attempt: u32, last_interval: Duration) -> Duration {
        // Handle multiplication carefully to avoid precision loss and overflow
        let last_millis = last_interval.as_millis().min(u128::MAX / 2);

        // Convert to u64 first, then to f64 to minimize precision loss
        let last_millis_u64 = u64::try_from(last_millis).unwrap_or(u64::MAX);
        #[expect(
            clippy::cast_precision_loss,
            reason = "u64 to f64 conversion necessary for exponential backoff calculation"
        )]
        let multiplied = (last_millis_u64 as f64 * self.multiplier).min(u64::MAX as f64);

        if multiplied < 0.0 || !multiplied.is_finite() {
            return Duration::from_millis(0);
        }

        #[expect(
            clippy::cast_possible_truncation,
            clippy::cast_sign_loss,
            reason = "f64 to u64 conversion safe after finite check and bounds validation"
        )]
        let next = Duration::from_millis(multiplied as u64);
        if next > self.max_interval {
            self.max_interval
        } else {
            next
        }
    }

    #[inline]
    fn should_retry(
        &self,
        attempt: u32,
        elapsed: Duration,
        max_retries: Option<u32>,
        timeout: Duration,
    ) -> bool {
        // Check timeout
        if elapsed >= timeout {
            return false;
        }

        // Check max retries
        if let Some(max) = max_retries {
            if attempt >= max {
                return false;
            }
        }

        true
    }

    #[inline]
    fn reset(&mut self) {
        // Nothing to reset for exponential backoff
    }

    #[inline]
    fn name(&self) -> &'static str {
        "exponential_backoff"
    }
}

/// Linear backoff retry strategy
pub struct LinearBackoffStrategy {
    increment: Duration,
    max_interval: Duration,
}

impl LinearBackoffStrategy {
    #[must_use]
    #[inline]
    /// Creates a new linear backoff strategy
    pub const fn new(increment: Duration, max_interval: Duration) -> Self {
        Self {
            increment,
            max_interval,
        }
    }
}

impl Default for LinearBackoffStrategy {
    #[inline]
    fn default() -> Self {
        Self::new(Duration::from_secs(1), Duration::from_secs(30))
    }
}

#[async_trait]
impl AsyncRetryStrategy for LinearBackoffStrategy {
    #[inline]
    fn next_interval(&mut self, _attempt: u32, last_interval: Duration) -> Duration {
        let next = last_interval + self.increment;
        if next > self.max_interval {
            self.max_interval
        } else {
            next
        }
    }

    #[inline]
    fn should_retry(
        &self,
        attempt: u32,
        elapsed: Duration,
        max_retries: Option<u32>,
        timeout: Duration,
    ) -> bool {
        if elapsed >= timeout {
            return false;
        }

        if let Some(max) = max_retries {
            if attempt >= max {
                return false;
            }
        }

        true
    }

    #[inline]
    fn reset(&mut self) {
        // Nothing to reset
    }

    #[inline]
    fn name(&self) -> &'static str {
        "linear_backoff"
    }
}

/// Strategy that waits for all targets to be ready
pub struct WaitForAllStrategy;

#[async_trait]
impl AsyncConnectionStrategy for WaitForAllStrategy {
    #[inline]
    async fn execute(
        &self,
        targets: &[Target],
        checker: &dyn AsyncTargetChecker,
        config: &WaitConfig,
    ) -> Result<WaitResult> {
        use futures::future::join_all;
        use tokio::time::Instant;

        let start = Instant::now();

        if targets.is_empty() {
            return Ok(WaitResult {
                success: true,
                elapsed: start.elapsed(),
                attempts: 0,
                target_results: vec![],
            });
        }

        // Create futures for each target using the async target checker
        let futures: Vec<_> = targets
            .iter()
            .map(|target| wait_for_single_target_with_checker(target, checker, config))
            .collect();

        let results = join_all(futures).await;
        let mut target_results = Vec::new();
        let mut all_successful = true;
        let mut total_attempts = 0;

        for result in results {
            match result {
                Ok(target_result) => {
                    if !target_result.success {
                        all_successful = false;
                    }
                    total_attempts += target_result.attempts;
                    target_results.push(target_result);
                }
                Err(e) => {
                    return Err(e);
                }
            }
        }

        if !all_successful {
            let failed_targets: Vec<_> = target_results
                .iter()
                .filter(|r| !r.success)
                .map(|r| r.target.display())
                .collect();
            return Err(WaitForError::Timeout {
                targets: std::borrow::Cow::Owned(failed_targets.join(", ")),
            });
        }

        Ok(WaitResult {
            success: all_successful,
            elapsed: start.elapsed(),
            attempts: total_attempts,
            target_results,
        })
    }

    #[inline]
    fn name(&self) -> &'static str {
        "wait_for_all"
    }
}

/// Strategy that waits for any target to be ready
pub struct WaitForAnyStrategy;

#[async_trait]
impl AsyncConnectionStrategy for WaitForAnyStrategy {
    #[inline]
    async fn execute(
        &self,
        targets: &[Target],
        checker: &dyn AsyncTargetChecker,
        config: &WaitConfig,
    ) -> Result<WaitResult> {
        use futures::future::select_ok;
        use tokio::time::Instant;

        let start = Instant::now();

        if targets.is_empty() {
            return Ok(WaitResult {
                success: true,
                elapsed: start.elapsed(),
                attempts: 0,
                target_results: vec![],
            });
        }

        let futures: Vec<_> = targets
            .iter()
            .map(|target| Box::pin(wait_for_single_target_with_checker(target, checker, config)))
            .collect();

        match select_ok(futures).await {
            Ok((result, _)) => Ok(WaitResult {
                success: result.success,
                elapsed: start.elapsed(),
                attempts: result.attempts,
                target_results: vec![result],
            }),
            Err(e) => Err(e),
        }
    }

    #[inline]
    fn name(&self) -> &'static str {
        "wait_for_any"
    }
}

/// Concurrent strategy that provides real-time feedback as targets become ready
pub struct ConcurrentProgressStrategy {
    concurrency_limit: usize,
}

impl ConcurrentProgressStrategy {
    #[must_use]
    #[inline]
    /// Creates a new concurrent progress strategy
    pub const fn new(concurrency_limit: usize) -> Self {
        Self { concurrency_limit }
    }
}

impl Default for ConcurrentProgressStrategy {
    #[inline]
    fn default() -> Self {
        Self::new(10) // Default to 10 concurrent checks
    }
}

#[async_trait]
impl AsyncConnectionStrategy for ConcurrentProgressStrategy {
    #[inline]
    async fn execute(
        &self,
        targets: &[Target],
        checker: &dyn AsyncTargetChecker,
        config: &WaitConfig,
    ) -> Result<WaitResult> {
        use futures::stream::{FuturesUnordered, StreamExt};
        use tokio::time::Instant;

        let start = Instant::now();

        if targets.is_empty() {
            return Ok(WaitResult {
                success: true,
                elapsed: start.elapsed(),
                attempts: 0,
                target_results: vec![],
            });
        }

        let mut futures = FuturesUnordered::new();
        let mut target_results = Vec::new();
        let mut total_attempts = 0;

        // Add futures with concurrency limit
        for chunk in targets.chunks(self.concurrency_limit) {
            for target in chunk {
                futures.push(wait_for_single_target_with_checker(target, checker, config));
            }

            // Process this batch
            while let Some(result) = futures.next().await {
                match result {
                    Ok(target_result) => {
                        total_attempts += target_result.attempts;
                        target_results.push(target_result);
                    }
                    Err(e) => return Err(e),
                }
            }
        }

        // Check if all were successful
        let all_successful = target_results.iter().all(|r| r.success);

        if !all_successful {
            let failed_targets: Vec<_> = target_results
                .iter()
                .filter(|r| !r.success)
                .map(|r| r.target.display())
                .collect();
            return Err(WaitForError::Timeout {
                targets: std::borrow::Cow::Owned(failed_targets.join(", ")),
            });
        }

        Ok(WaitResult {
            success: all_successful,
            elapsed: start.elapsed(),
            attempts: total_attempts,
            target_results,
        })
    }

    #[inline]
    fn name(&self) -> &'static str {
        "concurrent_progress"
    }

    /// Streaming implementation that yields results as they complete
    #[inline]
    async fn execute_streaming(
        &self,
        targets: &[Target],
        checker: &dyn AsyncTargetChecker,
        config: &WaitConfig,
    ) -> Result<Vec<TargetResult>> {
        // For this strategy, just use the normal execute and return all results
        // In a real implementation, this could provide progress callbacks
        match self.execute(targets, checker, config).await {
            Ok(wait_result) => Ok(wait_result.target_results),
            Err(e) => Err(e),
        }
    }
}

/// Helper function to wait for a single target using a custom checker
async fn wait_for_single_target_with_checker(
    target: &Target,
    checker: &dyn AsyncTargetChecker,
    config: &WaitConfig,
) -> Result<TargetResult> {
    use tokio::time::{Instant, sleep};

    let start = Instant::now();
    let deadline = start + config.timeout;
    let mut current_interval = config.initial_interval;
    let mut attempt = 0;
    let mut retry_strategy = ExponentialBackoffStrategy::default();

    loop {
        // Check for cancellation
        if let Some(token) = &config.cancellation_token {
            if token.is_cancelled() {
                return Err(WaitForError::Cancelled);
            }
        }

        // Check if we've exceeded the deadline
        let now = Instant::now();
        if now >= deadline {
            return Ok(TargetResult {
                target: target.clone(),
                success: false,
                elapsed: now.duration_since(start),
                attempts: attempt,
                error: Some("Overall timeout exceeded".to_string()),
            });
        }

        attempt += 1;

        // Try connection with remaining time constraint
        let remaining_time = deadline.duration_since(now);
        let connection_timeout = config.connection_timeout.min(remaining_time);

        let mut connection_config = config.clone();
        connection_config.connection_timeout = connection_timeout;

        match checker.check_target(target, &connection_config).await {
            Ok(()) => {
                return Ok(TargetResult {
                    target: target.clone(),
                    success: true,
                    elapsed: now.duration_since(start),
                    attempts: attempt,
                    error: None,
                });
            }
            Err(_e) => {
                // Check if we should retry
                if !retry_strategy.should_retry(
                    attempt,
                    now.duration_since(start),
                    config.max_retries,
                    config.timeout,
                ) {
                    return Ok(TargetResult {
                        target: target.clone(),
                        success: false,
                        elapsed: now.duration_since(start),
                        attempts: attempt,
                        error: Some(format!("Max retries ({:?}) exceeded", config.max_retries)),
                    });
                }

                // Calculate sleep duration
                current_interval = retry_strategy.next_interval(attempt, current_interval);
                let sleep_duration = current_interval.min(deadline.duration_since(Instant::now()));

                // Sleep with cancellation support
                if let Some(token) = &config.cancellation_token {
                    tokio::select! {
                        () = sleep(sleep_duration) => {},
                        () = token.cancelled() => {
                            return Err(WaitForError::Cancelled);
                        }
                    }
                } else {
                    sleep(sleep_duration).await;
                }
            }
        }
    }
}