136 lines
5.4 KiB
Go
136 lines
5.4 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
)
|
|
|
|
// MailQuotaReservation is a number of recipients counted against the day and
|
|
// the month window of one provider account for one send of one delivery. ID
|
|
// names its journal row.
|
|
type MailQuotaReservation struct {
|
|
ID uuid.UUID
|
|
DeliveryID uuid.UUID
|
|
Account domain.MailProviderAccount
|
|
Windows domain.MailQuotaWindows
|
|
Recipients int64
|
|
ReservedAt time.Time
|
|
}
|
|
|
|
// MailQuotaReserveRequest asks to count Reservation, which carries a new ID
|
|
// and the reservation time.
|
|
type MailQuotaReserveRequest struct {
|
|
Reservation MailQuotaReservation
|
|
Limits domain.MailQuotaLimits
|
|
// ReuseSince enables reuse: an open reservation of the same delivery and
|
|
// account made at or after it is returned instead of counting again. The
|
|
// zero time disables reuse.
|
|
ReuseSince time.Time
|
|
}
|
|
|
|
// MailQuotaCheck reports which window limits a reservation would exceed. A
|
|
// reservation took place only when neither is exceeded.
|
|
type MailQuotaCheck struct {
|
|
DayExceeded bool
|
|
MonthExceeded bool
|
|
}
|
|
|
|
// Reserved reports that the recipients are counted against both windows.
|
|
func (check MailQuotaCheck) Reserved() bool { return !check.DayExceeded && !check.MonthExceeded }
|
|
|
|
// MailQuotaReserveResult is the outcome of Reserve. Reservation is the new
|
|
// or reused reservation when Check.Reserved(); it is zero otherwise.
|
|
type MailQuotaReserveResult struct {
|
|
Check MailQuotaCheck
|
|
Reservation MailQuotaReservation
|
|
// Reused reports an open reservation of an earlier attempt of the same
|
|
// delivery: nothing was counted again.
|
|
Reused bool
|
|
}
|
|
|
|
// MailQuotaOutcome is what a send under a reservation established, as the
|
|
// journal records it.
|
|
type MailQuotaOutcome string
|
|
|
|
const (
|
|
MailQuotaOutcomeAccepted MailQuotaOutcome = "accepted"
|
|
MailQuotaOutcomeUnknown MailQuotaOutcome = "unknown"
|
|
MailQuotaOutcomeRejected MailQuotaOutcome = "rejected"
|
|
// MailQuotaOutcomeNotSent means that Send returned an error: nothing left
|
|
// the process.
|
|
MailQuotaOutcomeNotSent MailQuotaOutcome = "not_sent"
|
|
)
|
|
|
|
// MailQuotaSettlement applies the result of a send to the counters.
|
|
type MailQuotaSettlement struct {
|
|
Reservation MailQuotaReservation
|
|
Outcome MailQuotaOutcome
|
|
// Release gives the reservation back, once: a repeated release of the
|
|
// same reservation changes nothing.
|
|
Release bool
|
|
// ExhaustedPeriod, when set, marks that window of ExhaustedWindows full
|
|
// up to ExhaustedLimit, because the provider refused for its quota.
|
|
ExhaustedPeriod domain.MailQuotaPeriod
|
|
ExhaustedWindows domain.MailQuotaWindows
|
|
ExhaustedLimit int64
|
|
At time.Time
|
|
}
|
|
|
|
// MailQuotaUsage is the usage of one account in a day and a month window. A
|
|
// zero time means that the provider has not refused in that window.
|
|
type MailQuotaUsage struct {
|
|
DayUsed int64
|
|
MonthUsed int64
|
|
DayProviderExhaustedAt time.Time
|
|
MonthProviderExhaustedAt time.Time
|
|
}
|
|
|
|
// MailQuotaUsageReader reads quota usage without locking, for startup
|
|
// reports, operators, and metrics.
|
|
type MailQuotaUsageReader interface {
|
|
// Usage returns zero usage for windows without a row.
|
|
Usage(ctx context.Context, account domain.MailProviderAccount, windows domain.MailQuotaWindows) (MailQuotaUsage, error)
|
|
|
|
// OtherAccountsWithUsage lists the other account labels of the same
|
|
// provider that have usage in the month starting at monthStart.
|
|
OtherAccountsWithUsage(ctx context.Context, account domain.MailProviderAccount, monthStart time.Time) ([]string, error)
|
|
}
|
|
|
|
// MailQuotaRepository counts the recipients sent through each provider
|
|
// account per UTC day and UTC month, with a journal of reservations. The
|
|
// writes are available only through UnitOfWork, because they lock rows until
|
|
// the transaction ends; callers commit before they call a transport, so a
|
|
// slow provider never holds the locks.
|
|
//
|
|
// Every write locks all the window rows it touches in one statement, in
|
|
// (period_kind, period_start) order, before it writes them or the journal,
|
|
// so two quota writers wait for each other on these rows in one direction
|
|
// only, whatever plan PostgreSQL chooses. Callers that lock other rows in
|
|
// the same transaction keep their own consistent order for those.
|
|
//
|
|
// The counters and the journal do not cascade from accounts or deliveries:
|
|
// the provider keeps counting messages whose recipient account was deleted.
|
|
type MailQuotaRepository interface {
|
|
MailQuotaUsageReader
|
|
|
|
// Reserve first looks for a reusable open reservation (see
|
|
// MailQuotaReserveRequest.ReuseSince) and returns it with Reused set,
|
|
// counting nothing. Otherwise it adds the recipients to the day and the
|
|
// month window when both stay within limits, and to neither otherwise,
|
|
// and journals the reservation. Concurrent reservations for the same
|
|
// account never exceed a limit together. A limit lowered below the
|
|
// current usage refuses every new reservation until the window resets.
|
|
Reserve(ctx context.Context, request MailQuotaReserveRequest) (MailQuotaReserveResult, error)
|
|
|
|
// Settle records the outcome in the journal, gives the recipients back
|
|
// when Release is set and the reservation is still open, and marks the
|
|
// exhausted window. It reports whether this call released the
|
|
// reservation. A window that the provider reported exhausted keeps its
|
|
// usage on release.
|
|
Settle(ctx context.Context, settlement MailQuotaSettlement) (released bool, err error)
|
|
}
|