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
|
use super::{async_queue::AsyncQueue, atomic_instant::AtomicInstant};
use std::{
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
time::Duration,
};
use tokio::{
sync::oneshot::{self, Sender},
task::JoinHandle,
};
use tracing::{debug, trace};
use twilight_http_ratelimiting::headers::Present;
#[derive(Clone, Debug)]
pub enum TimeRemaining {
Finished,
NotStarted,
Some(Duration),
}
/// A bucket is a simple atomic implementation of a bucket used for ratelimiting
/// It can be updated dynamically depending on the discord api responses.
///
/// # Usage
/// ```
/// # use ratelimit::buckets::bucket::Bucket;
/// # use twilight_http_ratelimiting::RatelimitHeaders;
/// # use std::time::SystemTime;
/// # tokio_test::block_on(async {
///
/// let bucket = Bucket::new();
///
/// // Feed the headers informations into the bucket to update it
/// let headers = [
/// ( "x-ratelimit-bucket", "bucket id".as_bytes()),
/// ("x-ratelimit-limit", "100".as_bytes()),
/// ("x-ratelimit-remaining", "0".as_bytes()),
/// ("x-ratelimit-reset", "99999999999999".as_bytes()),
/// ("x-ratelimit-reset-after", "10.000".as_bytes()),
/// ];
///
/// // Parse the headers
/// let present = if let Ok(RatelimitHeaders::Present(present))
/// = RatelimitHeaders::from_pairs(headers.into_iter()) {
/// present
/// } else { todo!() };
///
/// // this should idealy the time of the request
/// let current_time = SystemTime::now()
/// .duration_since(SystemTime::UNIX_EPOCH)
/// .unwrap()
/// .as_millis() as u64;
///
/// bucket.update(&present, current_time);
/// # })
/// ```
///
/// # Async
/// You need to call this struct new method in a tokio 1.x async runtime.
#[derive(Debug)]
pub struct Bucket {
/// Limits of tickets that can be accepted
pub limit: AtomicU64,
/// Remaining requests that can be executed
pub remaining: AtomicU64,
/// Time to wait after [`Self::last_update`] before accepting new tickets.
pub reset_after: AtomicU64,
/// Last update got from the discord upstream
pub last_update: AtomicInstant,
/// List of tasks that dequeue tasks from [`Self::queue`]
tasks: Vec<JoinHandle<()>>,
/// Queue of tickets to be processed.
queue: AsyncQueue<Sender<()>>,
}
impl Drop for Bucket {
/// Simply abord the dequeue tasks to aboid leaking memory via arc(s)
fn drop(&mut self) {
for join in &self.tasks {
join.abort();
}
}
}
impl Bucket {
/// Creates a new bucket with four dequeue tasks
/// # Async
/// This functions **should** be called in a tokio 1.x runtime, otherwise the function *will* panic.
#[must_use]
pub fn new() -> Arc<Self> {
let tasks = vec![];
let this = Arc::new(Self {
limit: AtomicU64::new(u64::max_value()),
queue: AsyncQueue::default(),
remaining: AtomicU64::new(u64::max_value()),
reset_after: AtomicU64::new(u64::max_value()),
last_update: AtomicInstant::default(),
tasks,
});
// Run with 4 dequeue tasks
for _ in 0..4 {
let this = this.clone();
tokio::spawn(async move {
// continuously wait for elements in the queue to process them sequantially.
// this is using parallel tasks to allow (hopefully) better performance.
while let Some(element) = this.queue.pop().await {
if this.remaining() == 0 {
debug!("0 tickets remaining, we have to wait.");
match this.time_remaining() {
TimeRemaining::Finished => {
debug!("waiting seems finished.");
this.try_reset();
}
TimeRemaining::Some(duration) => {
debug!(milliseconds=%duration.as_millis(), "waiting for ratelimit");
tokio::time::sleep(duration).await;
this.try_reset();
}
TimeRemaining::NotStarted => {
debug!("we should not wait");
}
}
}
this.remaining.fetch_sub(1, Ordering::Relaxed);
let _ = element
.send(())
.map_err(|_| trace!("response channel was closed."));
}
});
}
this
}
/// Total number of tickets allowed in a cycle.
pub fn limit(&self) -> u64 {
self.limit.load(Ordering::Relaxed)
}
/// Number of tickets remaining in the current cycle.
pub fn remaining(&self) -> u64 {
self.remaining.load(Ordering::Relaxed)
}
/// Duration after the [`Self::last_update`] time the bucket will refresh.
pub fn reset_after(&self) -> u64 {
self.reset_after.load(Ordering::Relaxed)
}
/// Time remaining until this bucket will reset.
pub fn time_remaining(&self) -> TimeRemaining {
let reset_after = self.reset_after();
let last_update = &self.last_update;
if last_update.is_empty() {
debug!("last update is empty");
TimeRemaining::NotStarted
} else {
let elapsed = last_update.elapsed();
if elapsed > Duration::from_millis(reset_after) {
return TimeRemaining::Finished;
}
TimeRemaining::Some(Duration::from_millis(reset_after) - elapsed)
}
}
/// Try to reset this bucket's [`Self::last_update`] value if it has finished.
///
/// Returns whether resetting was possible.
pub fn try_reset(&self) -> bool {
if self.last_update.is_empty() {
return false;
}
if matches!(self.time_remaining(), TimeRemaining::Finished) {
self.remaining.store(self.limit(), Ordering::Relaxed);
self.last_update.set_millis(0);
true
} else {
false
}
}
/// Update this bucket's ratelimit data after a request has been made.
/// The time of the request should be given.
pub fn update(&self, ratelimits: &Present, time: u64) {
let bucket_limit = self.limit();
if self.last_update.is_empty() {
debug!(millis = time, "updated the last update time");
self.last_update.set_millis(time);
}
if bucket_limit != ratelimits.limit() && bucket_limit == u64::max_value() {
self.reset_after
.store(ratelimits.reset_after(), Ordering::SeqCst);
self.limit.store(ratelimits.limit(), Ordering::SeqCst);
}
self.remaining
.store(ratelimits.remaining(), Ordering::Relaxed);
}
/// Submits a ticket to the queue
/// A oneshot receiver is returned and will be called when the ticket is accepted.
pub fn ticket(&self) -> oneshot::Receiver<()> {
let (tx, rx) = oneshot::channel();
self.queue.push(tx);
rx
}
}
#[cfg(test)]
mod tests {
use std::{
ops::Add,
time::{Duration, Instant, SystemTime},
};
use tokio::time::timeout;
use tracing::info;
use twilight_http_ratelimiting::RatelimitHeaders;
use super::Bucket;
#[test_log::test(tokio::test)]
async fn should_ratelimit() {
let bucket = Bucket::new();
// Intialize a bucket with one remaining ticket
// and that resets in oue hour
let mreset = SystemTime::now()
.add(Duration::from_secs(100))
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis()
.to_string();
let headers: [(&str, &[u8]); 5] = [
("x-ratelimit-bucket", b"123"),
("x-ratelimit-limit", b"100"),
("x-ratelimit-remaining", b"1"),
("x-ratelimit-reset", mreset.as_bytes()),
("x-ratelimit-reset-after", b"100.000"),
];
if let RatelimitHeaders::Present(present) =
RatelimitHeaders::from_pairs(headers.into_iter()).unwrap()
{
// Integer truncating is expected
#[allow(clippy::cast_possible_truncation)]
bucket.update(
&present,
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis() as u64,
);
}
let ticket = bucket.ticket();
info!("first request");
// We should accept one ticket
let respo = timeout(Duration::from_secs(10), ticket).await;
assert!(respo.is_ok());
info!("second request");
let ticket = bucket.ticket();
// We should accept one ticket
let respo = timeout(Duration::from_secs(1), ticket).await;
// the ticket should not have responded because the queue is locked
assert!(respo.is_err());
}
#[test_log::test(tokio::test)]
async fn should_block_until_possible() {
let bucket = Bucket::new();
// Intialize a bucket with one remaining ticket
// and that resets in oue hour
let mreset = SystemTime::now()
.add(Duration::from_secs(100))
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis()
.to_string();
let headers: [(&str, &[u8]); 5] = [
("x-ratelimit-bucket", b"123"),
("x-ratelimit-limit", b"100"),
("x-ratelimit-remaining", b"0"),
("x-ratelimit-reset", mreset.as_bytes()),
("x-ratelimit-reset-after", b"10.000"),
];
if let RatelimitHeaders::Present(present) =
RatelimitHeaders::from_pairs(headers.into_iter()).unwrap()
{
// Integer truncating is expected
#[allow(clippy::cast_possible_truncation)]
bucket.update(
&present,
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_millis() as u64,
);
}
let ticket = bucket.ticket();
let start = Instant::now();
// in this case, the ratelimiter should wait 10 seconds
let respo = timeout(Duration::from_secs(12), ticket).await;
let end = start.elapsed().as_secs();
// we should have waited 10 seconds (+- 1s)
assert_eq!(10, end);
// and the ticket should be a success
assert!(respo.is_ok());
}
}
|