···4455The 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).
6677+78## [Unreleased]
99+1010+### Changed
1111+1212+- Deprecated `glimit.handler` and renamed it to the more descriptive `glimit.on_limit_exceeded`.
1313+814915## v0.1.3 - 2024-09-04
1016
···2626//// |> glimit.per_minute(100)
2727//// |> glimit.per_hour(1000)
2828//// |> glimit.identifier(fn(request) { request.ip })
2929-//// |> glimit.handler(fn(_request) { "Rate limit reached" })
2929+//// |> glimit.on_limit_exceeded(fn(_request) { "Rate limit reached" })
3030//// |> glimit.build()
3131////
3232//// let handler =
···4545pub type RateLimiter(a, b, id) {
4646 RateLimiter(
4747 subject: Subject(actor.Message(id)),
4848- handler: fn(a) -> b,
4848+ on_limit_exceeded: fn(a) -> b,
4949 identifier: fn(a) -> id,
5050 )
5151}
···5858 per_minute: Option(Int),
5959 per_hour: Option(Int),
6060 identifier: Option(fn(a) -> id),
6161- handler: Option(fn(a) -> b),
6161+ on_limit_exceeded: Option(fn(a) -> b),
6262 )
6363}
6464···7070 per_minute: None,
7171 per_hour: None,
7272 identifier: None,
7373- handler: None,
7373+ on_limit_exceeded: None,
7474 )
7575}
7676···103103104104/// Set the handler to be called when the rate limit is reached.
105105///
106106+pub fn on_limit_exceeded(
107107+ limiter: RateLimiterBuilder(a, b, id),
108108+ on_limit_exceeded: fn(a) -> b,
109109+) -> RateLimiterBuilder(a, b, id) {
110110+ RateLimiterBuilder(..limiter, on_limit_exceeded: Some(on_limit_exceeded))
111111+}
112112+113113+@deprecated("Use `on_limit_exceeded` instead")
106114pub fn handler(
107115 limiter: RateLimiterBuilder(a, b, id),
108108- handler: fn(a) -> b,
116116+ on_limit_exceeded: fn(a) -> b,
109117) -> RateLimiterBuilder(a, b, id) {
110110- RateLimiterBuilder(..limiter, handler: Some(handler))
118118+ RateLimiterBuilder(..limiter, on_limit_exceeded: Some(on_limit_exceeded))
111119}
112120113121/// Set the identifier function to be used to identify the rate limit.
···122130/// Build the rate limiter.
123131///
124132/// Panics if the rate limiter actor cannot be started or if the identifier
125125-/// function or handler function is missing.
133133+/// function or on_limit_exceeded function is missing.
126134///
127135pub fn build(config: RateLimiterBuilder(a, b, id)) -> RateLimiter(a, b, id) {
128136 case try_build(config) {
···142150 )
143151 use identifier <- result.try(case config.identifier {
144152 Some(identifier) -> Ok(identifier)
145145- None -> Error("Identifier function is required")
153153+ None -> Error("`identifier` function is required")
146154 })
147147- use handler <- result.try(case config.handler {
148148- Some(handler) -> Ok(handler)
149149- None -> Error("Handler function is required")
155155+ use on_limit_exceeded <- result.try(case config.on_limit_exceeded {
156156+ Some(on_limit_exceeded) -> Ok(on_limit_exceeded)
157157+ None -> Error("`on_limit_exceeded` function is required")
150158 })
151159152152- Ok(RateLimiter(subject: subject, handler: handler, identifier: identifier))
160160+ Ok(RateLimiter(
161161+ subject: subject,
162162+ on_limit_exceeded: on_limit_exceeded,
163163+ identifier: identifier,
164164+ ))
153165}
154166155167/// Apply the rate limiter to a request handler or function.
···159171 let identifier = limiter.identifier(input)
160172 case actor.hit(limiter.subject, identifier) {
161173 Ok(Nil) -> func(input)
162162- Error(Nil) -> limiter.handler(input)
174174+ Error(Nil) -> limiter.on_limit_exceeded(input)
163175 }
164176 }
165177}
···167179/// Apply the rate limiter to a request handler or function with two arguments.
168180///
169181/// Note: this function folds the two arguments into a tuple before passing them to the
170170-/// identifier or handler functions.
182182+/// identifier or on_limit_exceeded functions.
171183///
172184/// # Example
173185///
···181193/// let #(a, _) = i
182194/// a
183195/// })
184184-/// |> glimit.handler(fn(_) { "Rate limit reached" })
196196+/// |> glimit.on_limit_exceeded(fn(_) { "Rate limit reached" })
185197/// |> glimit.build()
186198///
187199/// let handler =
···196208 let identifier = limiter.identifier(#(a, b))
197209 case actor.hit(limiter.subject, identifier) {
198210 Ok(Nil) -> func(a, b)
199199- Error(Nil) -> limiter.handler(#(a, b))
211211+ Error(Nil) -> limiter.on_limit_exceeded(#(a, b))
200212 }
201213 }
202214}
···204216/// Apply the rate limiter to a request handler or function with three arguments.
205217///
206218/// Note: this function folds the three arguments into a tuple before passing them to the
207207-/// identifier or handler functions.
219219+/// identifier or on_limit_exceeded functions.
208220///
209221pub fn apply3(
210222 func: fn(a, b, c) -> d,
···214226 let identifier = limiter.identifier(#(a, b, c))
215227 case actor.hit(limiter.subject, identifier) {
216228 Ok(Nil) -> func(a, b, c)
217217- Error(Nil) -> limiter.handler(#(a, b, c))
229229+ Error(Nil) -> limiter.on_limit_exceeded(#(a, b, c))
218230 }
219231 }
220232}
···222234/// Apply the rate limiter to a request handler or function with four arguments.
223235///
224236/// Note: this function folds the four arguments into a tuple before passing them to the
225225-/// identifier or handler functions.
237237+/// identifier or on_limit_exceeded functions.
226238///
227239/// > ⚠️ For functions with more than four arguments, you'll need to write a custom
228240/// > wrapper function that folds the arguments into a tuple before passing them to the
···237249 let identifier = limiter.identifier(#(a, b, c, d))
238250 case actor.hit(limiter.subject, identifier) {
239251 Ok(Nil) -> func(a, b, c, d)
240240- Error(Nil) -> limiter.handler(#(a, b, c, d))
252252+ Error(Nil) -> limiter.on_limit_exceeded(#(a, b, c, d))
241253 }
242254 }
243255}