aboutsummaryrefslogtreecommitdiff
path: root/src/framework/buckets.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-05-22 17:02:00 -0700
committerZeyla Hellyer <[email protected]>2017-05-22 17:02:00 -0700
commit9969be60cf320797c37b317da24d9a08fd5eafa5 (patch)
treef27bf7a57af95bbc11990b1edcea9cca99276964 /src/framework/buckets.rs
parentReasonably derive Debug on items (diff)
downloadserenity-9969be60cf320797c37b317da24d9a08fd5eafa5.tar.xz
serenity-9969be60cf320797c37b317da24d9a08fd5eafa5.zip
Restructure modules
Modules are now separated into a fashion where the library can be used for most use cases, without needing to compile the rest. The core of serenity, with no features enabled, contains only the struct (model) definitions, constants, and prelude. Models do not have most functions compiled in, as that is separated into the `model` feature. The `client` module has been split into 3 modules: `client`, `gateway`, and `http`. `http` contains functions to interact with the REST API. `gateway` contains the Shard to interact with the gateway, requiring `http` for retrieving the gateway URL. `client` requires both of the other features and acts as an abstracted interface over both the gateway and REST APIs, handling the event loop. The `builder` module has been separated from `utils`, and can now be optionally compiled in. It and the `http` feature are required by the `model` feature due to a large number of methods requiring access to them. `utils` now contains a number of utilities, such as the Colour struct, the `MessageBuilder`, and mention parsing functions. Each of the original `ext` modules are still featured, with `cache` not requiring any feature to be enabled, `framework` requiring the `client`, `model`, and `utils`, and `voice` requiring `gateway`. In total the features and their requirements are: - `builder`: none - `cache`: none - `client`: `gateway`, `http` - `framework`: `client`, `model`, `utils` - `gateway`: `http` - `http`: none - `model`: `builder`, `http` - `utils`: none - `voice`: `gateway` The default features are `builder`, `cache`, `client`, `framework`, `gateway`, `model`, `http`, and `utils`. To help with forwards compatibility, modules have been re-exported from their original locations.
Diffstat (limited to 'src/framework/buckets.rs')
-rw-r--r--src/framework/buckets.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/framework/buckets.rs b/src/framework/buckets.rs
new file mode 100644
index 0000000..02cd658
--- /dev/null
+++ b/src/framework/buckets.rs
@@ -0,0 +1,60 @@
+use std::collections::HashMap;
+use std::default::Default;
+use time;
+
+#[doc(hidden)]
+pub struct Ratelimit {
+ pub delay: i64,
+ pub limit: Option<(i64, i32)>,
+}
+
+#[doc(hidden)]
+pub struct MemberRatelimit {
+ pub tickets: i32,
+ pub last_time: i64,
+ pub set_time: i64,
+}
+
+impl Default for MemberRatelimit {
+ fn default() -> Self {
+ MemberRatelimit {
+ tickets: 0,
+ last_time: 0,
+ set_time: 0,
+ }
+ }
+}
+
+#[doc(hidden)]
+pub struct Bucket {
+ pub ratelimit: Ratelimit,
+ pub users: HashMap<u64, MemberRatelimit>,
+}
+
+impl Bucket {
+ pub fn take(&mut self, user_id: u64) -> i64 {
+ let time = time::get_time().sec;
+ let user = self.users.entry(user_id)
+ .or_insert_with(MemberRatelimit::default);
+
+ if let Some((timespan, limit)) = self.ratelimit.limit {
+ if (user.tickets + 1) > limit {
+ if time < (user.set_time + timespan) {
+ return (user.set_time + timespan) - time;
+ } else {
+ user.tickets = 0;
+ user.set_time = time;
+ }
+ }
+ }
+
+ if time < user.last_time + self.ratelimit.delay {
+ (user.last_time + self.ratelimit.delay) - time
+ } else {
+ user.tickets += 1;
+ user.last_time = time;
+
+ 0
+ }
+ }
+}