Skip to content

Commit e38365c

Browse files
feat(http-ratelimiting): add Bucket::new (#2026)
Add `Bucket::new` to allow users to construct buckets, necessary for implementing the `Ratelimiter` trait's `bucket` method. This was a gap in the API that made the trait unable to be implemented.
1 parent 9f67808 commit e38365c

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

Diff for: twilight-http-ratelimiting/src/in_memory/mod.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ impl Ratelimiter for InMemoryRatelimiter {
113113
|| Box::pin(future::ok(None)),
114114
|bucket| {
115115
let started_at = bucket.started_at.lock().expect("bucket poisoned");
116-
117-
Box::pin(future::ok(Some(InfoBucket {
118-
limit: bucket.limit(),
119-
remaining: bucket.remaining(),
120-
reset_after: Duration::from_millis(bucket.reset_after()),
121-
started_at: *started_at,
122-
})))
116+
let reset_after = Duration::from_millis(bucket.reset_after());
117+
118+
Box::pin(future::ok(Some(InfoBucket::new(
119+
bucket.limit(),
120+
bucket.remaining(),
121+
reset_after,
122+
*started_at,
123+
))))
123124
},
124125
)
125126
}

Diff for: twilight-http-ratelimiting/src/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ pub struct Bucket {
5353
}
5454

5555
impl Bucket {
56+
/// Create a representation of a ratelimiter bucket.
57+
///
58+
/// Buckets are returned by ratelimiters via [`Ratelimiter::bucket`] method.
59+
/// Its primary use is for informational purposes, including information
60+
/// such as the [number of remaining tickets][`Self::limit`] or determining
61+
/// how much time remains
62+
/// [until the bucket interval resets][`Self::time_remaining`].
63+
#[must_use]
64+
pub const fn new(
65+
limit: u64,
66+
remaining: u64,
67+
reset_after: Duration,
68+
started_at: Option<Instant>,
69+
) -> Self {
70+
Self {
71+
limit,
72+
remaining,
73+
reset_after,
74+
started_at,
75+
}
76+
}
77+
5678
/// Total number of tickets allotted in a cycle.
5779
#[must_use]
5880
pub const fn limit(&self) -> u64 {

0 commit comments

Comments
 (0)