feroxbuster 2.13.1

A fast, simple, recursive content discovery tool.
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
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::sync::{Semaphore, SemaphorePermit};

/// A wrapper around Tokio's [`Semaphore`] that supports dynamic capacity reduction.
///
/// Unlike the standard Tokio semaphore, this implementation allows for reduction of the
/// effective capacity even when permits are already acquired and other tasks are waiting.
/// This is particularly useful for rate limiting scenarios where we need to dynamically
/// adjust the concurrency level based on runtime conditions.
///
/// # Key Features
///
/// - **Dynamic Capacity Reduction**: Can reduce capacity even when permits are in use
/// - **Queued Waiter Preservation**: Existing waiters remain in queue during capacity changes
/// - **Thread-Safe**: All operations are atomic and safe for concurrent use
/// - **Drop Safety**: Automatically manages capacity when permits are released
///
/// # Example
///
/// ```rust,no_run
/// use feroxbuster::sync::DynamicSemaphore;
///
/// #[tokio::main]
/// async fn main() {
///     let semaphore = DynamicSemaphore::new(2);
///     
///     // Acquire permits
///     let _permit1 = semaphore.acquire().await.unwrap();
///     let _permit2 = semaphore.acquire().await.unwrap();
///     
///     // Reduce capacity from 2 to 1 (takes effect when permits are released)
///     semaphore.reduce_capacity(1);
///     
///     // When permits are dropped, only 1 permit will be available instead of 2
/// }
/// ```

#[derive(Debug)]
pub struct DynamicSemaphore {
    /// The underlying Tokio semaphore that handles the actual permit management
    inner: Arc<Semaphore>,

    /// The current maximum capacity for this semaphore
    ///
    /// This value represents the desired maximum number of permits that should be
    /// available. When permits are released, the semaphore ensures that the total
    /// available permits never exceed this capacity.
    max_capacity: AtomicUsize,

    /// Counter for permits currently in use
    ///
    /// This is incremented when permits are acquired and decremented when released.
    /// We use this to track how many permits are actually in use vs the virtual capacity.
    permits_in_use: AtomicUsize,
}

/// A permit acquired from a [`DynamicSemaphore`].
///
/// This permit automatically manages the dynamic capacity when dropped. If releasing
/// the permit would cause the semaphore to exceed its current capacity limit, the
/// permit is "forgotten" instead of being returned to the available pool.
///
/// The permit provides the same guarantees as Tokio's [`SemaphorePermit`] but with
/// additional capacity management logic.
#[derive(Debug)]
pub struct DynamicSemaphorePermit<'a> {
    /// The underlying Tokio semaphore permit
    ///
    /// This is wrapped in an Option to allow for controlled dropping during
    /// capacity management in the Drop implementation.
    permit: Option<SemaphorePermit<'a>>,

    /// Reference to the parent semaphore for capacity checking
    semaphore: &'a DynamicSemaphore,
}

impl DynamicSemaphore {
    /// Creates a new [`DynamicSemaphore`] with the specified number of permits.
    ///
    /// # Arguments
    ///
    /// * `permits` - The initial number of permits available in the semaphore
    ///
    /// # Panics
    ///
    /// Panics if `permits` exceeds the maximum number of permits supported by
    /// the underlying Tokio semaphore implementation.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    ///
    /// let semaphore = DynamicSemaphore::new(10);
    /// assert_eq!(semaphore.current_capacity(), 10);
    /// ```
    pub fn new(permits: usize) -> Self {
        Self {
            inner: Arc::new(Semaphore::new(permits)),
            max_capacity: AtomicUsize::new(permits),
            permits_in_use: AtomicUsize::new(0),
        }
    }

    /// Acquires a permit from the semaphore.
    ///
    /// This method will wait until a permit becomes available. The returned permit
    /// will automatically manage capacity constraints when dropped.
    ///
    /// # Returns
    ///
    /// A [`Result`] containing a [`DynamicSemaphorePermit`] on success, or an
    /// [`tokio::sync::AcquireError`] if the semaphore has been closed.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let semaphore = DynamicSemaphore::new(1);
    ///     let permit = semaphore.acquire().await.unwrap();
    ///     // permit is automatically released when dropped
    /// }
    /// ```
    pub async fn acquire(&self) -> Result<DynamicSemaphorePermit<'_>, tokio::sync::AcquireError> {
        loop {
            // Check if we're already at or over capacity before acquiring
            let current_in_use = self.permits_in_use.load(Ordering::Acquire);
            let current_capacity = self.current_capacity();

            if current_in_use >= current_capacity {
                // We're at or over capacity, wait for a permit to be released
                let _temp_permit = self.inner.acquire().await?;
                // Drop the permit immediately and try again - this ensures we wait
                // for permits to become available but don't actually consume them
                // if we're over capacity
                drop(_temp_permit);
                continue;
            }

            // Try to acquire a permit
            let permit = self.inner.acquire().await?;

            // Atomically increment in_use and check if we're still within capacity
            let new_in_use = self.permits_in_use.fetch_add(1, Ordering::AcqRel) + 1;

            if new_in_use <= current_capacity {
                // We're within capacity, return the permit
                return Ok(DynamicSemaphorePermit {
                    permit: Some(permit),
                    semaphore: self,
                });
            } else {
                // We exceeded capacity between the check and increment, backtrack
                self.permits_in_use.fetch_sub(1, Ordering::AcqRel);
                drop(permit);
                // implicit try again
            }
        }
    }

    /// Attempts to acquire a permit without waiting.
    ///
    /// If a permit is immediately available, it is returned. Otherwise, this method
    /// returns an error indicating why the permit could not be acquired.
    ///
    /// # Returns
    ///
    /// A [`Result`] containing a [`DynamicSemaphorePermit`] if successful, or a
    /// [`tokio::sync::TryAcquireError`] if no permit is available or the semaphore is closed.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    /// use tokio::sync::TryAcquireError;
    ///
    /// let semaphore = DynamicSemaphore::new(1);
    /// match semaphore.try_acquire() {
    ///     Ok(permit) => println!("Got permit"),
    ///     Err(TryAcquireError::NoPermits) => println!("No permits available"),
    ///     Err(TryAcquireError::Closed) => println!("Semaphore closed"),
    /// };
    /// ```
    pub fn try_acquire(&self) -> Result<DynamicSemaphorePermit<'_>, tokio::sync::TryAcquireError> {
        // Check if we're already at or over capacity
        let current_in_use = self.permits_in_use.load(Ordering::Acquire);
        let current_capacity = self.current_capacity();

        if current_in_use >= current_capacity {
            // We're at or over capacity, cannot acquire
            return Err(tokio::sync::TryAcquireError::NoPermits);
        }

        // Try to acquire a permit from the underlying semaphore
        let permit = self.inner.try_acquire()?;

        // Atomically increment in_use and check if we're still within capacity
        let new_in_use = self.permits_in_use.fetch_add(1, Ordering::AcqRel) + 1;
        if new_in_use <= current_capacity {
            // We're within capacity, return the permit
            Ok(DynamicSemaphorePermit {
                permit: Some(permit),
                semaphore: self,
            })
        } else {
            // We exceeded capacity between the check and increment, backtrack
            self.permits_in_use.fetch_sub(1, Ordering::AcqRel);
            drop(permit);
            Err(tokio::sync::TryAcquireError::NoPermits)
        }
    }

    /// Reduces the maximum capacity of the semaphore.
    ///
    /// This method sets a new maximum capacity for the semaphore. The change takes
    /// effect immediately for new permit acquisitions. If there are currently more
    /// permits in use than the new capacity allows, the reduction will take effect
    /// gradually as permits are released.
    ///
    /// # Arguments
    ///
    /// * `new_capacity` - The new maximum number of permits that should be available
    ///
    /// # Returns
    ///
    /// The previous capacity value before the change.
    ///
    /// # Notes
    ///
    /// - This operation is atomic and thread-safe
    /// - Existing permit holders are not affected until they release their permits
    /// - Queued waiters remain in the queue and will eventually be served
    /// - If available permits exceed the new capacity, excess permits are immediately forgotten
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let semaphore = DynamicSemaphore::new(5);
    ///     
    ///     // Reduce capacity from 5 to 2
    ///     let old_capacity = semaphore.reduce_capacity(2);
    ///     assert_eq!(old_capacity, 5);
    ///     assert_eq!(semaphore.current_capacity(), 2);
    /// }
    /// ```
    pub fn reduce_capacity(&self, new_capacity: usize) -> usize {
        let old_capacity = self.max_capacity.swap(new_capacity, Ordering::AcqRel);

        // If we're reducing capacity and there are available permits that exceed
        // the new capacity, we should forget the excess permits immediately
        if new_capacity < old_capacity {
            let available = self.inner.available_permits();
            let to_forget = available.saturating_sub(new_capacity);

            if to_forget > 0 {
                self.inner.forget_permits(to_forget);
            }
        }

        old_capacity
    }

    /// Increases the maximum capacity of the semaphore.
    ///
    /// This method sets a new maximum capacity that is higher than the current one.
    /// Additional permits are immediately added to the semaphore up to the new capacity.
    ///
    /// # Arguments
    ///
    /// * `new_capacity` - The new maximum number of permits that should be available
    ///
    /// # Returns
    ///
    /// The previous capacity value before the change.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity would cause the semaphore to exceed its maximum
    /// supported permit count.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let semaphore = DynamicSemaphore::new(2);
    ///     
    ///     // Increase capacity from 2 to 5
    ///     let old_capacity = semaphore.increase_capacity(5);
    ///     assert_eq!(old_capacity, 2);
    ///     assert_eq!(semaphore.current_capacity(), 5);
    /// }
    /// ```
    pub fn increase_capacity(&self, new_capacity: usize) -> usize {
        let old_capacity = self.max_capacity.swap(new_capacity, Ordering::AcqRel);

        // If we're increasing capacity, add the additional permits
        if new_capacity > old_capacity {
            let to_add = new_capacity - old_capacity;
            self.inner.add_permits(to_add);
        }

        old_capacity
    }

    /// Returns the current maximum capacity of the semaphore.
    ///
    /// This represents the maximum number of permits that can be available at any
    /// given time, which may be different from the number of currently available permits.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    ///
    /// let semaphore = DynamicSemaphore::new(10);
    /// assert_eq!(semaphore.current_capacity(), 10);
    /// ```
    pub fn current_capacity(&self) -> usize {
        self.max_capacity.load(Ordering::Acquire)
    }

    /// Returns the number of permits currently available for immediate acquisition.
    ///
    /// This value represents permits that can be acquired without waiting. Note that
    /// this number may be less than the capacity if permits are currently in use.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let semaphore = DynamicSemaphore::new(3);
    ///     assert_eq!(semaphore.available_permits(), 3);
    ///     
    ///     let _permit = semaphore.acquire().await.unwrap();
    ///     assert_eq!(semaphore.available_permits(), 2);
    /// }
    /// ```
    pub fn available_permits(&self) -> usize {
        self.inner.available_permits()
    }

    /// Closes the semaphore, preventing new permits from being acquired.
    ///
    /// This will wake up all tasks currently waiting to acquire a permit, causing
    /// them to receive an [`tokio::sync::AcquireError`]. Existing permits remain
    /// valid until dropped.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let semaphore = DynamicSemaphore::new(1);
    ///     semaphore.close();
    ///     
    ///     // This will return an error
    ///     assert!(semaphore.acquire().await.is_err());
    /// }
    /// ```
    pub fn close(&self) {
        self.inner.close();
    }

    /// Returns whether the semaphore has been closed.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    ///
    /// let semaphore = DynamicSemaphore::new(1);
    /// assert!(!semaphore.is_closed());
    ///
    /// semaphore.close();
    /// assert!(semaphore.is_closed());
    /// ```
    pub fn is_closed(&self) -> bool {
        self.inner.is_closed()
    }

    /// Returns the current number of permits in use (for debugging).
    ///
    /// This is primarily useful for debugging and testing to understand
    /// the internal state of the semaphore.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use feroxbuster::sync::DynamicSemaphore;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///     let semaphore = DynamicSemaphore::new(3);
    ///     assert_eq!(semaphore.permits_in_use(), 0);
    ///
    ///     let _permit = semaphore.acquire().await.unwrap();
    ///     assert_eq!(semaphore.permits_in_use(), 1);
    /// }
    /// ```
    pub fn permits_in_use(&self) -> usize {
        self.permits_in_use.load(Ordering::Acquire)
    }
}

impl<'a> Drop for DynamicSemaphorePermit<'a> {
    /// Handles the automatic release of the permit with capacity management.
    ///
    /// This implementation uses an approach designed to avoid race conditions:
    ///
    /// We make the decision atomically BEFORE releasing the permit by checking if we're
    /// currently over capacity. If we are, we "forget" the permit instead of releasing it.
    /// If we're not over capacity, we release it normally.
    ///
    /// This works because:
    /// 1. We decrement permits_in_use first (atomically)
    /// 2. We check if permits_in_use + available_permits > capacity  
    /// 3. If so, we're over capacity and should forget this permit
    /// 4. If not, we can safely release it
    ///
    /// The key insight is that permits_in_use represents permits about to be released,
    /// so permits_in_use + available_permits tells us what the total would be after release.
    fn drop(&mut self) {
        if let Some(permit) = self.permit.take() {
            // First, atomically decrement our usage counter
            self.semaphore.permits_in_use.fetch_sub(1, Ordering::AcqRel);

            // Check current state
            let current_capacity = self.semaphore.current_capacity();
            let current_available = self.semaphore.available_permits();

            // Calculate what the total would be if we released this permit
            let total_after_release = current_available + 1;

            // If releasing would exceed capacity, forget the permit instead
            if total_after_release > current_capacity {
                // Forget the permit - it never gets added to available permits
                permit.forget();
            } else {
                // Safe to release normally
                drop(permit);
            }
        }
    }
}

// Ensure the permit can be safely sent between threads
unsafe impl<'a> Send for DynamicSemaphorePermit<'a> {}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::time::Duration;
    use tokio::time::sleep;

    #[tokio::test]
    async fn test_basic_acquire_release() {
        let semaphore = DynamicSemaphore::new(2);

        assert_eq!(semaphore.available_permits(), 2);
        assert_eq!(semaphore.current_capacity(), 2);
        assert_eq!(semaphore.permits_in_use(), 0);

        let permit1 = semaphore.acquire().await.unwrap();
        assert_eq!(semaphore.available_permits(), 1);
        assert_eq!(semaphore.permits_in_use(), 1);

        let permit2 = semaphore.acquire().await.unwrap();
        assert_eq!(semaphore.available_permits(), 0);
        assert_eq!(semaphore.permits_in_use(), 2);

        drop(permit1);
        assert_eq!(semaphore.available_permits(), 1);
        assert_eq!(semaphore.permits_in_use(), 1);

        drop(permit2);
        assert_eq!(semaphore.available_permits(), 2);
        assert_eq!(semaphore.permits_in_use(), 0);
    }

    #[tokio::test]
    async fn test_capacity_reduction() {
        let semaphore = DynamicSemaphore::new(3);

        // Acquire all permits
        let permit1 = semaphore.acquire().await.unwrap();
        let permit2 = semaphore.acquire().await.unwrap();
        let permit3 = semaphore.acquire().await.unwrap();

        assert_eq!(semaphore.available_permits(), 0);
        assert_eq!(semaphore.permits_in_use(), 3);

        // Reduce capacity to 2
        let old_capacity = semaphore.reduce_capacity(2);
        assert_eq!(old_capacity, 3);
        assert_eq!(semaphore.current_capacity(), 2);

        // Drop one permit - should be returned since we're within the new capacity (0 + 1 <= 2)
        drop(permit1);
        assert_eq!(semaphore.available_permits(), 1);
        assert_eq!(semaphore.permits_in_use(), 2);

        // Drop another permit - should be returned since we're still within capacity (1 + 1 <= 2)
        drop(permit2);
        assert_eq!(semaphore.available_permits(), 2);
        assert_eq!(semaphore.permits_in_use(), 1);

        // Drop the last permit - this would exceed capacity (2 + 1 > 2), so should be forgotten
        drop(permit3);
        assert_eq!(semaphore.available_permits(), 2); // Still 2, excess was forgotten
        assert_eq!(semaphore.permits_in_use(), 0);
    }

    #[tokio::test]
    async fn test_capacity_increase() {
        let semaphore = DynamicSemaphore::new(2);

        assert_eq!(semaphore.available_permits(), 2);

        // Increase capacity
        let old_capacity = semaphore.increase_capacity(5);
        assert_eq!(old_capacity, 2);
        assert_eq!(semaphore.current_capacity(), 5);
        assert_eq!(semaphore.available_permits(), 5);
    }

    #[tokio::test]
    async fn test_try_acquire() {
        let semaphore = DynamicSemaphore::new(1);

        let permit1 = semaphore.try_acquire().unwrap();
        assert!(semaphore.try_acquire().is_err());

        drop(permit1);
        assert!(semaphore.try_acquire().is_ok());
    }

    #[tokio::test]
    async fn test_close() {
        let semaphore = DynamicSemaphore::new(1);

        assert!(!semaphore.is_closed());
        semaphore.close();
        assert!(semaphore.is_closed());

        assert!(semaphore.acquire().await.is_err());
    }

    /// Test that reproduces the exact live site issue that was discovered
    #[tokio::test]
    async fn test_over_capacity_acquisition_prevention() {
        let semaphore = Arc::new(DynamicSemaphore::new(5));

        // Step 1: Acquire permits like a live site would
        let permit1 = semaphore.acquire().await.unwrap();
        let permit2 = semaphore.acquire().await.unwrap();

        assert_eq!(semaphore.available_permits(), 3);
        assert_eq!(semaphore.permits_in_use(), 2);

        // Step 2: Reduce capacity while permits are in use (the critical scenario)
        semaphore.reduce_capacity(1);

        assert_eq!(semaphore.current_capacity(), 1);
        assert_eq!(semaphore.available_permits(), 1); // Should be 1 (5-2=3, but capped at 1)
        assert_eq!(semaphore.permits_in_use(), 2); // Still 2 in use (over capacity)

        // Step 3: Try to acquire a new permit while over capacity - should FAIL
        assert!(
            semaphore.try_acquire().is_err(),
            "Should not be able to acquire when over capacity"
        );

        // Step 4: Release permits and verify capacity is enforced
        drop(permit1);
        assert_eq!(semaphore.available_permits(), 1);
        assert_eq!(semaphore.permits_in_use(), 1);

        drop(permit2);
        assert_eq!(semaphore.available_permits(), 1);
        assert_eq!(semaphore.permits_in_use(), 0);

        // Step 5: Now acquisition should work since we're at capacity
        let permit_new = semaphore.try_acquire().unwrap();
        assert_eq!(semaphore.available_permits(), 0);
        assert_eq!(semaphore.permits_in_use(), 1);

        drop(permit_new);
        assert_eq!(semaphore.available_permits(), 1);
        assert_eq!(semaphore.permits_in_use(), 0);
    }

    /// Test concurrent operations under load to verify race condition fixes
    #[tokio::test]
    async fn test_concurrent_capacity_reduction() {
        let semaphore = Arc::new(DynamicSemaphore::new(10));
        let mut handles = vec![];

        // Start many tasks that acquire permits and hold them briefly
        for _ in 0..20 {
            let sem = semaphore.clone();
            handles.push(tokio::spawn(async move {
                if let Ok(permit) = sem.try_acquire() {
                    sleep(Duration::from_millis(50)).await;
                    drop(permit);
                }
                // Some tasks won't get permits due to capacity limits - this is expected
            }));
        }

        // While tasks are running, reduce capacity
        sleep(Duration::from_millis(10)).await;
        semaphore.reduce_capacity(5);

        // Wait for all tasks to complete
        for handle in handles {
            handle.await.unwrap();
        }

        // Verify final state - available permits should never exceed capacity
        assert!(semaphore.available_permits() <= semaphore.current_capacity());
        assert_eq!(semaphore.current_capacity(), 5);
    }

    /// Stress test with continuous capacity changes and concurrent acquisitions
    #[tokio::test]
    async fn test_stress_concurrent_operations() {
        let semaphore = Arc::new(DynamicSemaphore::new(50));
        let mut handles = vec![];

        // Start tasks that continuously try to acquire and release permits
        for _ in 0..100 {
            let sem = semaphore.clone();
            handles.push(tokio::spawn(async move {
                for _ in 0..5 {
                    if let Ok(permit) = sem.try_acquire() {
                        tokio::task::yield_now().await;
                        drop(permit);
                    }
                    tokio::task::yield_now().await;
                }
            }));
        }

        // Continuously reduce capacity while tasks are running
        let sem_reducer = semaphore.clone();
        let reducer_handle = tokio::spawn(async move {
            for new_capacity in (1..=50).rev() {
                sem_reducer.reduce_capacity(new_capacity);
                tokio::task::yield_now().await;
            }
        });

        // Wait for all tasks
        for handle in handles {
            handle.await.unwrap();
        }
        reducer_handle.await.unwrap();

        // Final verification - the semaphore should be in a valid state
        assert!(semaphore.available_permits() <= semaphore.current_capacity());
        assert_eq!(semaphore.current_capacity(), 1);
        assert_eq!(semaphore.permits_in_use(), 0);
    }

    /// Test that demonstrates integration scenarios similar to feroxbuster usage
    #[tokio::test]
    async fn test_feroxbuster_integration_scenario() {
        let limiter = Arc::new(DynamicSemaphore::new(3));

        // Simulate 3 active scans by acquiring all permits
        let permit1 = limiter.acquire().await.unwrap();
        let permit2 = limiter.acquire().await.unwrap();
        let permit3 = limiter.acquire().await.unwrap();

        assert_eq!(limiter.available_permits(), 0);
        assert_eq!(limiter.current_capacity(), 3);

        // Simulate user reducing scan limit from 3 to 1 via scan management menu
        limiter.reduce_capacity(1);
        assert_eq!(limiter.current_capacity(), 1);

        // Verify no new scans can start when over capacity
        assert!(limiter.try_acquire().is_err());

        // As scans complete, capacity reduction takes effect
        drop(permit1);
        assert_eq!(limiter.available_permits(), 1);

        drop(permit2);
        assert_eq!(limiter.available_permits(), 1); // Excess forgotten

        drop(permit3);
        assert_eq!(limiter.available_permits(), 1); // Excess forgotten

        // Now only 1 scan can run concurrently
        let _new_permit = limiter.acquire().await.unwrap();
        assert_eq!(limiter.available_permits(), 0);
        assert!(limiter.try_acquire().is_err());
    }

    /// Test edge cases and boundary conditions
    #[tokio::test]
    async fn test_edge_cases() {
        // Test zero capacity
        let semaphore = DynamicSemaphore::new(0);
        assert_eq!(semaphore.current_capacity(), 0);
        assert_eq!(semaphore.available_permits(), 0);
        assert!(semaphore.try_acquire().is_err());

        // Test capacity reduction to zero
        let semaphore = DynamicSemaphore::new(2);
        let permit = semaphore.acquire().await.unwrap();

        semaphore.reduce_capacity(0);
        assert_eq!(semaphore.current_capacity(), 0);
        assert!(semaphore.try_acquire().is_err());

        drop(permit);
        assert_eq!(semaphore.available_permits(), 0);
        assert!(semaphore.try_acquire().is_err());

        // Test large capacity values
        let semaphore = DynamicSemaphore::new(1000);
        assert_eq!(semaphore.current_capacity(), 1000);
        assert_eq!(semaphore.available_permits(), 1000);

        let permit = semaphore.try_acquire().unwrap();
        assert_eq!(semaphore.available_permits(), 999);
        drop(permit);
        assert_eq!(semaphore.available_permits(), 1000);
    }
}