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
use SemaphoreQueue;
use Future;
/// A thread-safe model that limits the number of concurrently executing
/// async operations using a semaphore.
///
/// `PCModel<I>` is useful for rate-limiting access to a resource — for
/// example capping the number of simultaneous HTTP requests or database
/// queries. At most `n` closures (as configured in [`new`]) run at the same
/// time; additional callers suspend until a permit becomes available.
///
/// # Example
///
/// ```rust
/// use aqueue::PCModel;
/// use std::sync::Arc;
///
/// struct HttpClient;
/// impl HttpClient {
/// async fn get(&self, _url: &str) -> Vec<u8> { vec![] }
/// }
///
/// # #[tokio::main] async fn main() {
/// // Allow at most 4 concurrent requests
/// let client = Arc::new(PCModel::new(HttpClient, 4));
/// client.call(|c| async move { c.get("https://example.com").await }).await;
/// # }
/// ```
///
/// [`new`]: PCModel::new