F4 lists real-time updates and the interface had none: an open inbox only changed when the user reloaded it. Events come from the transactional outbox rather than from an in-process callback, which is what the outbox is for (PLAN.md section 9.4): the row is written in the same transaction as the message, so an event can never describe a delivery that rolled back, and a crash between commit and publish loses nothing because the row is still there on restart. The relay claims rows with SKIP LOCKED so a second instance can help without publishing anything twice. The hub fans events out per mailbox, keyed by the mailbox the session resolved to, so one account's arrivals are never visible to another even though they share the process. A subscriber that stops reading is dropped rather than allowed to stall the relay; it reconciles on reconnect. The client reconnects with capped exponential backoff and treats every successful connect as a resync, refetching the folder. Without that, a socket that dropped for a minute would leave the list quietly stale, which is worse than having no real-time updates at all. The upgrade goes through a Next rewrite, not the API route handler: a route handler sees a finished request and cannot upgrade a connection. Verified against the running stack - the handshake returns 101 through the Next server, and a message delivered on port 25 arrives as a frame on an already-open socket without the page asking for it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"lambdamail/protocols/internal/application/port"
|
|
)
|
|
|
|
// OutboxRepository implements the transactional outbox against Postgres.
|
|
type OutboxRepository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewOutboxRepository(pool *pgxpool.Pool) *OutboxRepository {
|
|
return &OutboxRepository{pool: pool}
|
|
}
|
|
|
|
func (r *OutboxRepository) FetchUnpublished(ctx context.Context, limit int) ([]port.OutboxEvent, error) {
|
|
if limit <= 0 || limit > 500 {
|
|
limit = 100
|
|
}
|
|
|
|
// SKIP LOCKED so two instances of this service can both relay without
|
|
// either publishing the same event twice or waiting on the other.
|
|
rows, err := r.pool.Query(ctx, `
|
|
SELECT id, event_type, aggregate_id, payload
|
|
FROM domain_events_outbox
|
|
WHERE published_at IS NULL
|
|
ORDER BY id
|
|
LIMIT $1
|
|
FOR UPDATE SKIP LOCKED
|
|
`, limit)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("fetch outbox: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
events := []port.OutboxEvent{}
|
|
for rows.Next() {
|
|
var e port.OutboxEvent
|
|
if err := rows.Scan(&e.ID, &e.EventType, &e.AggregateID, &e.Payload); err != nil {
|
|
return nil, err
|
|
}
|
|
events = append(events, e)
|
|
}
|
|
return events, rows.Err()
|
|
}
|
|
|
|
func (r *OutboxRepository) MarkPublished(ctx context.Context, ids []int64) error {
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
_, err := r.pool.Exec(ctx,
|
|
`UPDATE domain_events_outbox SET published_at = NOW() WHERE id = ANY($1)`, ids)
|
|
return err
|
|
}
|