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

+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