summaryrefslogtreecommitdiff
path: root/src/adaptive.rs
blob: ca9e0fc0e505ed9c6bbf7784443baa7b75ed97b4 (plain)
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
use std::sync::Arc;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};

pub struct AdaptiveConcurrency {
    current_concurrent: usize,
    min_concurrent: usize,
    max_concurrent: usize,
    failure_streak: usize,
    success_count: usize,
    fail_count: usize,
    semaphore: Arc<Semaphore>,
}

impl AdaptiveConcurrency {
    pub fn new(max_concurrent: usize) -> Self {
        let initial = num_cpus::get().min(max_concurrent);
        AdaptiveConcurrency {
            current_concurrent: initial,
            min_concurrent: 1,
            max_concurrent,
            failure_streak: 0,
            success_count: 0,
            fail_count: 0,
            semaphore: Arc::new(Semaphore::new(initial)),
        }
    }

    pub fn on_success(&mut self) {
        self.success_count += 1;
        self.failure_streak = 0;

        if self.success_count % 10 == 0 && self.current_concurrent < self.max_concurrent {
            self.current_concurrent += 1;
            self.semaphore
                .add_permits(1);
        }
    }

    pub fn on_failure(&mut self) {
        self.fail_count += 1;
        self.failure_streak += 1;

        if self.failure_streak >= 3 && self.current_concurrent > self.min_concurrent {
            let new_concurrent = (self.current_concurrent / 2).max(self.min_concurrent);
            let reduction = self.current_concurrent - new_concurrent;
            self.current_concurrent = new_concurrent;

            for _ in 0..reduction {
                let _ = self
                    .semaphore
                    .try_acquire();
            }
        }
    }

    pub fn get_concurrent(&self) -> usize {
        self.current_concurrent
    }

    pub fn semaphore(&self) -> Arc<Semaphore> {
        self.semaphore
            .clone()
    }
}

pub async fn acquire_slot(semaphore: &Arc<Semaphore>) -> OwnedSemaphorePermit {
    semaphore
        .clone()
        .acquire_owned()
        .await
        .unwrap()
}