this repo has no description

✏️ Renamed handler to on_limit_exceeded

+52 -34
+6
CHANGELOG.md
··· 4 4 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 + - Deprecated `glimit.handler` and renamed it to the more descriptive `glimit.on_limit_exceeded`. 13 + 8 14 9 15 ## v0.1.3 - 2024-09-04 10 16
+2 -2
README.md
··· 20 20 glimit.new() 21 21 |> glimit.per_second(2) 22 22 |> glimit.identifier(fn(x) { x }) 23 - |> glimit.handler(fn(_) { "Stop!" }) 23 + |> glimit.on_limit_exceeded(fn(_) { "Stop!" }) 24 24 |> glimit.build 25 25 26 26 let func = ··· 76 76 |> glimit.per_minute(100) 77 77 |> glimit.per_hour(1000) 78 78 |> glimit.identifier(get_identifier) 79 - |> glimit.handler(rate_limit_reached) 79 + |> glimit.on_limit_exceeded(rate_limit_reached) 80 80 |> glimit.build 81 81 82 82 let assert Ok(_) =
+32 -20
src/glimit.gleam
··· 26 26 //// |> glimit.per_minute(100) 27 27 //// |> glimit.per_hour(1000) 28 28 //// |> glimit.identifier(fn(request) { request.ip }) 29 - //// |> glimit.handler(fn(_request) { "Rate limit reached" }) 29 + //// |> glimit.on_limit_exceeded(fn(_request) { "Rate limit reached" }) 30 30 //// |> glimit.build() 31 31 //// 32 32 //// let handler = ··· 45 45 pub type RateLimiter(a, b, id) { 46 46 RateLimiter( 47 47 subject: Subject(actor.Message(id)), 48 - handler: fn(a) -> b, 48 + on_limit_exceeded: fn(a) -> b, 49 49 identifier: fn(a) -> id, 50 50 ) 51 51 } ··· 58 58 per_minute: Option(Int), 59 59 per_hour: Option(Int), 60 60 identifier: Option(fn(a) -> id), 61 - handler: Option(fn(a) -> b), 61 + on_limit_exceeded: Option(fn(a) -> b), 62 62 ) 63 63 } 64 64 ··· 70 70 per_minute: None, 71 71 per_hour: None, 72 72 identifier: None, 73 - handler: None, 73 + on_limit_exceeded: None, 74 74 ) 75 75 } 76 76 ··· 103 103 104 104 /// Set the handler to be called when the rate limit is reached. 105 105 /// 106 + pub fn on_limit_exceeded( 107 + limiter: RateLimiterBuilder(a, b, id), 108 + on_limit_exceeded: fn(a) -> b, 109 + ) -> RateLimiterBuilder(a, b, id) { 110 + RateLimiterBuilder(..limiter, on_limit_exceeded: Some(on_limit_exceeded)) 111 + } 112 + 113 + @deprecated("Use `on_limit_exceeded` instead") 106 114 pub fn handler( 107 115 limiter: RateLimiterBuilder(a, b, id), 108 - handler: fn(a) -> b, 116 + on_limit_exceeded: fn(a) -> b, 109 117 ) -> RateLimiterBuilder(a, b, id) { 110 - RateLimiterBuilder(..limiter, handler: Some(handler)) 118 + RateLimiterBuilder(..limiter, on_limit_exceeded: Some(on_limit_exceeded)) 111 119 } 112 120 113 121 /// Set the identifier function to be used to identify the rate limit. ··· 122 130 /// Build the rate limiter. 123 131 /// 124 132 /// Panics if the rate limiter actor cannot be started or if the identifier 125 - /// function or handler function is missing. 133 + /// function or on_limit_exceeded function is missing. 126 134 /// 127 135 pub fn build(config: RateLimiterBuilder(a, b, id)) -> RateLimiter(a, b, id) { 128 136 case try_build(config) { ··· 142 150 ) 143 151 use identifier <- result.try(case config.identifier { 144 152 Some(identifier) -> Ok(identifier) 145 - None -> Error("Identifier function is required") 153 + None -> Error("`identifier` function is required") 146 154 }) 147 - use handler <- result.try(case config.handler { 148 - Some(handler) -> Ok(handler) 149 - None -> Error("Handler function is required") 155 + use on_limit_exceeded <- result.try(case config.on_limit_exceeded { 156 + Some(on_limit_exceeded) -> Ok(on_limit_exceeded) 157 + None -> Error("`on_limit_exceeded` function is required") 150 158 }) 151 159 152 - Ok(RateLimiter(subject: subject, handler: handler, identifier: identifier)) 160 + Ok(RateLimiter( 161 + subject: subject, 162 + on_limit_exceeded: on_limit_exceeded, 163 + identifier: identifier, 164 + )) 153 165 } 154 166 155 167 /// Apply the rate limiter to a request handler or function. ··· 159 171 let identifier = limiter.identifier(input) 160 172 case actor.hit(limiter.subject, identifier) { 161 173 Ok(Nil) -> func(input) 162 - Error(Nil) -> limiter.handler(input) 174 + Error(Nil) -> limiter.on_limit_exceeded(input) 163 175 } 164 176 } 165 177 } ··· 167 179 /// Apply the rate limiter to a request handler or function with two arguments. 168 180 /// 169 181 /// Note: this function folds the two arguments into a tuple before passing them to the 170 - /// identifier or handler functions. 182 + /// identifier or on_limit_exceeded functions. 171 183 /// 172 184 /// # Example 173 185 /// ··· 181 193 /// let #(a, _) = i 182 194 /// a 183 195 /// }) 184 - /// |> glimit.handler(fn(_) { "Rate limit reached" }) 196 + /// |> glimit.on_limit_exceeded(fn(_) { "Rate limit reached" }) 185 197 /// |> glimit.build() 186 198 /// 187 199 /// let handler = ··· 196 208 let identifier = limiter.identifier(#(a, b)) 197 209 case actor.hit(limiter.subject, identifier) { 198 210 Ok(Nil) -> func(a, b) 199 - Error(Nil) -> limiter.handler(#(a, b)) 211 + Error(Nil) -> limiter.on_limit_exceeded(#(a, b)) 200 212 } 201 213 } 202 214 } ··· 204 216 /// Apply the rate limiter to a request handler or function with three arguments. 205 217 /// 206 218 /// Note: this function folds the three arguments into a tuple before passing them to the 207 - /// identifier or handler functions. 219 + /// identifier or on_limit_exceeded functions. 208 220 /// 209 221 pub fn apply3( 210 222 func: fn(a, b, c) -> d, ··· 214 226 let identifier = limiter.identifier(#(a, b, c)) 215 227 case actor.hit(limiter.subject, identifier) { 216 228 Ok(Nil) -> func(a, b, c) 217 - Error(Nil) -> limiter.handler(#(a, b, c)) 229 + Error(Nil) -> limiter.on_limit_exceeded(#(a, b, c)) 218 230 } 219 231 } 220 232 } ··· 222 234 /// Apply the rate limiter to a request handler or function with four arguments. 223 235 /// 224 236 /// Note: this function folds the four arguments into a tuple before passing them to the 225 - /// identifier or handler functions. 237 + /// identifier or on_limit_exceeded functions. 226 238 /// 227 239 /// > ⚠️ For functions with more than four arguments, you'll need to write a custom 228 240 /// > wrapper function that folds the arguments into a tuple before passing them to the ··· 237 249 let identifier = limiter.identifier(#(a, b, c, d)) 238 250 case actor.hit(limiter.subject, identifier) { 239 251 Ok(Nil) -> func(a, b, c, d) 240 - Error(Nil) -> limiter.handler(#(a, b, c, d)) 252 + Error(Nil) -> limiter.on_limit_exceeded(#(a, b, c, d)) 241 253 } 242 254 } 243 255 }
+12 -12
test/glimit_test.gleam
··· 11 11 glimit.new() 12 12 |> glimit.per_second(2) 13 13 |> glimit.identifier(fn(_) { "id" }) 14 - |> glimit.handler(fn(_) { "Stop!" }) 14 + |> glimit.on_limit_exceeded(fn(_) { "Stop!" }) 15 15 |> glimit.build 16 16 17 17 let func = ··· 29 29 glimit.new() 30 30 |> glimit.per_minute(2) 31 31 |> glimit.identifier(fn(_) { "id" }) 32 - |> glimit.handler(fn(_) { "Stop!" }) 32 + |> glimit.on_limit_exceeded(fn(_) { "Stop!" }) 33 33 |> glimit.build 34 34 35 35 let func = ··· 47 47 glimit.new() 48 48 |> glimit.per_hour(2) 49 49 |> glimit.identifier(fn(_) { "id" }) 50 - |> glimit.handler(fn(_) { "Stop!" }) 50 + |> glimit.on_limit_exceeded(fn(_) { "Stop!" }) 51 51 |> glimit.build 52 52 53 53 let func = ··· 65 65 glimit.new() 66 66 |> glimit.per_second(2) 67 67 |> glimit.identifier(fn(x) { x }) 68 - |> glimit.handler(fn(_) { "Stop!" }) 68 + |> glimit.on_limit_exceeded(fn(_) { "Stop!" }) 69 69 |> glimit.build 70 70 71 71 let func = ··· 89 89 let #(a, _) = i 90 90 a 91 91 }) 92 - |> glimit.handler(fn(_) { "Stop!" }) 92 + |> glimit.on_limit_exceeded(fn(_) { "Stop!" }) 93 93 |> glimit.build 94 94 95 95 let func = ··· 107 107 glimit.new() 108 108 |> glimit.per_second(2) 109 109 |> glimit.identifier(fn(_) { "id" }) 110 - |> glimit.handler(fn(_) { "Stop!" }) 110 + |> glimit.on_limit_exceeded(fn(_) { "Stop!" }) 111 111 |> glimit.build 112 112 113 113 let func = ··· 124 124 glimit.new() 125 125 |> glimit.per_second(2) 126 126 |> glimit.identifier(fn(_) { "id" }) 127 - |> glimit.handler(fn(_) { "Stop!" }) 127 + |> glimit.on_limit_exceeded(fn(_) { "Stop!" }) 128 128 |> glimit.build 129 129 130 130 let func = ··· 140 140 glimit.new() 141 141 |> glimit.per_second(2) 142 142 |> glimit.identifier(fn(x) { x }) 143 - |> glimit.handler(fn(x) { x }) 143 + |> glimit.on_limit_exceeded(fn(x) { x }) 144 144 |> glimit.try_build 145 145 |> should.be_ok() 146 146 } ··· 148 148 pub fn try_build_identifier_missing_test() { 149 149 glimit.new() 150 150 |> glimit.per_second(2) 151 - |> glimit.handler(fn(x) { x }) 151 + |> glimit.on_limit_exceeded(fn(x) { x }) 152 152 |> glimit.try_build 153 - |> should.equal(Error("Identifier function is required")) 153 + |> should.equal(Error("`identifier` function is required")) 154 154 } 155 155 156 - pub fn try_build_handler_missing_test() { 156 + pub fn try_build_on_limit_exceeded_missing_test() { 157 157 glimit.new() 158 158 |> glimit.per_second(2) 159 159 |> glimit.identifier(fn(x) { x }) 160 160 |> glimit.try_build 161 - |> should.equal(Error("Handler function is required")) 161 + |> should.equal(Error("`on_limit_exceeded` function is required")) 162 162 }