This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
/
ratelimit.d
118 lines (97 loc) · 3.15 KB
/
ratelimit.d
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
/**
Utilities for tracking and managing rate limits.
*/
module dscord.api.ratelimit;
import std.conv,
std.math,
std.random,
core.time,
core.sync.mutex;
import vibe.core.core;
import vibe.core.sync : LocalManualEvent, createManualEvent;
import dscord.api.routes,
dscord.util.time;
/// Return a random backoff duration (by default between 0.5 and 3 seconds)
Duration randomBackoff(int low=500, int high=3000) {
int milliseconds = uniform(low, high);
return milliseconds.msecs;
}
/**
Stores the rate limit state for a given bucket.
*/
struct RateLimitState {
int remaining;
/// Time at which this rate limit resets
long resetTime;
/// Returns true if this request is valid
bool willRateLimit() {
if (this.remaining - 1 < 0) {
if (getUnixTime() <= this.resetTime) {
return true;
}
}
return false;
}
/// Return the time that needs to be waited before another request can be made
Duration waitTime() {
return (this.resetTime - getUnixTime()).seconds + 500.msecs;
}
}
/**
RateLimiter provides an interface for rate limiting HTTP Requests.
*/
class RateLimiter {
LocalManualEvent[Bucket] cooldowns;
RateLimitState[Bucket] states;
/// Cooldown a bucket for a given duration. Blocks ALL requests from completing.
void cooldown(Bucket bucket, Duration duration) {
if (bucket in this.cooldowns) {
this.cooldowns[bucket].wait();
} else {
this.cooldowns[bucket] = createManualEvent();
sleep(duration);
this.cooldowns[bucket].emit();
this.cooldowns.remove(bucket);
}
}
/**
Check whether a request can be made for a bucket. If the bucket is on cooldown,
wait until the cooldown resets before returning.
*/
bool check(Bucket bucket, Duration timeout) {
// If we're currently waiting for a cooldown, join the waiting club
if (bucket in this.cooldowns) {
if (this.cooldowns[bucket].wait(timeout, 0) != 0) {
return false;
}
}
// If we don't have the bucket cached, return
if (bucket !in this.states) return true;
// If this request will rate limit, wait until it won't anymore
if (this.states[bucket].willRateLimit()) {
this.cooldown(bucket, this.states[bucket].waitTime());
}
return true;
}
/// Update a given bucket with headers returned from a request.
void update(Bucket bucket, string remaining, string reset, string retryAfter) {
long resetSeconds = (reset.to!long);
// If we have a retryAfter header, it may be more accurate
if (retryAfter != "") {
FloatingPointControl fpctrl;
fpctrl.rounding = FloatingPointControl.roundUp;
long retryAfterSeconds = rndtol(retryAfter.to!long / 1000.0);
long nextRequestAt = getUnixTime() + retryAfterSeconds;
if (nextRequestAt > resetSeconds) {
resetSeconds = nextRequestAt;
}
}
// Create a new RateLimitState if one doesn't exist
if (bucket !in this.states) {
this.states[bucket] = RateLimitState();
}
// Save our remaining requests and reset seconds
this.states[bucket].remaining = remaining.to!int;
this.states[bucket].resetTime = resetSeconds;
}
}