LemonChannels.Outbox.RateLimiter (lemon_channels v0.1.0)

View Source

Rate limiter for outbound message delivery.

Enforces per-channel rate limits to avoid API throttling.

Summary

Functions

Check if a message can be sent (non-consuming check for initial queue decision).

Returns a specification to start this module under a supervisor.

Atomically check and consume a rate limit token.

Record that a message was sent (decrements token count).

Get the current rate limit status for a channel/account.

Functions

check(channel_id, account_id)

@spec check(channel_id :: binary(), account_id :: binary()) ::
  :ok | {:rate_limited, non_neg_integer()}

Check if a message can be sent (non-consuming check for initial queue decision).

Returns :ok if allowed, {:rate_limited, wait_ms} if rate limited. This does NOT consume a token - use consume/2 for that.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

consume(channel_id, account_id)

@spec consume(channel_id :: binary(), account_id :: binary()) ::
  :ok | {:rate_limited, non_neg_integer()}

Atomically check and consume a rate limit token.

Returns :ok if token was consumed, {:rate_limited, wait_ms} if rate limited. This is the primary function to use when actually sending a message.

record(channel_id, account_id)

@spec record(channel_id :: binary(), account_id :: binary()) :: :ok

Record that a message was sent (decrements token count).

start_link(opts \\ [])

status(channel_id, account_id)

@spec status(channel_id :: binary(), account_id :: binary()) :: map()

Get the current rate limit status for a channel/account.