feat(notifications): send product notifications with a rights check right before each send

This commit is contained in:
shira committed 2026-10-09 20:38:39 +03:00
1 parent a856066316
commit 623d41b969
59 files changed
+4125 -1126

No files matched your search

+32 -3
View File
@@ -51,8 +51,12 @@ var (
// Send returned an error, the delivery's next state). The reservation
// journal makes a repeated settlement harmless.
type QuotaGate struct {
account domain.MailProviderAccount
limits domain.MailQuotaLimits
account domain.MailProviderAccount
limits domain.MailQuotaLimits
// reserveDaily and reserveMonthly are the recipients per window that this
// gate leaves free for other mail; see WithReserve.
reserveDaily int64
reserveMonthly int64
idempotencyWindow time.Duration
clock func() time.Time
logger *slog.Logger
@@ -74,6 +78,27 @@ func NewQuotaGate(
return &QuotaGate{account: account, limits: limits, idempotencyWindow: idempotencyWindow, clock: clock, logger: logger}
}
// WithReserve returns a gate that admits a send only while the windows keep
// the given recipients free after it: daily and monthly recipients that other
// mail may use. The service lane keeps the full limits, so a reserve held for
// service mail is never spent by product notifications. The reserve shapes
// admission only; a window that the provider reports used up is still marked
// against the full limit, so the provider's answer is never understated.
func (gate *QuotaGate) WithReserve(daily, monthly int64) (*QuotaGate, error) {
if _, err := gate.limits.WithReserve(daily, monthly); err != nil {
return nil, fmt.Errorf("QuotaGate.WithReserve - reserve: %w", err)
}
next := *gate
next.reserveDaily, next.reserveMonthly = daily, monthly
return &next, nil
}
// admission returns the limits that a send is admitted against: the limits
// less the reserve. It is computed at each decision from the current limits.
func (gate *QuotaGate) admission() (domain.MailQuotaLimits, error) {
return gate.limits.WithReserve(gate.reserveDaily, gate.reserveMonthly)
}
// QuotaPermit admits one send of one delivery. It holds the reservation, so
// a settlement after midnight still releases the windows the send was
// counted in.
@@ -142,6 +167,10 @@ func (gate *QuotaGate) PrepareSend(ctx context.Context, unit outbound.UnitOfWork
return SendDecision{}, fmt.Errorf("%s - delivery is %s: %w", location, delivery.Status(), domain.ErrDeliveryTransition)
}
now := gate.clock()
admission, err := gate.admission()
if err != nil {
return SendDecision{}, fmt.Errorf("%s - admission limits: %w", location, err)
}
reservationID, err := uuid.NewV7()
if err != nil {
return SendDecision{}, fmt.Errorf("%s - generate reservation ID: %w", location, err)
@@ -155,7 +184,7 @@ func (gate *QuotaGate) PrepareSend(ctx context.Context, unit outbound.UnitOfWork
Recipients: recipientsPerDelivery,
ReservedAt: now,
},
Limits: gate.limits,
Limits: admission,
}
if gate.idempotencyWindow > 0 {
request.ReuseSince = now.Add(-gate.idempotencyWindow)