feat(notification): add Resend quota gate and waiting-for-reset state

This commit is contained in:
shira committed 2026-10-06 11:50:38 +03:00
1 parent 3b917befa9
commit b856d4ff54
40 files changed
+4116 -48

No files matched your search

@@ -7,6 +7,7 @@ import (
"strings"
"time"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
@@ -98,9 +99,10 @@ type providerAnswer struct {
// have been accepted. Repeating cannot succeed and a new key could send
// the message twice: the delivery layer stops and escalates on this code.
// - 409 without a known name: unknown, since it may be either of the above.
// - 429 daily_quota_exceeded: quota_exhausted until the next midnight UTC;
// 429 monthly_quota_exceeded: quota_exhausted without a wait hint, the
// reset depends on the billing cycle; other 429: rate_limited with
// - 429 daily_quota_exceeded: quota_exhausted with QuotaPeriod day until
// the next midnight UTC; 429 monthly_quota_exceeded: quota_exhausted
// with QuotaPeriod month and without a wait hint, the reset depends on
// the billing cycle; other 429: rate_limited with
// Retry-After or RateLimit-Reset.
// - other 4xx (validation, key, permission, not found): invalid_request.
// - 503 service_unavailable: rejected temporary; the API refused the
@@ -155,9 +157,11 @@ func classifyTooManyRequests(name string, header http.Header, now time.Time) out
switch name {
case errorNameDailyQuota:
// The daily quota resets at midnight UTC, not after a rolling day.
return rejected(outbound.MailRejectionQuotaExhausted, untilNextUTCMidnight(now))
return quotaExhausted(domain.MailQuotaPeriodDay, untilNextUTCMidnight(now))
case errorNameMonthlyQuota:
return rejected(outbound.MailRejectionQuotaExhausted, 0)
// The monthly reset follows the account's billing cycle, which the
// API does not report.
return quotaExhausted(domain.MailQuotaPeriodMonth, 0)
default:
return rejected(outbound.MailRejectionRateLimited, rateLimitWait(header, now))
}
@@ -167,6 +171,12 @@ func rejected(reason outbound.MailRejection, wait time.Duration) outbound.SendRe
return outbound.SendResult{Outcome: outbound.MailOutcomeRejected, Rejection: reason, RetryAfter: wait}
}
func quotaExhausted(period domain.MailQuotaPeriod, wait time.Duration) outbound.SendResult {
result := rejected(outbound.MailRejectionQuotaExhausted, wait)
result.QuotaPeriod = period
return result
}
func unknownResult(wait time.Duration) outbound.SendResult {
return outbound.SendResult{Outcome: outbound.MailOutcomeUnknown, RetryAfter: wait}
}
@@ -65,6 +65,13 @@ type Sender struct {
const redactionMarker = "[redacted Resend API key]"
// IdempotencyWindow is how long Resend keeps an Idempotency-Key and answers
// a repeated request under it with the original result instead of sending
// again: 24 hours, per the Resend API documentation. Bootstrap passes it to
// notification.NewQuotaGate, so a retry inside the window does not count
// against the quota a second time.
const IdempotencyWindow = 24 * time.Hour
// credential holds the Authorization header value. It redacts itself in fmt
// and slog output in case it is ever printed directly.
type credential struct {
@@ -213,6 +213,13 @@ func TestSendClassifiesProviderAnswers(t *testing.T) {
rejectedAs := func(reason outbound.MailRejection, wait time.Duration, code outbound.MailProviderCode) outbound.SendResult {
return outbound.SendResult{Outcome: outbound.MailOutcomeRejected, Rejection: reason, RetryAfter: wait, ProviderCode: code}
}
// quotaAs names the window, so the quota gate need not guess it from
// the wait.
quotaAs := func(period domain.MailQuotaPeriod, wait time.Duration, code outbound.MailProviderCode) outbound.SendResult {
result := rejectedAs(outbound.MailRejectionQuotaExhausted, wait, code)
result.QuotaPeriod = period
return result
}
unknown := outbound.SendResult{Outcome: outbound.MailOutcomeUnknown}
unknownAs := func(wait time.Duration, code outbound.MailProviderCode) outbound.SendResult {
return outbound.SendResult{Outcome: outbound.MailOutcomeUnknown, RetryAfter: wait, ProviderCode: code}
@@ -243,8 +250,8 @@ func TestSendClassifiesProviderAnswers(t *testing.T) {
{name: "429 rate limit with an HTTP date", respond: respondJSON(http.StatusTooManyRequests, providerError(429, "rate_limit_exceeded"), map[string]string{"Retry-After": fixedNow.Add(90 * time.Second).Format(http.TimeFormat)}), want: rejectedAs(outbound.MailRejectionRateLimited, 90*time.Second, "rate_limit_exceeded")},
{name: "429 rate limit with a huge Retry-After", respond: respondJSON(http.StatusTooManyRequests, providerError(429, "rate_limit_exceeded"), map[string]string{"Retry-After": "99999999999"}), want: rejectedAs(outbound.MailRejectionRateLimited, 24*time.Hour, "rate_limit_exceeded")},
{name: "429 without a body", respond: respondJSON(http.StatusTooManyRequests, ``, nil), want: rejectedAs(outbound.MailRejectionRateLimited, 0, "")},
{name: "429 daily quota", respond: respondJSON(http.StatusTooManyRequests, providerError(429, "daily_quota_exceeded"), map[string]string{"Retry-After": "1"}), want: rejectedAs(outbound.MailRejectionQuotaExhausted, 150*time.Minute, "daily_quota_exceeded")},
{name: "429 monthly quota", respond: respondJSON(http.StatusTooManyRequests, providerError(429, "monthly_quota_exceeded"), nil), want: rejectedAs(outbound.MailRejectionQuotaExhausted, 0, "monthly_quota_exceeded")},
{name: "429 daily quota", respond: respondJSON(http.StatusTooManyRequests, providerError(429, "daily_quota_exceeded"), map[string]string{"Retry-After": "1"}), want: quotaAs(domain.MailQuotaPeriodDay, 150*time.Minute, "daily_quota_exceeded")},
{name: "429 monthly quota", respond: respondJSON(http.StatusTooManyRequests, providerError(429, "monthly_quota_exceeded"), nil), want: quotaAs(domain.MailQuotaPeriodMonth, 0, "monthly_quota_exceeded")},
{name: "500 application error", respond: respondJSON(http.StatusInternalServerError, providerError(500, "application_error"), map[string]string{"Retry-After": "5"}), want: unknownAs(0, "application_error")},
{name: "503 service unavailable", respond: respondJSON(http.StatusServiceUnavailable, providerError(503, "service_unavailable"), map[string]string{"Retry-After": "30"}), want: rejectedAs(outbound.MailRejectionTemporary, 30*time.Second, "service_unavailable")},
{name: "401 invalid API key", respond: respondJSON(http.StatusUnauthorized, providerError(401, "invalid_api_key"), nil), want: rejectedAs(outbound.MailRejectionInvalidRequest, 0, "invalid_api_key")},