-module(glimit). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/glimit.gleam"). -export([new/0, per_second/2, per_second_fn/2, burst_limit/2, burst_limit_fn/2, on_limit_exceeded/2, identifier/2, build/1, apply_built/2, apply/2, apply2/2, apply3/2, apply4/2]). -export_type([rate_limiter/3, rate_limiter_builder/3]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " This module provides a rate limiter that can be used to limit the number of\n" " requests or function calls per second for a given identifier.\n" "\n" " A single rate limiter actor stores all token bucket state. Each hit is a single\n" " message to the rate limiter, which performs the Token Bucket calculation inline.\n" " A periodic sweep removes idle (full) buckets to reduce memory usage. The\n" " rate limiter fails open — if the rate limiter actor is unavailable, requests are\n" " allowed through.\n" "\n" " The rate limits are configured using the following two options:\n" "\n" " - `per_second`: The rate of new available tokens per second. Think of this\n" " as the steady state rate limit.\n" " - `burst_limit`: The maximum number of available tokens. Think of this as\n" " the burst rate limit. The default value is the `per_second` rate limit.\n" "\n" " The rate limiter can be applied to a function or handler using the `apply`\n" " function, which returns a new function that checks the rate limit before\n" " calling the original function.\n" "\n" " # Example\n" "\n" " ```gleam\n" " import glimit\n" "\n" " let limiter =\n" " glimit.new()\n" " |> glimit.per_second(10)\n" " |> glimit.burst_limit(100)\n" " |> glimit.identifier(fn(request) { request.ip })\n" " |> glimit.on_limit_exceeded(fn(_request) { \"Rate limit reached\" })\n" "\n" " let handler =\n" " fn(_request) { \"Hello, world!\" }\n" " |> glimit.apply(limiter)\n" " ```\n" "\n" " # Multi-argument functions\n" "\n" " `apply` wraps a single-argument function `fn(a) -> b`. To rate-limit a\n" " function with multiple arguments, use `apply2`, `apply3`, or `apply4`:\n" "\n" " ```gleam\n" " let limiter =\n" " glimit.new()\n" " |> glimit.per_second(10)\n" " |> glimit.identifier(fn(args: #(String, String)) { args.0 })\n" " |> glimit.on_limit_exceeded(fn(_args) { too_many_requests() })\n" "\n" " let limited_handle =\n" " handle\n" " |> glimit.apply2(limiter)\n" "\n" " limited_handle(\"user_123\", \"upload\")\n" " ```\n" "\n" ). -type rate_limiter(FYS, FYT, FYU) :: {rate_limiter, gleam@erlang@process:subject(glimit@rate_limiter:message(FYU)), fun((FYS) -> FYT), fun((FYS) -> FYU)}. -type rate_limiter_builder(FYV, FYW, FYX) :: {rate_limiter_builder, gleam@option:option(fun((FYX) -> integer())), gleam@option:option(fun((FYX) -> integer())), gleam@option:option(fun((FYV) -> FYX)), gleam@option:option(fun((FYV) -> FYW))}. -file("src/glimit.gleam", 85). ?DOC(" Create a new rate limiter builder.\n"). -spec new() -> rate_limiter_builder(any(), any(), any()). new() -> {rate_limiter_builder, none, none, none, none}. -file("src/glimit.gleam", 113). ?DOC( " Set the rate of new available tokens per second.\n" "\n" " Note that this is not the maximum number of requests that can be made in a single\n" " second, but the rate at which tokens are added to the bucket. Think of this as the\n" " steady state rate limit, while the `burst_limit` function sets the maximum number of\n" " available tokens (or the burst rate limit).\n" "\n" " This value is also used as the default value for the `burst_limit` function.\n" "\n" " # Example\n" "\n" " ```gleam\n" " import glimit\n" "\n" " let limiter =\n" " glimit.new()\n" " |> glimit.per_second(10)\n" " ```\n" ). -spec per_second(rate_limiter_builder(FZE, FZF, FZG), integer()) -> rate_limiter_builder(FZE, FZF, FZG). per_second(Limiter, Limit) -> {rate_limiter_builder, {some, fun(_) -> Limit end}, erlang:element(3, Limiter), erlang:element(4, Limiter), erlang:element(5, Limiter)}. -file("src/glimit.gleam", 135). ?DOC( " Set the rate limit per second, based on the identifier.\n" "\n" " # Example\n" "\n" " ```gleam\n" " import glimit\n" "\n" " let limiter =\n" " glimit.new()\n" " |> glimit.identifier(fn(request) { request.user_id })\n" " |> glimit.per_second_fn(fn(user_id) {\n" " db.get_rate_limit(user_id)\n" " })\n" " ```\n" ). -spec per_second_fn( rate_limiter_builder(FZN, FZO, FZP), fun((FZP) -> integer()) ) -> rate_limiter_builder(FZN, FZO, FZP). per_second_fn(Limiter, Limit_fn) -> {rate_limiter_builder, {some, Limit_fn}, erlang:element(3, Limiter), erlang:element(4, Limiter), erlang:element(5, Limiter)}. -file("src/glimit.gleam", 158). ?DOC( " Set the maximum number of available tokens.\n" "\n" " The maximum number of available tokens is the maximum number of requests that can be\n" " made in a single second. The default value is the same as the rate limit per second.\n" "\n" " # Example\n" "\n" " ```gleam\n" " import glimit\n" "\n" " let limiter =\n" " glimit.new()\n" " |> glimit.per_second(10)\n" " |> glimit.burst_limit(100)\n" " ```\n" ). -spec burst_limit(rate_limiter_builder(FZW, FZX, FZY), integer()) -> rate_limiter_builder(FZW, FZX, FZY). burst_limit(Limiter, Burst_limit) -> {rate_limiter_builder, erlang:element(2, Limiter), {some, fun(_) -> Burst_limit end}, erlang:element(4, Limiter), erlang:element(5, Limiter)}. -file("src/glimit.gleam", 181). ?DOC( " Set the maximum number of available tokens, based on the identifier.\n" "\n" " # Example\n" "\n" " ```gleam\n" " import glimit\n" "\n" " let limiter =\n" " glimit.new()\n" " |> glimit.identifier(fn(request) { request.user_id })\n" " |> glimit.per_second(10)\n" " |> glimit.burst_limit_fn(fn(user_id) {\n" " db.get_burst_limit(user_id)\n" " })\n" " ```\n" ). -spec burst_limit_fn( rate_limiter_builder(GAF, GAG, GAH), fun((GAH) -> integer()) ) -> rate_limiter_builder(GAF, GAG, GAH). burst_limit_fn(Limiter, Burst_limit_fn) -> {rate_limiter_builder, erlang:element(2, Limiter), {some, Burst_limit_fn}, erlang:element(4, Limiter), erlang:element(5, Limiter)}. -file("src/glimit.gleam", 201). ?DOC( " Set the handler to be called when the rate limit is reached.\n" "\n" " # Example\n" "\n" " ```gleam\n" " import glimit\n" "\n" " let limiter =\n" " glimit.new()\n" " |> glimit.per_second(10)\n" " |> glimit.on_limit_exceeded(fn(_request) { \"Rate limit reached\" })\n" " ```\n" ). -spec on_limit_exceeded(rate_limiter_builder(GAO, GAP, GAQ), fun((GAO) -> GAP)) -> rate_limiter_builder(GAO, GAP, GAQ). on_limit_exceeded(Limiter, On_limit_exceeded) -> {rate_limiter_builder, erlang:element(2, Limiter), erlang:element(3, Limiter), erlang:element(4, Limiter), {some, On_limit_exceeded}}. -file("src/glimit.gleam", 220). ?DOC( " Set the identifier function to be used to identify the rate limit.\n" "\n" " # Example\n" "\n" " ```gleam\n" " import glimit\n" "\n" " let limiter =\n" " glimit.new()\n" " |> glimit.identifier(fn(request) { request.ip })\n" " ```\n" ). -spec identifier(rate_limiter_builder(GAX, GAY, GAZ), fun((GAX) -> GAZ)) -> rate_limiter_builder(GAX, GAY, GAZ). identifier(Limiter, Identifier) -> {rate_limiter_builder, erlang:element(2, Limiter), erlang:element(3, Limiter), {some, Identifier}, erlang:element(5, Limiter)}. -file("src/glimit.gleam", 236). ?DOC( " Build the rate limiter.\n" "\n" " Note that using `apply` will already build the rate limiter, so this function is\n" " only useful if you want to build the rate limiter manually and apply it to multiple\n" " functions.\n" "\n" " To apply the resulting rate limiter to a function or handler, use the `apply_built`\n" " function.\n" ). -spec build(rate_limiter_builder(GBG, GBH, GBI)) -> {ok, rate_limiter(GBG, GBH, GBI)} | {error, binary()}. build(Config) -> gleam@result:'try'(case erlang:element(2, Config) of {some, Per_second} -> {ok, Per_second}; none -> {error, <<"`per_second` rate limit is required"/utf8>>} end, fun(Per_second@1) -> Burst_limit@1 = case erlang:element(3, Config) of {some, Burst_limit} -> Burst_limit; none -> Per_second@1 end, gleam@result:'try'(case erlang:element(4, Config) of {some, Identifier} -> {ok, Identifier}; none -> {error, <<"`identifier` function is required"/utf8>>} end, fun(Identifier@1) -> gleam@result:'try'(case erlang:element(5, Config) of {some, On_limit_exceeded} -> {ok, On_limit_exceeded}; none -> {error, <<"`on_limit_exceeded` function is required"/utf8>>} end, fun(On_limit_exceeded@1) -> gleam@result:'try'( begin _pipe = glimit@rate_limiter:new( Per_second@1, Burst_limit@1 ), gleam@result:map_error( _pipe, fun(_) -> <<"Failed to start rate limiter"/utf8>> end ) end, fun(Rate_limiter_actor) -> {ok, {rate_limiter, Rate_limiter_actor, On_limit_exceeded@1, Identifier@1}} end ) end) end) end). -file("src/glimit.gleam", 288). ?DOC( " Apply the rate limiter to a request handler or function.\n" "\n" " This function is useful if you want to build the rate limiter manually using the\n" " `build` function.\n" ). -spec apply_built(fun((GBX) -> GBY), rate_limiter(GBX, GBY, any())) -> fun((GBX) -> GBY). apply_built(Func, Limiter) -> fun(Input) -> Identifier = (erlang:element(4, Limiter))(Input), case glimit@rate_limiter:hit(erlang:element(2, Limiter), Identifier) of {ok, nil} -> Func(Input); {error, rate_limited} -> (erlang:element(3, Limiter))(Input); {error, unavailable} -> Func(Input) end end. -file("src/glimit.gleam", 272). ?DOC( " Apply the rate limiter to a request handler or function.\n" "\n" " Panics if the rate limiter cannot be started or if the `identifier`\n" " function or `on_limit_exceeded` function is missing.\n" ). -spec apply(fun((GBR) -> GBS), rate_limiter_builder(GBR, GBS, any())) -> fun((GBR) -> GBS). apply(Func, Config) -> Limiter@1 = case build(Config) of {ok, Limiter} -> Limiter; {error, Message} -> erlang:error(#{gleam_error => panic, message => Message, file => <>, module => <<"glimit"/utf8>>, function => <<"apply"/utf8>>, line => 278}) end, apply_built(Func, Limiter@1). -file("src/glimit.gleam", 322). ?DOC( " Apply the rate limiter to a 2-argument function.\n" "\n" " The config's `identifier` and `on_limit_exceeded` receive a `#(a, b)` tuple.\n" "\n" " # Example\n" "\n" " ```gleam\n" " let limiter =\n" " glimit.new()\n" " |> glimit.per_second(10)\n" " |> glimit.identifier(fn(args: #(String, String)) { args.0 })\n" " |> glimit.on_limit_exceeded(fn(_) { \"Rate limited\" })\n" "\n" " let limited =\n" " handle\n" " |> glimit.apply2(limiter)\n" "\n" " limited(\"user_123\", \"upload\")\n" " ```\n" ). -spec apply2( fun((GCD, GCE) -> GCF), rate_limiter_builder({GCD, GCE}, GCF, any()) ) -> fun((GCD, GCE) -> GCF). apply2(Func, Config) -> Wrapped = begin _pipe = fun(Args) -> Func(erlang:element(1, Args), erlang:element(2, Args)) end, apply(_pipe, Config) end, fun(A, B) -> Wrapped({A, B}) end. -file("src/glimit.gleam", 352). ?DOC( " Apply the rate limiter to a 3-argument function.\n" "\n" " The config's `identifier` and `on_limit_exceeded` receive a `#(a, b, c)` tuple.\n" "\n" " # Example\n" "\n" " ```gleam\n" " let limiter =\n" " glimit.new()\n" " |> glimit.per_second(10)\n" " |> glimit.identifier(fn(args: #(String, String, Int)) { args.0 })\n" " |> glimit.on_limit_exceeded(fn(_) { \"Rate limited\" })\n" "\n" " let limited =\n" " handle\n" " |> glimit.apply3(limiter)\n" "\n" " limited(\"user_123\", \"upload\", 42)\n" " ```\n" ). -spec apply3( fun((GCK, GCL, GCM) -> GCN), rate_limiter_builder({GCK, GCL, GCM}, GCN, any()) ) -> fun((GCK, GCL, GCM) -> GCN). apply3(Func, Config) -> Wrapped = begin _pipe = fun(Args) -> Func( erlang:element(1, Args), erlang:element(2, Args), erlang:element(3, Args) ) end, apply(_pipe, Config) end, fun(A, B, C) -> Wrapped({A, B, C}) end. -file("src/glimit.gleam", 382). ?DOC( " Apply the rate limiter to a 4-argument function.\n" "\n" " The config's `identifier` and `on_limit_exceeded` receive a `#(a, b, c, d)` tuple.\n" "\n" " # Example\n" "\n" " ```gleam\n" " let limiter =\n" " glimit.new()\n" " |> glimit.per_second(10)\n" " |> glimit.identifier(fn(args: #(String, String, Int, Bool)) { args.0 })\n" " |> glimit.on_limit_exceeded(fn(_) { \"Rate limited\" })\n" "\n" " let limited =\n" " handle\n" " |> glimit.apply4(limiter)\n" "\n" " limited(\"user_123\", \"upload\", 42, True)\n" " ```\n" ). -spec apply4( fun((GCS, GCT, GCU, GCV) -> GCW), rate_limiter_builder({GCS, GCT, GCU, GCV}, GCW, any()) ) -> fun((GCS, GCT, GCU, GCV) -> GCW). apply4(Func, Config) -> Wrapped = begin _pipe = fun(Args) -> Func( erlang:element(1, Args), erlang:element(2, Args), erlang:element(3, Args), erlang:element(4, Args) ) end, apply(_pipe, Config) end, fun(A, B, C, D) -> Wrapped({A, B, C, D}) end.