- Release removes the held message and queues its deliveries in one transaction - Of several simultaneous approvals one wins, the others get 404 - Extract the thread-root computation and test its linearity without the database (fast under -race)
183 lines
5.3 KiB
Go
183 lines
5.3 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// NewOutbound is one recipient to queue.
|
|
type NewOutbound struct {
|
|
From string // envelope sender (VERP address)
|
|
Rcpt string
|
|
MemberID int64 // 0 for system mail
|
|
Prefix []byte // per-recipient headers, sent before the shared message
|
|
}
|
|
|
|
// OutboxItem is a queued delivery to one recipient.
|
|
type OutboxItem struct {
|
|
ID int64
|
|
MsgID int64
|
|
From string
|
|
Rcpt string
|
|
MemberID int64
|
|
Prefix []byte
|
|
Attempts int
|
|
NextAt time.Time
|
|
LastError string
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// OutboxStats summarises the queue.
|
|
type OutboxStats struct {
|
|
Pending int // every queued delivery
|
|
Retrying int // deliveries that already failed at least once
|
|
Oldest time.Time // zero when the queue is empty
|
|
}
|
|
|
|
// Enqueue stores a message and one delivery per recipient, all or nothing.
|
|
func (s *Store) Enqueue(msg []byte, items []NewOutbound) error {
|
|
if len(items) == 0 {
|
|
return nil
|
|
}
|
|
tx, err := s.db.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if err := enqueueTx(tx, s.Now().Unix(), msg, items); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// EnqueueFromHeld releases a held message: it removes it from the moderation queue and queues
|
|
// its deliveries in one transaction. Of several simultaneous approvals only one succeeds, the
|
|
// others get ErrNotFound, so a message is never delivered twice. With no recipients the message
|
|
// is still released.
|
|
func (s *Store) EnqueueFromHeld(heldID int64, msg []byte, items []NewOutbound) error {
|
|
tx, err := s.db.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if err := affected(tx.Exec(`DELETE FROM held WHERE id=?`, heldID)); err != nil {
|
|
return err
|
|
}
|
|
if len(items) > 0 {
|
|
if err := enqueueTx(tx, s.Now().Unix(), msg, items); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
func enqueueTx(tx *sql.Tx, now int64, msg []byte, items []NewOutbound) error {
|
|
res, err := tx.Exec(`INSERT INTO outbox_msgs(data) VALUES(?)`, msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
msgID, _ := res.LastInsertId()
|
|
for _, it := range items {
|
|
prefix := it.Prefix
|
|
if prefix == nil {
|
|
prefix = []byte{}
|
|
}
|
|
if _, err := tx.Exec(`INSERT INTO outbox(msg_id, envelope_from, rcpt, member_id, prefix, next_at, created_at)
|
|
VALUES(?,?,?,?,?,?,?)`, msgID, it.From, it.Rcpt, it.MemberID, prefix, now, now); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
const outboxCols = `id, msg_id, envelope_from, rcpt, member_id, prefix, attempts, next_at, last_error, created_at`
|
|
|
|
func scanOutbox(row interface{ Scan(...any) error }) (OutboxItem, error) {
|
|
var it OutboxItem
|
|
var next, created int64
|
|
err := row.Scan(&it.ID, &it.MsgID, &it.From, &it.Rcpt, &it.MemberID, &it.Prefix, &it.Attempts, &next, &it.LastError, &created)
|
|
it.NextAt, it.CreatedAt = time.Unix(next, 0), time.Unix(created, 0)
|
|
return it, err
|
|
}
|
|
|
|
func (s *Store) queryOutbox(q string, args ...any) ([]OutboxItem, error) {
|
|
rows, err := s.db.Query(q, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []OutboxItem
|
|
for rows.Next() {
|
|
it, err := scanOutbox(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, it)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// DueOutbox returns up to limit deliveries whose time has come, longest waiting first.
|
|
func (s *Store) DueOutbox(limit int) ([]OutboxItem, error) {
|
|
return s.queryOutbox(`SELECT `+outboxCols+` FROM outbox WHERE next_at<=? ORDER BY next_at, id LIMIT ?`,
|
|
s.Now().Unix(), limit)
|
|
}
|
|
|
|
// ListOutbox returns up to limit deliveries for display, failing ones first.
|
|
func (s *Store) ListOutbox(limit int) ([]OutboxItem, error) {
|
|
return s.queryOutbox(`SELECT `+outboxCols+` FROM outbox ORDER BY attempts DESC, next_at, id LIMIT ?`, limit)
|
|
}
|
|
|
|
// OutboxData returns the shared message of a queued delivery.
|
|
func (s *Store) OutboxData(msgID int64) ([]byte, error) {
|
|
var b []byte
|
|
err := s.db.QueryRow(`SELECT data FROM outbox_msgs WHERE id=?`, msgID).Scan(&b)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return b, err
|
|
}
|
|
|
|
// CompleteOutbox removes a delivery (sent, dropped or discarded) and the shared
|
|
// message once no recipient needs it.
|
|
func (s *Store) CompleteOutbox(id int64) error {
|
|
tx, err := s.db.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
var msgID int64
|
|
err = tx.QueryRow(`SELECT msg_id FROM outbox WHERE id=?`, id).Scan(&msgID)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.Exec(`DELETE FROM outbox WHERE id=?`, id); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.Exec(`DELETE FROM outbox_msgs WHERE id=? AND NOT EXISTS (SELECT 1 FROM outbox WHERE msg_id=?)`, msgID, msgID); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// RetryOutbox records a failed attempt and schedules the next one.
|
|
func (s *Store) RetryOutbox(id int64, next time.Time, lastError string) error {
|
|
return affected(s.db.Exec(`UPDATE outbox SET attempts=attempts+1, next_at=?, last_error=? WHERE id=?`,
|
|
next.Unix(), lastError, id))
|
|
}
|
|
|
|
// OutboxStats returns queue totals.
|
|
func (s *Store) OutboxStats() (OutboxStats, error) {
|
|
var st OutboxStats
|
|
var oldest sql.NullInt64
|
|
err := s.db.QueryRow(`SELECT COUNT(*), COALESCE(SUM(attempts>0),0), MIN(created_at) FROM outbox`).
|
|
Scan(&st.Pending, &st.Retrying, &oldest)
|
|
if oldest.Valid {
|
|
st.Oldest = time.Unix(oldest.Int64, 0)
|
|
}
|
|
return st, err
|
|
}
|