···1010### Added
11111212- Added the `per_second_fn` and `burst_limit_fn` functions to dynamically set limits based on the identifier.
1313+- Added examples to the `examples/` directory.
1414+- Added examples to the documentation.
13151416### Changed
1517
+76-4
src/glimit.gleam
···7373 )
7474}
75757676-/// Set the rate limit per second.
7676+/// Set the rate of new available tokens per second.
7777///
7878-/// The value is not only used for the rate at which tokens are added to the bucket, but
7979-/// also for the maximum number of available tokens. To set a different value fo the
8080-/// maximum number of available tokens, use the `burst_limit` function.
7878+/// Note that this is not the maximum number of requests that can be made in a single
7979+/// second, but the rate at which tokens are added to the bucket. Think of this as the
8080+/// steady state rate limit, while the `burst_limit` function sets the maximum number of
8181+/// available tokens (or the burst rate limit).
8282+///
8383+/// This value is also used as the default value for the `burst_limit` function.
8484+///
8585+/// # Example
8686+///
8787+/// ```gleam
8888+/// import glimit
8989+///
9090+/// let limiter =
9191+/// glimit.new()
9292+/// |> glimit.per_second(10)
9393+/// ```
8194///
8295pub fn per_second(
8396 limiter: RateLimiterBuilder(a, b, id),
···8810189102/// Set the rate limit per second, based on the identifier.
90103///
104104+/// # Example
105105+///
106106+/// ```gleam
107107+/// import glimit
108108+///
109109+/// let limiter =
110110+/// glimit.new()
111111+/// |> glimit.identifier(fn(request) { request.user_id })
112112+/// |> glimit.per_second_fn(fn(user_id) {
113113+/// db.get_rate_limit(user_id)
114114+/// })
115115+/// ```
116116+///
91117pub fn per_second_fn(
92118 limiter: RateLimiterBuilder(a, b, id),
93119 limit_fn: fn(id) -> Int,
···100126/// The maximum number of available tokens is the maximum number of requests that can be
101127/// made in a single second. The default value is the same as the rate limit per second.
102128///
129129+/// # Example
130130+///
131131+/// ```gleam
132132+/// import glimit
133133+///
134134+/// let limiter =
135135+/// glimit.new()
136136+/// |> glimit.per_second(10)
137137+/// |> glimit.burst_limit(100)
138138+/// ```
139139+///
103140pub fn burst_limit(
104141 limiter: RateLimiterBuilder(a, b, id),
105142 burst_limit: Int,
···109146110147/// Set the maximum number of available tokens, based on the identifier.
111148///
149149+/// # Example
150150+///
151151+/// ```gleam
152152+/// import glimit
153153+///
154154+/// let limiter =
155155+/// glimit.new()
156156+/// |> glimit.identifier(fn(request) { request.user_id })
157157+/// |> glimit.per_second(10)
158158+/// |> glimit.burst_limit_fn(fn(user_id) {
159159+/// db.get_burst_limit(user_id)
160160+/// })
161161+/// ```
162162+///
112163pub fn burst_limit_fn(
113164 limiter: RateLimiterBuilder(a, b, id),
114165 burst_limit_fn: fn(id) -> Int,
···118169119170/// Set the handler to be called when the rate limit is reached.
120171///
172172+/// # Example
173173+///
174174+/// ```gleam
175175+/// import glimit
176176+///
177177+/// let limiter =
178178+/// glimit.new()
179179+/// |> glimit.per_second(10)
180180+/// |> glimit.on_limit_exceeded(fn(_request) { "Rate limit reached" })
181181+/// ```
182182+///
121183pub fn on_limit_exceeded(
122184 limiter: RateLimiterBuilder(a, b, id),
123185 on_limit_exceeded: fn(a) -> b,
···126188}
127189128190/// Set the identifier function to be used to identify the rate limit.
191191+///
192192+/// # Example
193193+///
194194+/// ```gleam
195195+/// import glimit
196196+///
197197+/// let limiter =
198198+/// glimit.new()
199199+/// |> glimit.identifier(fn(request) { request.ip })
200200+/// ```
129201///
130202pub fn identifier(
131203 limiter: RateLimiterBuilder(a, b, id),