+8
-1
CHANGELOG.md
+8
-1
CHANGELOG.md
···
5
5
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
7
7
8
+
## [Unreleased]
9
+
10
+
### Changed
11
+
12
+
- Removed the need for `glimit.build()` and `glimit.try_build()` functions. Now, the `glimit.apply()` function can be used directly with the limiter configuration.
13
+
14
+
8
15
## 0.2.0 - 2024-09-07
9
16
10
17
### Breaking changes
···
16
23
- Added a `burst_limit` setting to the limiter configuration. This setting allows the user to set the maximum number of tokens that the bucket can hold.
17
24
18
25
19
-
## v0.1.3 - 2024-09-04
26
+
## 0.1.3 - 2024-09-04
20
27
21
28
### Added
22
29
-1
README.md
-1
README.md
-1
examples/mist/src/app.gleam
-1
examples/mist/src/app.gleam
-1
examples/wisp/src/app.gleam
-1
examples/wisp/src/app.gleam
+29
-15
src/glimit.gleam
+29
-15
src/glimit.gleam
···
29
29
//// |> glimit.burst_limit(100)
30
30
//// |> glimit.identifier(fn(request) { request.ip })
31
31
//// |> glimit.on_limit_exceeded(fn(_request) { "Rate limit reached" })
32
-
//// |> glimit.build()
33
32
////
34
33
//// let handler =
35
34
//// fn(_request) { "Hello, world!" }
···
119
118
120
119
/// Build the rate limiter.
121
120
///
122
-
/// Panics if the rate limiter registry cannot be started or if the `identifier`
123
-
/// function or `on_limit_exceeded` function is missing.
121
+
/// Note that using `apply` will already build the rate limiter, so this function is
122
+
/// only useful if you want to build the rate limiter manually and apply it to multiple
123
+
/// functions.
124
124
///
125
-
/// To handle errors instead of panicking, use `try_build`.
125
+
/// To apply the resulting rate limiter to a function or handler, use the `apply_built`
126
+
/// function.
126
127
///
127
-
pub fn build(config: RateLimiterBuilder(a, b, id)) -> RateLimiter(a, b, id) {
128
-
case try_build(config) {
129
-
Ok(limiter) -> limiter
130
-
Error(message) -> panic as message
131
-
}
132
-
}
133
-
134
-
/// Build the rate limiter, but return an error instead of panicking.
135
-
///
136
-
pub fn try_build(
128
+
pub fn build(
137
129
config: RateLimiterBuilder(a, b, id),
138
130
) -> Result(RateLimiter(a, b, id), String) {
139
131
use per_second <- result.try(case config.per_second {
···
166
158
167
159
/// Apply the rate limiter to a request handler or function.
168
160
///
169
-
pub fn apply(func: fn(a) -> b, limiter: RateLimiter(a, b, id)) -> fn(a) -> b {
161
+
/// Panics if the rate limiter registry cannot be started or if the `identifier`
162
+
/// function or `on_limit_exceeded` function is missing.
163
+
///
164
+
pub fn apply(
165
+
func: fn(a) -> b,
166
+
config: RateLimiterBuilder(a, b, id),
167
+
) -> fn(a) -> b {
168
+
let limiter = case build(config) {
169
+
Ok(limiter) -> limiter
170
+
Error(message) -> panic as message
171
+
}
172
+
apply_built(func, limiter)
173
+
}
174
+
175
+
/// Apply the rate limiter to a request handler or function.
176
+
///
177
+
/// This function is useful if you want to build the rate limiter manually using the
178
+
/// `build` function.
179
+
///
180
+
pub fn apply_built(
181
+
func: fn(a) -> b,
182
+
limiter: RateLimiter(a, b, id),
183
+
) -> fn(a) -> b {
170
184
fn(input: a) -> b {
171
185
let identifier = limiter.identifier(input)
172
186
case limiter.rate_limiter_registry |> registry.get_or_create(identifier) {
+2
-4
test/glimit_test.gleam
+2
-4
test/glimit_test.gleam
···
14
14
|> glimit.per_second(2)
15
15
|> glimit.identifier(fn(_) { "id" })
16
16
|> glimit.on_limit_exceeded(fn(_) { "Stop!" })
17
-
|> glimit.build
18
17
19
18
let func =
20
19
fn(_) { "OK" }
···
32
31
|> glimit.per_second(2)
33
32
|> glimit.identifier(fn(x) { x })
34
33
|> glimit.on_limit_exceeded(fn(_) { "Stop!" })
35
-
|> glimit.build
36
34
37
35
let func =
38
36
fn(_) { "OK" }
···
47
45
}
48
46
49
47
pub fn burst_limit_test() {
50
-
let limiter =
48
+
let assert Ok(limiter) =
51
49
glimit.new()
52
50
|> glimit.per_second(1)
53
51
|> glimit.burst_limit(3)
···
57
55
58
56
let func =
59
57
fn(_) { "OK" }
60
-
|> glimit.apply(limiter)
58
+
|> glimit.apply_built(limiter)
61
59
62
60
let assert Ok(rate_limiter) =
63
61
limiter.rate_limiter_registry