feat(notification): add durable mail delivery state

This commit is contained in:
shira committed 2026-10-06 10:52:47 +03:00
1 parent 391c8a6cec
commit 2db585fbff
41 files changed
+4903 -108

No files matched your search

@@ -0,0 +1,94 @@
package outbound
import (
"context"
"errors"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
var (
// ErrDeliveryNotFound reports that no delivery has the ID.
ErrDeliveryNotFound = errors.New("delivery not found")
// ErrDeliveryVersionConflict reports that RecordAttempt matched no row:
// another worker recorded an attempt first, or the delivery no longer
// exists. Nothing was written; read the delivery again.
ErrDeliveryVersionConflict = errors.New("delivery changed concurrently")
// ErrDeliveryContentImmutable reports a statement that tried to change
// the recipient, content, provider account, or idempotency key of a
// stored delivery, or a recorded attempt. The database refuses it; it
// signals a code defect.
ErrDeliveryContentImmutable = errors.New("delivery content is immutable")
// ErrDeliveryProviderPinned reports a statement that tried to change the
// provider account of a delivery after its first attempt. The database
// refuses it; domain.Delivery.Rebind refuses it first.
ErrDeliveryProviderPinned = errors.New("delivery provider account is pinned")
// ErrMailRequestNotFound reports that no mail request has the ID: it was
// already turned into a delivery, or its account was deleted.
ErrMailRequestNotFound = errors.New("mail request not found")
)
// DeliveryRepository stores mail deliveries and their attempts. UnitOfWork
// binds it to one transaction; an adapter may also provide it outside a
// transaction, where every call is its own statement. Errors never quote the
// recipient or the content.
type DeliveryRepository interface {
// Create stores a delivery built by domain.NewDelivery and reports
// created=true. When a delivery with the same ID exists, it stores
// nothing, keeps the stored content, and reports created=false. It
// returns an error wrapping ErrUserNotFound when the recipient account
// does not exist; the surrounding transaction is then aborted.
Create(ctx context.Context, delivery domain.Delivery) (created bool, err error)
// Get returns ErrDeliveryNotFound when no delivery has the ID, and an
// error wrapping domain.ErrMailContentIntegrity when the stored content
// no longer matches its hash.
Get(ctx context.Context, id uuid.UUID) (domain.Delivery, error)
// ListAttempts returns the recorded attempts in attempt order; an empty
// list for a delivery without attempts or an unknown ID.
ListAttempts(ctx context.Context, id uuid.UUID) ([]domain.DeliveryAttempt, error)
// RecordAttempt stores next and attempt as returned by
// domain.Delivery.RecordAttempt, atomically, when the stored version
// still equals the version before the attempt (next.Version() - 1).
// Otherwise it writes nothing and returns ErrDeliveryVersionConflict.
RecordAttempt(ctx context.Context, next domain.Delivery, attempt domain.DeliveryAttempt) error
// WaitForQuota stores next as returned by domain.Delivery.WaitForQuota,
// without an attempt record, when the stored version still equals
// next.Version() - 1. Otherwise it returns ErrDeliveryVersionConflict.
WaitForQuota(ctx context.Context, next domain.Delivery) error
// Rebind stores the provider account of next as returned by
// domain.Delivery.Rebind under the same version rule as WaitForQuota. A
// delivery that has an attempt meanwhile fails with
// ErrDeliveryVersionConflict; the database refuses a provider change
// after an attempt with ErrDeliveryProviderPinned.
Rebind(ctx context.Context, next domain.Delivery) error
}
// MailRequestRepository stores mail requests between the producer
// transaction and the consumer transaction that turns them into deliveries.
// Errors never quote the request text.
type MailRequestRepository interface {
// Create stores a request built by domain.NewMailRequest. It returns an
// error wrapping ErrUserNotFound when the recipient account does not
// exist; the surrounding transaction is then aborted.
Create(ctx context.Context, request domain.MailRequest) error
// Lock reads the request and locks it until the transaction ends. A
// concurrent caller waits and then gets ErrMailRequestNotFound when the
// first transaction deleted it.
Lock(ctx context.Context, id uuid.UUID) (domain.MailRequest, error)
// Delete removes the request; deleting a missing request is not an
// error.
Delete(ctx context.Context, id uuid.UUID) error
}
+5 -4
View File
@@ -59,7 +59,7 @@ var (
// therefore calls InvalidateActive before UpdateRegistration or Insert; the
// opposite order can deadlock against a concurrent confirmation. The only
// exception is DeleteUnverifiedRegisteredBefore, which locks the account
// before its tokens and sessions; it is safe because every lock it takes uses
// before its tokens, sessions, and deliveries; it is safe because every lock it takes uses
// SKIP LOCKED and never waits. A deadlock that still happens, for example
// between callers that ignore this order, aborts one transaction with
// ErrTransactionConflict, and the caller may retry it.
@@ -99,9 +99,10 @@ type UserRepository interface {
// DeleteUnverifiedRegisteredBefore deletes at most limit accounts that are
// unverified and were last registered before cutoff, oldest first, with
// their tokens and sessions, and returns how many it deleted. It never
// waits for a lock: an account whose row, token, or session is locked by
// another transaction is skipped and stays for the next run. A cleanup job
// their tokens, sessions, and mail deliveries, and returns how many it
// deleted. It never waits for a lock: an account whose row, token,
// session, or delivery is locked by another transaction is skipped and
// stays for the next run. A cleanup job
// repeats it until the result is below limit and runs again later.
DeleteUnverifiedRegisteredBefore(ctx context.Context, cutoff time.Time, limit int32) (int64, error)
}
@@ -50,6 +50,8 @@ type Transactor interface {
type UnitOfWork interface {
IdentityRepositories
Policies() PolicyRepository
Deliveries() DeliveryRepository
MailRequests() MailRequestRepository
Outbox() OutboxWriter
}