GamendWeb.RateLimit (gamend_web v1.0.1216)

Copy Markdown View Source

Rate limiter facade used by GamendWeb.Plugs.RateLimiter, LiveView helpers, and channels.

Delegates to one of two Hammer-powered backends:

Configuration

config :gamend_web, GamendWeb.RateLimit,
  backend: :redis,
  redis: [url: "redis://localhost:6379"]

Selected at runtime via the RATE_LIMIT_BACKEND env var ("ets" or "redis"); the Redis URL falls back to RATE_LIMIT_REDIS_URL, CACHE_REDIS_URL, then REDIS_URL.

The configured backend is started in the host application supervision tree.

Summary

Functions

Returns the currently configured backend module.

Daily chat quota for one user (Gamend.Limits :max_chat_messages_per_day, rolling 24h window). Returns :ok or {:error, :chat_daily_limit}.

Daily chat-report quota for one user (Gamend.Limits :max_chat_reports_per_user_per_day, rolling 24h window). Returns :ok or {:error, :report_daily_limit}.

Functions

backend()

@spec backend() :: module()

Returns the currently configured backend module.

check_chat_daily(user_id)

@spec check_chat_daily(term()) :: :ok | {:error, :chat_daily_limit}

Daily chat quota for one user (Gamend.Limits :max_chat_messages_per_day, rolling 24h window). Returns :ok or {:error, :chat_daily_limit}.

Skipped when rate limiting is disabled or the limit is 0.

check_report_daily(user_id)

@spec check_report_daily(term()) :: :ok | {:error, :report_daily_limit}

Daily chat-report quota for one user (Gamend.Limits :max_chat_reports_per_user_per_day, rolling 24h window). Returns :ok or {:error, :report_daily_limit}.

Skipped when rate limiting is disabled or the limit is 0.

hit(key, scale, limit)

@spec hit(String.t(), pos_integer(), pos_integer()) ::
  {:allow, non_neg_integer()} | {:deny, non_neg_integer()}