+2
CHANGELOG.md
+2
CHANGELOG.md
+76
-4
src/glimit.gleam
+76
-4
src/glimit.gleam
···
73
73
)
74
74
}
75
75
76
-
/// Set the rate limit per second.
76
+
/// Set the rate of new available tokens per second.
77
77
///
78
-
/// The value is not only used for the rate at which tokens are added to the bucket, but
79
-
/// also for the maximum number of available tokens. To set a different value fo the
80
-
/// maximum number of available tokens, use the `burst_limit` function.
78
+
/// Note that this is not the maximum number of requests that can be made in a single
79
+
/// second, but the rate at which tokens are added to the bucket. Think of this as the
80
+
/// steady state rate limit, while the `burst_limit` function sets the maximum number of
81
+
/// available tokens (or the burst rate limit).
82
+
///
83
+
/// This value is also used as the default value for the `burst_limit` function.
84
+
///
85
+
/// # Example
86
+
///
87
+
/// ```gleam
88
+
/// import glimit
89
+
///
90
+
/// let limiter =
91
+
/// glimit.new()
92
+
/// |> glimit.per_second(10)
93
+
/// ```
81
94
///
82
95
pub fn per_second(
83
96
limiter: RateLimiterBuilder(a, b, id),
···
88
101
89
102
/// Set the rate limit per second, based on the identifier.
90
103
///
104
+
/// # Example
105
+
///
106
+
/// ```gleam
107
+
/// import glimit
108
+
///
109
+
/// let limiter =
110
+
/// glimit.new()
111
+
/// |> glimit.identifier(fn(request) { request.user_id })
112
+
/// |> glimit.per_second_fn(fn(user_id) {
113
+
/// db.get_rate_limit(user_id)
114
+
/// })
115
+
/// ```
116
+
///
91
117
pub fn per_second_fn(
92
118
limiter: RateLimiterBuilder(a, b, id),
93
119
limit_fn: fn(id) -> Int,
···
100
126
/// The maximum number of available tokens is the maximum number of requests that can be
101
127
/// made in a single second. The default value is the same as the rate limit per second.
102
128
///
129
+
/// # Example
130
+
///
131
+
/// ```gleam
132
+
/// import glimit
133
+
///
134
+
/// let limiter =
135
+
/// glimit.new()
136
+
/// |> glimit.per_second(10)
137
+
/// |> glimit.burst_limit(100)
138
+
/// ```
139
+
///
103
140
pub fn burst_limit(
104
141
limiter: RateLimiterBuilder(a, b, id),
105
142
burst_limit: Int,
···
109
146
110
147
/// Set the maximum number of available tokens, based on the identifier.
111
148
///
149
+
/// # Example
150
+
///
151
+
/// ```gleam
152
+
/// import glimit
153
+
///
154
+
/// let limiter =
155
+
/// glimit.new()
156
+
/// |> glimit.identifier(fn(request) { request.user_id })
157
+
/// |> glimit.per_second(10)
158
+
/// |> glimit.burst_limit_fn(fn(user_id) {
159
+
/// db.get_burst_limit(user_id)
160
+
/// })
161
+
/// ```
162
+
///
112
163
pub fn burst_limit_fn(
113
164
limiter: RateLimiterBuilder(a, b, id),
114
165
burst_limit_fn: fn(id) -> Int,
···
118
169
119
170
/// Set the handler to be called when the rate limit is reached.
120
171
///
172
+
/// # Example
173
+
///
174
+
/// ```gleam
175
+
/// import glimit
176
+
///
177
+
/// let limiter =
178
+
/// glimit.new()
179
+
/// |> glimit.per_second(10)
180
+
/// |> glimit.on_limit_exceeded(fn(_request) { "Rate limit reached" })
181
+
/// ```
182
+
///
121
183
pub fn on_limit_exceeded(
122
184
limiter: RateLimiterBuilder(a, b, id),
123
185
on_limit_exceeded: fn(a) -> b,
···
126
188
}
127
189
128
190
/// Set the identifier function to be used to identify the rate limit.
191
+
///
192
+
/// # Example
193
+
///
194
+
/// ```gleam
195
+
/// import glimit
196
+
///
197
+
/// let limiter =
198
+
/// glimit.new()
199
+
/// |> glimit.identifier(fn(request) { request.ip })
200
+
/// ```
129
201
///
130
202
pub fn identifier(
131
203
limiter: RateLimiterBuilder(a, b, id),