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

+4
View File
@@ -86,6 +86,10 @@ type Config struct {
// pinned to. It is configuration identity, never a credential.
EmailAccount string
EmailTimeout time.Duration
// EmailQuotaDaily and EmailQuotaMonthly limit the recipients sent
// through the configured provider account per UTC day and UTC month.
EmailQuotaDaily int
EmailQuotaMonthly int
// AppPublicURL is the public origin without a trailing slash, for links
// in mail.
AppPublicURL string
+68
View File
@@ -46,6 +46,16 @@ const (
// delivery worker indefinitely.
minEmailTimeout = time.Second
maxEmailTimeout = time.Minute
// The defaults are the Resend Free plan limits checked on 2026-10-05:
// 100 recipients per UTC day and 3000 per month. They apply to resend
// and to disabled, which sends nothing. smtp has no default: a relay's
// limits are not Resend's, so the operator states them.
defaultEmailQuotaDaily = 100
defaultEmailQuotaMonthly = 3000
// maxEmailQuota is far above any plan this project can afford; a larger
// value is a typo, not a plan.
maxEmailQuota = 1_000_000_000
)
// emailAccountPattern matches domain.NewMailProviderAccount: a lowercase
@@ -73,6 +83,9 @@ func (config *Config) loadMail(lookup LookupEnv) error {
if err != nil {
return err
}
if err := config.loadMailQuota(lookup); err != nil {
return err
}
switch config.EmailProvider {
case EmailProviderResend:
@@ -85,6 +98,35 @@ func (config *Config) loadMail(lookup LookupEnv) error {
return nil
}
// loadMailQuota reads the quota of the configured provider account. The
// quota is read for every provider, so no transport can run without one.
// For smtp an unset value stays zero, which validation reports as missing.
//
// An empty value counts as unset, as for SMTP_PORT: Compose passes an unset
// variable as an empty string, and a Compose-side default would give smtp
// the Resend limits without the operator stating them.
func (config *Config) loadMailQuota(lookup LookupEnv) error {
daily, monthly := defaultEmailQuotaDaily, defaultEmailQuotaMonthly
if config.EmailProvider == EmailProviderSMTP {
daily, monthly = 0, 0
}
setOnly := func(key string) (string, bool) {
value, ok := lookup(key)
if strings.TrimSpace(value) == "" {
return "", false
}
return value, ok
}
var err error
if config.EmailQuotaDaily, err = intOrDefault(setOnly, "EMAIL_QUOTA_DAILY", daily); err != nil {
return err
}
if config.EmailQuotaMonthly, err = intOrDefault(setOnly, "EMAIL_QUOTA_MONTHLY", monthly); err != nil {
return err
}
return nil
}
func (config *Config) loadSMTP(lookup LookupEnv) error {
config.SMTPHost = strings.TrimSpace(valueOrDefault(lookup, "SMTP_HOST", ""))
config.SMTPUsername = strings.TrimSpace(valueOrDefault(lookup, "SMTP_USERNAME", ""))
@@ -128,6 +170,9 @@ func (config Config) ValidateMail() error {
if config.EmailTimeout < minEmailTimeout || config.EmailTimeout > maxEmailTimeout {
return fmt.Errorf("Config.ValidateMail - EMAIL_TIMEOUT must be between %s and %s", minEmailTimeout, maxEmailTimeout)
}
if err := config.validateMailQuota(); err != nil {
return err
}
switch config.EmailProvider {
case EmailProviderResend:
@@ -139,6 +184,29 @@ func (config Config) ValidateMail() error {
return nil
}
// validateMailQuota accepts limits from 1 to maxEmailQuota where the daily
// limit does not exceed the monthly one. There is no value that turns the
// quota off: zero is refused rather than read as "unlimited", so switching
// EMAIL_PROVIDER, for example to smtp, never skips the quota gate.
func (config Config) validateMailQuota() error {
for _, quota := range []struct {
name string
value int
}{{"EMAIL_QUOTA_DAILY", config.EmailQuotaDaily}, {"EMAIL_QUOTA_MONTHLY", config.EmailQuotaMonthly}} {
if quota.value >= 1 && quota.value <= maxEmailQuota {
continue
}
if config.EmailProvider == EmailProviderSMTP {
return fmt.Errorf("Config.validateMailQuota - %s must be set to an integer from 1 to %d when EMAIL_PROVIDER is smtp; the Resend defaults do not apply to a relay", quota.name, maxEmailQuota)
}
return fmt.Errorf("Config.validateMailQuota - %s must be an integer from 1 to %d", quota.name, maxEmailQuota)
}
if config.EmailQuotaDaily > config.EmailQuotaMonthly {
return errors.New("Config.validateMailQuota - EMAIL_QUOTA_DAILY must not exceed EMAIL_QUOTA_MONTHLY")
}
return nil
}
// validateEmailFrom accepts one RFC 5322 mailbox, with or without a display
// name. The address itself follows the account address rules of
// domain.NewEmailAddress: ASCII only, because the mailer does not support
+44
View File
@@ -37,6 +37,10 @@ func smtpValues(overrides map[string]string) map[string]string {
"SMTP_USERNAME": "mailer",
"SMTP_PASSWORD": testSMTPPassword,
"SMTP_TLS_MODE": "starttls",
// smtp has no quota default, so a relay always runs with stated
// limits.
"EMAIL_QUOTA_DAILY": "500",
"EMAIL_QUOTA_MONTHLY": "10000",
}
for key, value := range overrides {
values[key] = value
@@ -70,6 +74,9 @@ func TestLoadFromEnvMailDisabledIgnoresTransportSettings(t *testing.T) {
if settings.EmailAccount != "default" {
t.Fatalf("EmailAccount = %q, want the default label", settings.EmailAccount)
}
if settings.EmailQuotaDaily != 100 || settings.EmailQuotaMonthly != 3000 {
t.Fatalf("quota = %d/%d, want the Resend Free defaults 100/3000", settings.EmailQuotaDaily, settings.EmailQuotaMonthly)
}
if settings.ResendAPIKey != "" || settings.SMTPHost != "" || settings.SMTPPort != 0 || settings.SMTPPassword != "" || settings.SMTPTLSMode != "" {
t.Fatal("settings of an inactive transport were loaded")
}
@@ -92,6 +99,9 @@ func TestLoadFromEnvMailResendLoadsOnlyResendSettings(t *testing.T) {
if settings.SMTPHost != "" || settings.SMTPPassword != "" {
t.Fatal("SMTP settings were loaded for EMAIL_PROVIDER=resend")
}
if settings.EmailQuotaDaily != 100 || settings.EmailQuotaMonthly != 3000 {
t.Fatalf("quota = %d/%d, want the Resend Free defaults 100/3000", settings.EmailQuotaDaily, settings.EmailQuotaMonthly)
}
}
func TestLoadFromEnvMailSMTPLoadsOnlySMTPSettings(t *testing.T) {
@@ -109,6 +119,28 @@ func TestLoadFromEnvMailSMTPLoadsOnlySMTPSettings(t *testing.T) {
if settings.ResendAPIKey != "" {
t.Fatal("RESEND_API_KEY was loaded for EMAIL_PROVIDER=smtp")
}
if settings.EmailQuotaDaily != 500 || settings.EmailQuotaMonthly != 10000 {
t.Fatalf("quota = %d/%d, want the configured 500/10000", settings.EmailQuotaDaily, settings.EmailQuotaMonthly)
}
}
func TestLoadFromEnvMailQuotaOverridesTheDefaults(t *testing.T) {
// A lowered limit is taken as configured, for every provider.
for _, values := range []map[string]string{
{"EMAIL_PROVIDER": "resend", "RESEND_API_KEY": testResendAPIKey},
smtpValues(nil),
{"EMAIL_PROVIDER": "disabled"},
} {
values["EMAIL_QUOTA_DAILY"] = " 2 "
values["EMAIL_QUOTA_MONTHLY"] = "5"
settings, err := LoadFromEnv(mailEnvironment(values))
if err != nil {
t.Fatalf("%s: LoadFromEnv() error = %v", values["EMAIL_PROVIDER"], err)
}
if settings.EmailQuotaDaily != 2 || settings.EmailQuotaMonthly != 5 {
t.Fatalf("%s: quota = %d/%d, want 2/5", values["EMAIL_PROVIDER"], settings.EmailQuotaDaily, settings.EmailQuotaMonthly)
}
}
}
func TestLoadFromEnvAcceptsMailBoundaries(t *testing.T) {
@@ -128,6 +160,9 @@ func TestLoadFromEnvAcceptsMailBoundaries(t *testing.T) {
{name: "HTTP loopback IPv6", values: map[string]string{"APP_PUBLIC_URL": "http://[::1]:8080/"}},
{name: "SMTP over STARTTLS", values: smtpValues(map[string]string{"SMTP_TLS_MODE": "STARTTLS"})},
{name: "SMTP host as IP address", values: smtpValues(map[string]string{"SMTP_HOST": "10.0.0.25"})},
{name: "smallest quota", values: map[string]string{"EMAIL_QUOTA_DAILY": "1", "EMAIL_QUOTA_MONTHLY": "1"}},
{name: "largest quota", values: map[string]string{"EMAIL_QUOTA_DAILY": "1000000000", "EMAIL_QUOTA_MONTHLY": "1000000000"}},
{name: "empty quota takes the default", values: map[string]string{"EMAIL_QUOTA_DAILY": " ", "EMAIL_QUOTA_MONTHLY": ""}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
@@ -199,6 +234,15 @@ func TestLoadFromEnvRejectsMailSettings(t *testing.T) {
{name: "SMTP password unset", values: smtpValues(nil), unset: []string{"SMTP_PASSWORD"}, want: "Config.validateSMTP - SMTP_PASSWORD must be set when EMAIL_PROVIDER is smtp"},
{name: "SMTP TLS mode unset", values: smtpValues(nil), unset: []string{"SMTP_TLS_MODE"}, want: "Config.validateSMTP - SMTP_TLS_MODE must be starttls or tls"},
{name: "SMTP plaintext", values: smtpValues(map[string]string{"SMTP_TLS_MODE": "none"}), want: "Config.validateSMTP - SMTP_TLS_MODE must be starttls or tls; plaintext SMTP is not supported"},
{name: "SMTP daily quota unset", values: smtpValues(nil), unset: []string{"EMAIL_QUOTA_DAILY"}, want: "Config.validateMailQuota - EMAIL_QUOTA_DAILY must be set to an integer from 1 to 1000000000 when EMAIL_PROVIDER is smtp"},
{name: "SMTP monthly quota unset", values: smtpValues(nil), unset: []string{"EMAIL_QUOTA_MONTHLY"}, want: "Config.validateMailQuota - EMAIL_QUOTA_MONTHLY must be set to an integer from 1 to 1000000000 when EMAIL_PROVIDER is smtp"},
{name: "SMTP quota zero", values: smtpValues(map[string]string{"EMAIL_QUOTA_DAILY": "0"}), want: "Config.validateMailQuota - EMAIL_QUOTA_DAILY must be set to an integer from 1"},
{name: "daily quota zero", values: map[string]string{"EMAIL_QUOTA_DAILY": "0"}, want: "Config.validateMailQuota - EMAIL_QUOTA_DAILY must be an integer from 1 to 1000000000"},
{name: "daily quota negative", values: map[string]string{"EMAIL_QUOTA_DAILY": "-1"}, want: "Config.validateMailQuota - EMAIL_QUOTA_DAILY must be an integer from 1"},
{name: "monthly quota above the maximum", values: map[string]string{"EMAIL_QUOTA_MONTHLY": "1000000001"}, want: "Config.validateMailQuota - EMAIL_QUOTA_MONTHLY must be an integer from 1"},
{name: "daily quota not a number", values: map[string]string{"EMAIL_QUOTA_DAILY": "unlimited"}, want: "config.intOrDefault - EMAIL_QUOTA_DAILY must be an integer"},
{name: "SMTP quota empty as Compose passes it unset", values: smtpValues(map[string]string{"EMAIL_QUOTA_MONTHLY": ""}), want: "Config.validateMailQuota - EMAIL_QUOTA_MONTHLY must be set to an integer from 1"},
{name: "daily quota above the monthly quota", values: map[string]string{"EMAIL_QUOTA_DAILY": "3001"}, want: "Config.validateMailQuota - EMAIL_QUOTA_DAILY must not exceed EMAIL_QUOTA_MONTHLY"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {