Simplify rate limiter slide: strip to essential GenServer logic
Torey Heinz
committed Feb 22, 2026
commit 436ac6652cf6064f4d2a4f1c51c99b8b51859838
Showing 1
changed file with
5 additions
and 18 deletions
slides/03-real-world.html
+5
-18
| @@ | @@ -164,31 +164,18 @@ end |
| defmodule Marketing.EmailRateLimiter do | |
| use GenServer | |
| - | defstruct tokens: 0.0, rate: 100.0, |
| - | daily_remaining: 750_000, last_refill_ms: nil |
| + | defstruct tokens: 0.0, rate: 100.0 |
| # Public API — other code just calls this | |
| def ready?, do: GenServer.call(__MODULE__, :acquire) | |
| - | def init(_opts) do |
| - | state = %__MODULE__{last_refill_ms: now_ms()} |
| - | Process.send_after(self(), :refill, 1_000) |
| - | {:ok, state} |
| - | end |
| - | |
| - | # Synchronous check: can we send right now? |
| def handle_call(:acquire, _from, state) do | |
| state = refill(state) | |
| - | cond do |
| - | state.daily_remaining <= 1_000 -> |
| - | {:reply, {:not_ready, :exceeded_daily}, state} |
| - | state.tokens >= 1.0 -> |
| - | {:reply, {:ok, :token}, |
| - | %{state | tokens: state.tokens - 1.0, |
| - | daily_remaining: state.daily_remaining - 1}} |
| - | true -> |
| - | {:reply, {:not_ready, :exceeded_rate}, state} |
| + | if state.tokens >= 1.0 do |
| + | {:reply, {:ok, :token}, %{state | tokens: state.tokens - 1.0}} |
| + | else |
| + | {:reply, {:not_ready, :exceeded_rate}, state} |
| end | |
| end | |
| end | |