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

+112
View File
@@ -0,0 +1,112 @@
package domain
import (
"errors"
"fmt"
"time"
)
// ErrInvalidMailQuota reports quota limits or a quota window that break a
// quota rule.
var ErrInvalidMailQuota = errors.New("invalid mail quota")
// MailQuotaPeriod is a quota window kind. Both windows are UTC calendar
// periods, as the Resend Free plan counts them: a day starts at 00:00 UTC and
// a month on its first day at 00:00 UTC.
type MailQuotaPeriod string
const (
MailQuotaPeriodDay MailQuotaPeriod = "day"
MailQuotaPeriodMonth MailQuotaPeriod = "month"
)
// MailQuotaLimits are the recipients that one provider account may be sent
// per UTC day and per UTC calendar month. Every recipient counts, as
// providers count them. The values come from configuration, never from
// domain constants, because they follow the provider plan of the stand.
type MailQuotaLimits struct {
daily int64
monthly int64
}
// NewMailQuotaLimits accepts positive limits where the daily limit does not
// exceed the monthly one. A zero limit is refused rather than read as
// "unlimited": no setting turns the quota off.
func NewMailQuotaLimits(daily, monthly int64) (MailQuotaLimits, error) {
switch {
case daily <= 0 || monthly <= 0:
return MailQuotaLimits{}, fmt.Errorf("domain.NewMailQuotaLimits - limits must be positive: %w", ErrInvalidMailQuota)
case daily > monthly:
return MailQuotaLimits{}, fmt.Errorf("domain.NewMailQuotaLimits - the daily limit must not exceed the monthly limit: %w", ErrInvalidMailQuota)
}
return MailQuotaLimits{daily: daily, monthly: monthly}, nil
}
func (limits MailQuotaLimits) IsZero() bool { return limits.daily == 0 }
func (limits MailQuotaLimits) Daily() int64 { return limits.daily }
func (limits MailQuotaLimits) Monthly() int64 { return limits.monthly }
// Of returns the limit of a period; zero for an unknown period.
func (limits MailQuotaLimits) Of(period MailQuotaPeriod) int64 {
switch period {
case MailQuotaPeriodDay:
return limits.daily
case MailQuotaPeriodMonth:
return limits.monthly
default:
return 0
}
}
// MailQuotaWindows are the UTC day and the UTC calendar month that contain
// one instant. A reservation counts against both.
type MailQuotaWindows struct {
dayStart time.Time
monthStart time.Time
}
// MailQuotaWindowsAt returns the windows that contain at, in UTC whatever
// the location of at.
func MailQuotaWindowsAt(at time.Time) MailQuotaWindows {
utc := at.UTC()
return MailQuotaWindows{
dayStart: time.Date(utc.Year(), utc.Month(), utc.Day(), 0, 0, 0, 0, time.UTC),
monthStart: time.Date(utc.Year(), utc.Month(), 1, 0, 0, 0, 0, time.UTC),
}
}
func (windows MailQuotaWindows) IsZero() bool { return windows.dayStart.IsZero() }
// DayStart is 00:00 UTC of the day.
func (windows MailQuotaWindows) DayStart() time.Time { return windows.dayStart }
// MonthStart is 00:00 UTC of the first day of the month.
func (windows MailQuotaWindows) MonthStart() time.Time { return windows.monthStart }
// Start returns the start of a period; the zero time for an unknown period.
func (windows MailQuotaWindows) Start(period MailQuotaPeriod) time.Time {
switch period {
case MailQuotaPeriodDay:
return windows.dayStart
case MailQuotaPeriodMonth:
return windows.monthStart
default:
return time.Time{}
}
}
// ResetAt returns when a period ends and its count starts again from zero:
// the next 00:00 UTC for the day, the first day of the next month at 00:00
// UTC for the month. The zero time for an unknown period.
func (windows MailQuotaWindows) ResetAt(period MailQuotaPeriod) time.Time {
switch period {
case MailQuotaPeriodDay:
return windows.dayStart.AddDate(0, 0, 1)
case MailQuotaPeriodMonth:
return windows.monthStart.AddDate(0, 1, 0)
default:
return time.Time{}
}
}
+120
View File
@@ -0,0 +1,120 @@
package domain
import (
"errors"
"testing"
"time"
)
func TestMailQuotaWindowsAtUsesUTCCalendarPeriods(t *testing.T) {
moscow := time.FixedZone("UTC+3", 3*60*60)
tests := []struct {
name string
at time.Time
dayStart, monthStart time.Time
dayReset, monthReset time.Time
}{
{
name: "middle of a day",
at: time.Date(2026, 10, 6, 12, 30, 0, 0, time.UTC),
dayStart: time.Date(2026, 10, 6, 0, 0, 0, 0, time.UTC),
monthStart: time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC),
dayReset: time.Date(2026, 10, 7, 0, 0, 0, 0, time.UTC),
monthReset: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "exactly at midnight UTC belongs to the new day",
at: time.Date(2026, 10, 7, 0, 0, 0, 0, time.UTC),
dayStart: time.Date(2026, 10, 7, 0, 0, 0, 0, time.UTC),
monthStart: time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC),
dayReset: time.Date(2026, 10, 8, 0, 0, 0, 0, time.UTC),
monthReset: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "one nanosecond before midnight UTC belongs to the old day",
at: time.Date(2026, 10, 6, 23, 59, 59, 999999999, time.UTC),
dayStart: time.Date(2026, 10, 6, 0, 0, 0, 0, time.UTC),
monthStart: time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC),
dayReset: time.Date(2026, 10, 7, 0, 0, 0, 0, time.UTC),
monthReset: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "local time after local midnight is still the previous UTC day",
at: time.Date(2026, 11, 1, 1, 0, 0, 0, moscow),
dayStart: time.Date(2026, 10, 31, 0, 0, 0, 0, time.UTC),
monthStart: time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC),
dayReset: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC),
monthReset: time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "leap day",
at: time.Date(2028, 2, 29, 18, 0, 0, 0, time.UTC),
dayStart: time.Date(2028, 2, 29, 0, 0, 0, 0, time.UTC),
monthStart: time.Date(2028, 2, 1, 0, 0, 0, 0, time.UTC),
dayReset: time.Date(2028, 3, 1, 0, 0, 0, 0, time.UTC),
monthReset: time.Date(2028, 3, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "February of a common year",
at: time.Date(2027, 2, 28, 18, 0, 0, 0, time.UTC),
dayStart: time.Date(2027, 2, 28, 0, 0, 0, 0, time.UTC),
monthStart: time.Date(2027, 2, 1, 0, 0, 0, 0, time.UTC),
dayReset: time.Date(2027, 3, 1, 0, 0, 0, 0, time.UTC),
monthReset: time.Date(2027, 3, 1, 0, 0, 0, 0, time.UTC),
},
{
name: "December resets into January of the next year",
at: time.Date(2026, 12, 31, 23, 0, 0, 0, time.UTC),
dayStart: time.Date(2026, 12, 31, 0, 0, 0, 0, time.UTC),
monthStart: time.Date(2026, 12, 1, 0, 0, 0, 0, time.UTC),
dayReset: time.Date(2027, 1, 1, 0, 0, 0, 0, time.UTC),
monthReset: time.Date(2027, 1, 1, 0, 0, 0, 0, time.UTC),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
windows := MailQuotaWindowsAt(test.at)
if !windows.DayStart().Equal(test.dayStart) || windows.DayStart().Location() != time.UTC {
t.Fatalf("DayStart() = %s, want %s in UTC", windows.DayStart(), test.dayStart)
}
if !windows.MonthStart().Equal(test.monthStart) || windows.Start(MailQuotaPeriodMonth) != windows.MonthStart() {
t.Fatalf("MonthStart() = %s, want %s", windows.MonthStart(), test.monthStart)
}
if got := windows.ResetAt(MailQuotaPeriodDay); !got.Equal(test.dayReset) {
t.Fatalf("ResetAt(day) = %s, want %s", got, test.dayReset)
}
if got := windows.ResetAt(MailQuotaPeriodMonth); !got.Equal(test.monthReset) {
t.Fatalf("ResetAt(month) = %s, want %s", got, test.monthReset)
}
// The reset instant opens the next window.
if next := MailQuotaWindowsAt(test.dayReset); !next.DayStart().Equal(test.dayReset) {
t.Fatalf("the day reset %s does not start the next day window", test.dayReset)
}
})
}
}
func TestNewMailQuotaLimits(t *testing.T) {
limits, err := NewMailQuotaLimits(100, 3000)
if err != nil {
t.Fatalf("NewMailQuotaLimits() error = %v", err)
}
if limits.Of(MailQuotaPeriodDay) != 100 || limits.Of(MailQuotaPeriodMonth) != 3000 || limits.Of("week") != 0 {
t.Fatalf("limits = %d/%d", limits.Daily(), limits.Monthly())
}
if _, err := NewMailQuotaLimits(3000, 3000); err != nil {
t.Fatalf("equal limits: error = %v", err)
}
for name, values := range map[string][2]int64{
"zero daily": {0, 3000},
"zero monthly": {100, 0},
"negative": {-1, 3000},
"daily above monthly": {3001, 3000},
} {
t.Run(name, func(t *testing.T) {
if _, err := NewMailQuotaLimits(values[0], values[1]); !errors.Is(err, ErrInvalidMailQuota) {
t.Fatalf("NewMailQuotaLimits(%d, %d) error = %v, want ErrInvalidMailQuota", values[0], values[1], err)
}
})
}
}