parse_book_source/http_client/rate_limiter/
token_bucket.rs

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
use std::sync::Arc;

use tokio::{
    sync::Semaphore,
    time::{interval, Duration},
};
use tokio_util::sync::CancellationToken;

#[derive(Debug, Clone)]
pub struct TokenBucket {
    pub sem: Arc<Semaphore>,
    pub cancel_token: CancellationToken,
}

impl TokenBucket {
    pub fn new(capacity: usize, rate: Duration) -> Self {
        let sem = Arc::new(Semaphore::new(capacity));
        let cancel_token = CancellationToken::new();
        tokio::spawn({
            let sem = sem.clone();
            let cancel_token = cancel_token.clone();
            let mut interval = interval(rate);
            interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

            async move {
                loop {
                    tokio::select! {
                        _ = cancel_token.cancelled() => break,
                        _ = interval.tick() => {
                            if sem.available_permits() < capacity {
                                sem.add_permits(1);
                            }
                        }
                    }
                }
            }
        });

        Self { sem, cancel_token }
    }

    pub async fn acquire(&self) {
        let permits = self.sem.acquire().await.unwrap();
        permits.forget();
    }
}

impl Drop for TokenBucket {
    fn drop(&mut self) {
        self.cancel_token.cancel();
    }
}