Skip to content

Add rate limit mail interceptor with Redis-backed quota tracking#25

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/create-rate-limit-interceptor-emails
Draft

Add rate limit mail interceptor with Redis-backed quota tracking#25
Copilot wants to merge 3 commits into
mainfrom
copilot/create-rate-limit-interceptor-emails

Conversation

Copy link
Copy Markdown

Copilot AI commented May 11, 2026

Adds daily and monthly email sending quotas enforced via an ActionMailer interceptor, with quota state stored in Redis and an event hook for subscribers.

Configuration

Two new ENV vars control the limits (either or both can be set):

  • ACTION_MAILER_MAX_EMAILS_PER_DAY
  • ACTION_MAILER_MAX_EMAILS_PER_MONTH

The interceptor is only registered when at least one limit is configured.

New components

  • RateLimitMailInterceptor — checks Redis quota before each delivery; blocks the email (perform_deliveries = false), logs a WARN, and publishes an ActiveSupport::Notifications event when a limit is hit:

    ActiveSupport::Notifications.subscribe("decidim.pokecode.mail_quota_exceeded") do |*, payload|
      # payload: { period: :daily | :monthly, current_count:, limit:, recipients: }
    end
  • MailQuota — Redis model wrapping the quota counters. Keys are scoped by date (decidim:pokecode:mail_quota:daily:YYYY-MM-DD) and month (…:monthly:YYYY-MM) with EXPIREAT set to end-of-period so they self-clean. Exposes daily_count, monthly_count, and increment! (atomic via MULTI). Uses a class-level shared Redis connection from REDIS_URL.

Behavior

Daily limit is checked first; if clear, monthly is checked. Quota is only incremented when delivery proceeds.

Copilot AI linked an issue May 11, 2026 that may be closed by this pull request
7 tasks
Copilot AI changed the title [WIP] Add rate limit interceptor for emails Add rate limit mail interceptor with Redis-backed quota tracking May 11, 2026
Copilot AI requested a review from microstudi May 11, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create a rate limit interceptor for emails

2 participants