1305 lines
37 KiB
Go
1305 lines
37 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/redis/go-redis/v9"
|
|
"golang.org/x/sync/singleflight"
|
|
|
|
"messenger/internal/auth"
|
|
"messenger/internal/model"
|
|
"messenger/internal/s3store"
|
|
)
|
|
|
|
type cachedSession struct {
|
|
User model.User `json:"user"`
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
}
|
|
|
|
type PostgresStore struct {
|
|
pool *pgxpool.Pool
|
|
rdb *redis.Client
|
|
queryTimeout time.Duration
|
|
|
|
sfGroup singleflight.Group
|
|
|
|
mu sync.RWMutex
|
|
cacheMode string
|
|
writeMode string
|
|
|
|
sqlQueries atomic.Int64
|
|
cacheHits atomic.Int64
|
|
cacheMisses atomic.Int64
|
|
singleflightShared atomic.Int64
|
|
}
|
|
|
|
type Config struct {
|
|
DatabaseURL string
|
|
CacheURL string
|
|
MaxConns int32
|
|
MinConns int32
|
|
QueryTimeout time.Duration
|
|
CacheMode string
|
|
WriteMode string
|
|
}
|
|
|
|
func NewPostgresStore(ctx context.Context, cfg Config) (*PostgresStore, error) {
|
|
poolCfg, err := pgxpool.ParseConfig(cfg.DatabaseURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid database URL: %w", err)
|
|
}
|
|
|
|
if cfg.MaxConns <= 0 {
|
|
cfg.MaxConns = 12
|
|
}
|
|
if cfg.MinConns <= 0 {
|
|
cfg.MinConns = 2
|
|
}
|
|
if cfg.QueryTimeout <= 0 {
|
|
cfg.QueryTimeout = 5 * time.Second
|
|
}
|
|
if cfg.CacheMode == "" {
|
|
cfg.CacheMode = "write_through_versioned"
|
|
}
|
|
if cfg.WriteMode == "" {
|
|
cfg.WriteMode = "pipeline"
|
|
}
|
|
|
|
poolCfg.MaxConns = cfg.MaxConns
|
|
poolCfg.MinConns = cfg.MinConns
|
|
poolCfg.MaxConnLifetime = 30 * time.Minute
|
|
poolCfg.MaxConnIdleTime = 5 * time.Minute
|
|
poolCfg.HealthCheckPeriod = 15 * time.Second
|
|
poolCfg.ConnConfig.ConnectTimeout = 3 * time.Second
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, poolCfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create pgx pool: %w", err)
|
|
}
|
|
|
|
var rdb *redis.Client
|
|
if cfg.CacheURL != "" {
|
|
opt, err := redis.ParseURL(cfg.CacheURL)
|
|
if err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("invalid cache URL: %w", err)
|
|
}
|
|
opt.DialTimeout = 500 * time.Millisecond
|
|
opt.ReadTimeout = 500 * time.Millisecond
|
|
opt.WriteTimeout = 500 * time.Millisecond
|
|
opt.PoolSize = 30
|
|
opt.MaxRetries = 1
|
|
rdb = redis.NewClient(opt)
|
|
}
|
|
|
|
return &PostgresStore{
|
|
pool: pool,
|
|
rdb: rdb,
|
|
queryTimeout: cfg.QueryTimeout,
|
|
cacheMode: cfg.CacheMode,
|
|
writeMode: cfg.WriteMode,
|
|
}, nil
|
|
}
|
|
|
|
func (s *PostgresStore) RunMigrations(ctx context.Context, migrations []MigrationFile) error {
|
|
qCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
|
|
defer cancel()
|
|
|
|
tx, err := s.pool.Begin(qCtx)
|
|
if err != nil {
|
|
return fmt.Errorf("begin migration tx: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback(qCtx) }()
|
|
|
|
// Serialize concurrent migration runners via PostgreSQL advisory transaction lock.
|
|
if _, err := tx.Exec(qCtx, `SELECT pg_advisory_xact_lock(20260301)`); err != nil {
|
|
return fmt.Errorf("acquire migration advisory lock: %w", err)
|
|
}
|
|
|
|
_, err = tx.Exec(qCtx, `
|
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
version VARCHAR(64) PRIMARY KEY,
|
|
applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
);
|
|
`)
|
|
if err != nil {
|
|
return fmt.Errorf("create schema_migrations: %w", err)
|
|
}
|
|
|
|
for _, mig := range migrations {
|
|
var alreadyApplied bool
|
|
err = tx.QueryRow(qCtx, `SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version = $1)`, mig.Version).Scan(&alreadyApplied)
|
|
if err != nil {
|
|
return fmt.Errorf("check migration version %s: %w", mig.Version, err)
|
|
}
|
|
|
|
if !alreadyApplied {
|
|
if _, err := tx.Exec(qCtx, mig.SQL); err != nil {
|
|
return fmt.Errorf("apply migration %s: %w", mig.Version, err)
|
|
}
|
|
if _, err := tx.Exec(qCtx, `INSERT INTO schema_migrations (version, applied_at) VALUES ($1, NOW())`, mig.Version); err != nil {
|
|
return fmt.Errorf("record migration %s: %w", mig.Version, err)
|
|
}
|
|
log.Printf("Migration %s applied successfully", mig.Version)
|
|
} else {
|
|
log.Printf("Migration %s already applied (idempotent skip)", mig.Version)
|
|
}
|
|
}
|
|
|
|
return tx.Commit(qCtx)
|
|
}
|
|
|
|
func (s *PostgresStore) Ready(ctx context.Context) error {
|
|
qCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
|
|
var ok int
|
|
if err := s.pool.QueryRow(qCtx, `SELECT 1 FROM schema_migrations WHERE version = '003_attachments.sql' LIMIT 1`).Scan(&ok); err != nil {
|
|
return ErrUnavailable
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *PostgresStore) Close() {
|
|
if s.rdb != nil {
|
|
_ = s.rdb.Close()
|
|
}
|
|
if s.pool != nil {
|
|
s.pool.Close()
|
|
}
|
|
}
|
|
|
|
func (s *PostgresStore) getCacheMode() string {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
return s.cacheMode
|
|
}
|
|
|
|
func (s *PostgresStore) SetCacheMode(mode string) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.cacheMode = mode
|
|
}
|
|
|
|
func (s *PostgresStore) ResetStats() {
|
|
s.sqlQueries.Store(0)
|
|
s.cacheHits.Store(0)
|
|
s.cacheMisses.Store(0)
|
|
s.singleflightShared.Store(0)
|
|
}
|
|
|
|
func (s *PostgresStore) Stats(ctx context.Context) StoreStats {
|
|
redisOK := false
|
|
if s.rdb != nil {
|
|
rCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
|
|
defer cancel()
|
|
if err := s.rdb.Ping(rCtx).Err(); err == nil {
|
|
redisOK = true
|
|
}
|
|
}
|
|
return StoreStats{
|
|
SQLQueries: s.sqlQueries.Load(),
|
|
CacheHits: s.cacheHits.Load(),
|
|
CacheMisses: s.cacheMisses.Load(),
|
|
SingleflightShared: s.singleflightShared.Load(),
|
|
CacheMode: s.getCacheMode(),
|
|
WriteMode: s.writeMode,
|
|
RedisConnected: redisOK,
|
|
}
|
|
}
|
|
|
|
func classifyDBError(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
var pgErr *pgconn.PgError
|
|
if errors.As(err, &pgErr) {
|
|
switch pgErr.Code {
|
|
case "23505": // unique_violation
|
|
return fmt.Errorf("%w: resource already exists", ErrConflict)
|
|
case "23503": // foreign_key_violation
|
|
return fmt.Errorf("%w: referenced entity does not exist", ErrBadRequest)
|
|
case "23514": // check_violation
|
|
return fmt.Errorf("%w: check constraint failed", ErrBadRequest)
|
|
case "22P02": // invalid_text_representation (e.g. bad UUID)
|
|
return fmt.Errorf("%w: invalid identifier format", ErrBadRequest)
|
|
}
|
|
}
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrNotFound
|
|
}
|
|
return fmt.Errorf("%w: database operation failed", ErrUnavailable)
|
|
}
|
|
|
|
func (s *PostgresStore) CreateUser(ctx context.Context, req model.CreateUserRequest) (*model.User, error) {
|
|
if !usernameRegex.MatchString(req.Username) {
|
|
return nil, fmt.Errorf("%w: username must match ^[a-z0-9_]{3,32}$", ErrBadRequest)
|
|
}
|
|
dnLen := utf8.RuneCountInString(req.DisplayName)
|
|
if dnLen < 1 || dnLen > 100 || strings.TrimSpace(req.DisplayName) == "" {
|
|
return nil, fmt.Errorf("%w: display_name must be 1..100 characters and not only whitespace", ErrBadRequest)
|
|
}
|
|
pwLen := utf8.RuneCountInString(req.Password)
|
|
if pwLen < 12 || pwLen > 128 || strings.TrimSpace(req.Password) == "" {
|
|
return nil, fmt.Errorf("%w: password must be 12..128 characters and not only whitespace", ErrBadRequest)
|
|
}
|
|
|
|
pwHash, err := auth.HashPassword(req.Password)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: failed to hash password", ErrUnavailable)
|
|
}
|
|
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
id := uuid.New().String()
|
|
now := NowUTC()
|
|
|
|
s.sqlQueries.Add(1)
|
|
_, err = s.pool.Exec(qCtx, `
|
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
|
VALUES ($1::uuid, $2, $3, $4, $5::timestamptz)
|
|
`, id, req.Username, req.DisplayName, pwHash, now)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
|
|
return &model.User{
|
|
ID: id,
|
|
Username: req.Username,
|
|
DisplayName: req.DisplayName,
|
|
CreatedAt: FormatTime(now),
|
|
}, nil
|
|
}
|
|
|
|
func (s *PostgresStore) AuthenticateAndCreateSession(ctx context.Context, username, password string, ttlSeconds int) (*model.Session, error) {
|
|
if !usernameRegex.MatchString(username) {
|
|
auth.VerifyDummy(password)
|
|
return nil, ErrUnauthorized
|
|
}
|
|
pwLen := utf8.RuneCountInString(password)
|
|
if pwLen < 1 || pwLen > 256 {
|
|
auth.VerifyDummy(password)
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
var user model.User
|
|
var pwHash string
|
|
var createdAt time.Time
|
|
|
|
s.sqlQueries.Add(1)
|
|
err := s.pool.QueryRow(qCtx, `
|
|
SELECT id::text, username, display_name, password_hash, created_at
|
|
FROM users
|
|
WHERE username = $1
|
|
`, username).Scan(&user.ID, &user.Username, &user.DisplayName, &pwHash, &createdAt)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
auth.VerifyDummy(password)
|
|
return nil, ErrUnauthorized
|
|
}
|
|
return nil, classifyDBError(err)
|
|
}
|
|
user.CreatedAt = FormatTime(createdAt)
|
|
|
|
valid, err := auth.VerifyPassword(password, pwHash)
|
|
if err != nil || !valid {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
rawToken, tokenHash, err := auth.GenerateSessionToken()
|
|
if err != nil {
|
|
return nil, ErrUnavailable
|
|
}
|
|
|
|
if ttlSeconds <= 0 {
|
|
ttlSeconds = 3600
|
|
}
|
|
now := NowUTC()
|
|
expiresAt := now.Add(time.Duration(ttlSeconds) * time.Second)
|
|
|
|
s.sqlQueries.Add(1)
|
|
_, err = s.pool.Exec(qCtx, `
|
|
INSERT INTO sessions (token_hash, user_id, expires_at, created_at)
|
|
VALUES ($1, $2::uuid, $3::timestamptz, $4::timestamptz)
|
|
`, tokenHash, user.ID, expiresAt, now)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
|
|
s.cacheSession(ctx, tokenHash, user, expiresAt)
|
|
|
|
return &model.Session{
|
|
Token: rawToken,
|
|
TokenType: "Bearer",
|
|
ExpiresAt: FormatTime(expiresAt),
|
|
User: user,
|
|
}, nil
|
|
}
|
|
|
|
func (s *PostgresStore) cacheSession(ctx context.Context, tokenHash string, user model.User, expiresAt time.Time) {
|
|
if s.rdb == nil {
|
|
return
|
|
}
|
|
remaining := time.Until(expiresAt)
|
|
if remaining <= 0 {
|
|
return
|
|
}
|
|
payload, err := json.Marshal(cachedSession{
|
|
User: user,
|
|
ExpiresAt: expiresAt,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
rCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
|
|
defer cancel()
|
|
key := "sess:" + tokenHash
|
|
if err := s.rdb.Set(rCtx, key, payload, remaining).Err(); err != nil {
|
|
log.Printf("WARN: redis session write failed (graceful bypass): %v", err)
|
|
return
|
|
}
|
|
_ = s.rdb.ExpireAt(rCtx, key, expiresAt).Err()
|
|
}
|
|
|
|
func (s *PostgresStore) GetUserByToken(ctx context.Context, rawToken string) (*model.User, error) {
|
|
if len(rawToken) < 32 || len(rawToken) > 256 {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
tokenHash := auth.HashToken(rawToken)
|
|
now := NowUTC()
|
|
|
|
// 1. Check Valkey/Redis session cache first
|
|
if s.rdb != nil {
|
|
rCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
|
|
val, err := s.rdb.Get(rCtx, "sess:"+tokenHash).Bytes()
|
|
cancel()
|
|
if err == nil {
|
|
var cs cachedSession
|
|
if jsonErr := json.Unmarshal(val, &cs); jsonErr == nil {
|
|
if now.Before(cs.ExpiresAt) {
|
|
s.cacheHits.Add(1)
|
|
return &cs.User, nil
|
|
}
|
|
}
|
|
} else if !errors.Is(err, redis.Nil) {
|
|
log.Printf("WARN: redis session read failed (falling back to PostgreSQL): %v", err)
|
|
}
|
|
s.cacheMisses.Add(1)
|
|
}
|
|
|
|
// 2. Fallback to PostgreSQL source of truth
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
var user model.User
|
|
var userCreatedAt, expiresAt time.Time
|
|
|
|
s.sqlQueries.Add(1)
|
|
err := s.pool.QueryRow(qCtx, `
|
|
SELECT u.id::text, u.username, u.display_name, u.created_at, s.expires_at
|
|
FROM sessions s
|
|
JOIN users u ON u.id = s.user_id
|
|
WHERE s.token_hash = $1
|
|
`, tokenHash).Scan(&user.ID, &user.Username, &user.DisplayName, &userCreatedAt, &expiresAt)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrUnauthorized
|
|
}
|
|
return nil, classifyDBError(err)
|
|
}
|
|
|
|
expiresAt = expiresAt.UTC().Truncate(time.Microsecond)
|
|
if !now.Before(expiresAt) {
|
|
// Session expired: clean up lazily and reject
|
|
_, _ = s.pool.Exec(qCtx, `DELETE FROM sessions WHERE token_hash = $1`, tokenHash)
|
|
return nil, ErrUnauthorized
|
|
}
|
|
|
|
user.CreatedAt = FormatTime(userCreatedAt)
|
|
// Repopulate Valkey/Redis with original expires_at (never extending TTL!)
|
|
s.cacheSession(ctx, tokenHash, user, expiresAt)
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *PostgresStore) RevokeSession(ctx context.Context, rawToken string) error {
|
|
if len(rawToken) < 32 || len(rawToken) > 256 {
|
|
return ErrUnauthorized
|
|
}
|
|
tokenHash := auth.HashToken(rawToken)
|
|
|
|
if s.rdb != nil {
|
|
rCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond)
|
|
if err := s.rdb.Del(rCtx, "sess:"+tokenHash).Err(); err != nil {
|
|
log.Printf("WARN: redis session delete failed: %v", err)
|
|
}
|
|
cancel()
|
|
}
|
|
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
s.sqlQueries.Add(1)
|
|
tag, err := s.pool.Exec(qCtx, `
|
|
DELETE FROM sessions
|
|
WHERE token_hash = $1 AND expires_at > $2::timestamptz
|
|
`, tokenHash, NowUTC())
|
|
if err != nil {
|
|
return classifyDBError(err)
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrUnauthorized
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *PostgresStore) CreateChat(ctx context.Context, callerID string, req model.CreateChatRequest) (*model.Chat, error) {
|
|
titleLen := utf8.RuneCountInString(req.Title)
|
|
if titleLen < 1 || titleLen > 100 || strings.TrimSpace(req.Title) == "" {
|
|
return nil, fmt.Errorf("%w: title must be 1..100 characters and not only whitespace", ErrBadRequest)
|
|
}
|
|
if len(req.MemberIDs) < 1 || len(req.MemberIDs) > 100 {
|
|
return nil, fmt.Errorf("%w: member_ids must contain 1..100 items", ErrBadRequest)
|
|
}
|
|
|
|
seen := make(map[string]struct{}, len(req.MemberIDs))
|
|
callerIncluded := false
|
|
for _, mid := range req.MemberIDs {
|
|
if _, err := uuid.Parse(mid); err != nil {
|
|
return nil, fmt.Errorf("%w: invalid UUID in member_ids", ErrBadRequest)
|
|
}
|
|
if _, exists := seen[mid]; exists {
|
|
return nil, fmt.Errorf("%w: duplicate user in member_ids", ErrBadRequest)
|
|
}
|
|
seen[mid] = struct{}{}
|
|
if mid == callerID {
|
|
callerIncluded = true
|
|
}
|
|
}
|
|
if !callerIncluded {
|
|
return nil, fmt.Errorf("%w: caller must be included in member_ids", ErrBadRequest)
|
|
}
|
|
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
// Composite atomic transaction: insert chat + insert all chat_members
|
|
tx, err := s.pool.Begin(qCtx)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
defer func() { _ = tx.Rollback(qCtx) }()
|
|
|
|
chatID := uuid.New().String()
|
|
now := NowUTC()
|
|
|
|
s.sqlQueries.Add(1)
|
|
_, err = tx.Exec(qCtx, `
|
|
INSERT INTO chats (id, title, created_at)
|
|
VALUES ($1::uuid, $2, $3::timestamptz)
|
|
`, chatID, req.Title, now)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
|
|
for idx, mid := range req.MemberIDs {
|
|
s.sqlQueries.Add(1)
|
|
_, err = tx.Exec(qCtx, `
|
|
INSERT INTO chat_members (chat_id, user_id, ord)
|
|
VALUES ($1::uuid, $2::uuid, $3)
|
|
`, chatID, mid, idx)
|
|
if err != nil {
|
|
// If any member does not exist in users, FK constraint 23503 triggers and rolls back the entire chat creation!
|
|
return nil, classifyDBError(err)
|
|
}
|
|
}
|
|
|
|
if err := tx.Commit(qCtx); err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
|
|
membersCopy := make([]string, len(req.MemberIDs))
|
|
copy(membersCopy, req.MemberIDs)
|
|
|
|
return &model.Chat{
|
|
ID: chatID,
|
|
Title: req.Title,
|
|
MemberIDs: membersCopy,
|
|
CreatedAt: FormatTime(now),
|
|
}, nil
|
|
}
|
|
|
|
func (s *PostgresStore) ListChats(ctx context.Context, callerID string, limit int, cursorStr string) (*model.ChatPage, error) {
|
|
if limit < 1 || limit > 100 {
|
|
return nil, fmt.Errorf("%w: limit must be between 1 and 100", ErrBadRequest)
|
|
}
|
|
|
|
scope := "chats:" + callerID
|
|
var hasCursor bool
|
|
var cursorTime time.Time
|
|
var cursorID string
|
|
if cursorStr != "" {
|
|
var err error
|
|
cursorTime, cursorID, err = DecodeCursor(cursorStr, scope)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hasCursor = true
|
|
}
|
|
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
var rows pgx.Rows
|
|
var err error
|
|
s.sqlQueries.Add(1)
|
|
if !hasCursor {
|
|
rows, err = s.pool.Query(qCtx, `
|
|
SELECT c.id::text, c.title, c.created_at,
|
|
array_agg(cm_all.user_id::text ORDER BY cm_all.ord ASC) AS member_ids
|
|
FROM chats c
|
|
JOIN chat_members cm_me ON cm_me.chat_id = c.id AND cm_me.user_id = $1::uuid
|
|
JOIN chat_members cm_all ON cm_all.chat_id = c.id
|
|
GROUP BY c.id, c.title, c.created_at
|
|
ORDER BY c.created_at ASC, c.id ASC
|
|
LIMIT $2
|
|
`, callerID, limit+1)
|
|
} else {
|
|
rows, err = s.pool.Query(qCtx, `
|
|
SELECT c.id::text, c.title, c.created_at,
|
|
array_agg(cm_all.user_id::text ORDER BY cm_all.ord ASC) AS member_ids
|
|
FROM chats c
|
|
JOIN chat_members cm_me ON cm_me.chat_id = c.id AND cm_me.user_id = $1::uuid
|
|
JOIN chat_members cm_all ON cm_all.chat_id = c.id
|
|
WHERE (c.created_at, c.id) > ($2::timestamptz, $3::uuid)
|
|
GROUP BY c.id, c.title, c.created_at
|
|
ORDER BY c.created_at ASC, c.id ASC
|
|
LIMIT $4
|
|
`, callerID, cursorTime, cursorID, limit+1)
|
|
}
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
items := make([]model.Chat, 0, limit)
|
|
for rows.Next() {
|
|
var ch model.Chat
|
|
var createdAt time.Time
|
|
if err := rows.Scan(&ch.ID, &ch.Title, &createdAt, &ch.MemberIDs); err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
ch.CreatedAt = FormatTime(createdAt)
|
|
items = append(items, ch)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
|
|
var nextCursor *string
|
|
if len(items) > limit {
|
|
items = items[:limit]
|
|
last := items[limit-1]
|
|
c := EncodeCursor(scope, last.CreatedAt, last.ID)
|
|
nextCursor = &c
|
|
}
|
|
|
|
return &model.ChatPage{
|
|
Items: items,
|
|
NextCursor: nextCursor,
|
|
}, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetChat(ctx context.Context, callerID, chatID string) (*model.Chat, error) {
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
var ch model.Chat
|
|
var createdAt time.Time
|
|
|
|
s.sqlQueries.Add(1)
|
|
err := s.pool.QueryRow(qCtx, `
|
|
SELECT c.id::text, c.title, c.created_at,
|
|
array_agg(cm.user_id::text ORDER BY cm.ord ASC) AS member_ids
|
|
FROM chats c
|
|
JOIN chat_members cm ON cm.chat_id = c.id
|
|
WHERE c.id = $1::uuid
|
|
AND EXISTS (
|
|
SELECT 1 FROM chat_members WHERE chat_id = c.id AND user_id = $2::uuid
|
|
)
|
|
GROUP BY c.id, c.title, c.created_at
|
|
`, chatID, callerID).Scan(&ch.ID, &ch.Title, &createdAt, &ch.MemberIDs)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
ch.CreatedAt = FormatTime(createdAt)
|
|
return &ch, nil
|
|
}
|
|
|
|
func (s *PostgresStore) lookupIdempotency(ctx context.Context, callerID, chatID, idempotencyKey, reqHash string, now time.Time) (*model.Message, bool, error) {
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
var existingHash string
|
|
var msg model.Message
|
|
var createdAt time.Time
|
|
|
|
s.sqlQueries.Add(1)
|
|
err := s.pool.QueryRow(qCtx, `
|
|
SELECT ik.request_hash, m.id::text, m.chat_id::text, m.sender_id::text, m.text, m.created_at,
|
|
COALESCE(array_agg(ma.attachment_id::text ORDER BY ma.ord ASC) FILTER (WHERE ma.attachment_id IS NOT NULL), '{}') AS attachment_ids
|
|
FROM idempotency_keys ik
|
|
JOIN messages m ON m.id = ik.message_id
|
|
LEFT JOIN message_attachments ma ON ma.message_id = m.id
|
|
WHERE ik.user_id = $1::uuid
|
|
AND ik.chat_id = $2::uuid
|
|
AND ik.idempotency_key = $3
|
|
AND ik.expires_at > $4::timestamptz
|
|
GROUP BY ik.request_hash, m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
|
`, callerID, chatID, idempotencyKey, now).Scan(&existingHash, &msg.ID, &msg.ChatID, &msg.SenderID, &msg.Text, &createdAt, &msg.AttachmentIDs)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, false, nil
|
|
}
|
|
return nil, false, classifyDBError(err)
|
|
}
|
|
if existingHash != reqHash {
|
|
return nil, true, fmt.Errorf("%w: idempotency key already used with different request body", ErrConflict)
|
|
}
|
|
if msg.AttachmentIDs == nil {
|
|
msg.AttachmentIDs = []string{}
|
|
}
|
|
msg.CreatedAt = FormatTime(createdAt)
|
|
return &msg, true, nil
|
|
}
|
|
|
|
func canonicalMessageHash(text string, attachmentIDs []string) string {
|
|
normAtts := attachmentIDs
|
|
if normAtts == nil {
|
|
normAtts = []string{}
|
|
}
|
|
canonicalBytes, _ := json.Marshal(struct {
|
|
Text string `json:"text"`
|
|
AttachmentIDs []string `json:"attachment_ids"`
|
|
}{
|
|
Text: text,
|
|
AttachmentIDs: normAtts,
|
|
})
|
|
sum := sha256.Sum256(canonicalBytes)
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
func (s *PostgresStore) IsChatMember(ctx context.Context, callerID, chatID string) (bool, error) {
|
|
return s.checkChatMembership(ctx, chatID, callerID, s.getCacheMode())
|
|
}
|
|
|
|
func (s *PostgresStore) CreateAttachment(ctx context.Context, att model.Attachment) (*model.Attachment, error) {
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
now := NowUTC()
|
|
s.sqlQueries.Add(1)
|
|
_, err := s.pool.Exec(qCtx, `
|
|
INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, status, error_code, created_at, updated_at)
|
|
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, 'ready', NULL, $9::timestamptz, $9::timestamptz)
|
|
`, att.ID, att.ChatID, att.OwnerID, att.Filename, att.ContentType, att.SizeBytes, att.SHA256, att.ObjectKey, now)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
|
|
ts := FormatTime(now)
|
|
out := att
|
|
out.Status = "ready"
|
|
out.Variants = []model.Variant{}
|
|
out.ErrorCode = nil
|
|
out.CreatedAt = ts
|
|
out.UpdatedAt = ts
|
|
return &out, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetAttachment(ctx context.Context, callerID, attachmentID string) (*model.Attachment, error) {
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
var att model.Attachment
|
|
var createdAt, updatedAt time.Time
|
|
s.sqlQueries.Add(1)
|
|
err := s.pool.QueryRow(qCtx, `
|
|
SELECT a.id::text, a.chat_id::text, a.owner_id::text, a.filename, a.content_type,
|
|
a.size_bytes, a.sha256, a.object_key, a.status, a.error_code, a.created_at, a.updated_at
|
|
FROM attachments a
|
|
JOIN chat_members cm ON cm.chat_id = a.chat_id AND cm.user_id = $2::uuid
|
|
WHERE a.id = $1::uuid
|
|
`, attachmentID, callerID).Scan(
|
|
&att.ID, &att.ChatID, &att.OwnerID, &att.Filename, &att.ContentType,
|
|
&att.SizeBytes, &att.SHA256, &att.ObjectKey, &att.Status, &att.ErrorCode, &createdAt, &updatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
att.Variants = []model.Variant{}
|
|
att.CreatedAt = FormatTime(createdAt)
|
|
att.UpdatedAt = FormatTime(updatedAt)
|
|
return &att, nil
|
|
}
|
|
|
|
func (s *PostgresStore) CreateMessage(ctx context.Context, callerID, chatID, idempotencyKey string, idempotencyTTLSeconds int, req model.CreateMessageRequest) (*model.Message, error) {
|
|
if !IdempotencyKeyRegex.MatchString(idempotencyKey) {
|
|
return nil, fmt.Errorf("%w: invalid or missing Idempotency-Key header", ErrBadRequest)
|
|
}
|
|
if len(req.AttachmentIDs) > 10 {
|
|
return nil, fmt.Errorf("%w: attachment_ids must contain at most 10 items", ErrBadRequest)
|
|
}
|
|
seenAtts := make(map[string]struct{}, len(req.AttachmentIDs))
|
|
normAtts := make([]string, 0, len(req.AttachmentIDs))
|
|
for _, aid := range req.AttachmentIDs {
|
|
if _, err := uuid.Parse(aid); err != nil {
|
|
return nil, fmt.Errorf("%w: invalid UUID in attachment_ids", ErrBadRequest)
|
|
}
|
|
if _, dup := seenAtts[aid]; dup {
|
|
return nil, fmt.Errorf("%w: duplicate attachment_id in attachment_ids", ErrBadRequest)
|
|
}
|
|
seenAtts[aid] = struct{}{}
|
|
normAtts = append(normAtts, aid)
|
|
}
|
|
|
|
textLen := utf8.RuneCountInString(req.Text)
|
|
if textLen > 4000 {
|
|
return nil, fmt.Errorf("%w: text must be at most 4000 characters", ErrBadRequest)
|
|
}
|
|
if strings.TrimSpace(req.Text) == "" && len(normAtts) == 0 {
|
|
return nil, fmt.Errorf("%w: text must not be empty/whitespace when attachment_ids is empty", ErrBadRequest)
|
|
}
|
|
if idempotencyTTLSeconds <= 0 {
|
|
idempotencyTTLSeconds = 86400
|
|
}
|
|
|
|
mode := s.getCacheMode()
|
|
// Always verify membership/chat existence first so outsiders get 404 without touching idempotency_keys.
|
|
isMember, err := s.checkChatMembership(ctx, chatID, callerID, mode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !isMember {
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
reqHash := canonicalMessageHash(req.Text, normAtts)
|
|
now := NowUTC()
|
|
|
|
// Fast-path replay lookup
|
|
if existing, found, err := s.lookupIdempotency(ctx, callerID, chatID, idempotencyKey, reqHash, now); err != nil {
|
|
return nil, err
|
|
} else if found {
|
|
return existing, nil
|
|
}
|
|
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
|
|
// Validate that all referenced attachments belong to this chat and are ready (foreign/unknown -> 404, failed -> 409).
|
|
if len(normAtts) > 0 {
|
|
s.sqlQueries.Add(1)
|
|
rows, aErr := s.pool.Query(qCtx, `
|
|
SELECT id::text, status
|
|
FROM attachments
|
|
WHERE chat_id = $1::uuid AND id = ANY($2::uuid[])
|
|
`, chatID, normAtts)
|
|
if aErr != nil {
|
|
return nil, classifyDBError(aErr)
|
|
}
|
|
foundStatuses := make(map[string]string, len(normAtts))
|
|
for rows.Next() {
|
|
var aid, status string
|
|
if sErr := rows.Scan(&aid, &status); sErr != nil {
|
|
rows.Close()
|
|
return nil, classifyDBError(sErr)
|
|
}
|
|
foundStatuses[aid] = status
|
|
}
|
|
rErr := rows.Err()
|
|
rows.Close()
|
|
if rErr != nil {
|
|
return nil, classifyDBError(rErr)
|
|
}
|
|
if len(foundStatuses) != len(normAtts) {
|
|
return nil, ErrNotFound
|
|
}
|
|
for _, aid := range normAtts {
|
|
if foundStatuses[aid] != "ready" {
|
|
return nil, fmt.Errorf("%w: attachment is not ready", ErrConflict)
|
|
}
|
|
}
|
|
}
|
|
|
|
tx, err := s.pool.Begin(qCtx)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
defer func() { _ = tx.Rollback(qCtx) }()
|
|
|
|
msgID := uuid.New().String()
|
|
expiresAt := now.Add(time.Duration(idempotencyTTLSeconds) * time.Second)
|
|
|
|
batch := &pgx.Batch{}
|
|
batch.Queue(`
|
|
DELETE FROM idempotency_keys
|
|
WHERE user_id = $1::uuid AND chat_id = $2::uuid AND idempotency_key = $3 AND expires_at <= $4::timestamptz
|
|
`, callerID, chatID, idempotencyKey, now)
|
|
batch.Queue(`
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5::timestamptz)
|
|
`, msgID, chatID, callerID, req.Text, now)
|
|
for idx, aid := range normAtts {
|
|
batch.Queue(`
|
|
INSERT INTO message_attachments (message_id, attachment_id, ord)
|
|
VALUES ($1::uuid, $2::uuid, $3)
|
|
`, msgID, aid, idx)
|
|
}
|
|
batch.Queue(`
|
|
INSERT INTO idempotency_keys (user_id, chat_id, idempotency_key, request_hash, message_id, created_at, expires_at)
|
|
VALUES ($1::uuid, $2::uuid, $3, $4, $5::uuid, $6::timestamptz, $7::timestamptz)
|
|
ON CONFLICT (user_id, chat_id, idempotency_key) DO NOTHING
|
|
`, callerID, chatID, idempotencyKey, reqHash, msgID, now, expiresAt)
|
|
|
|
expectedCmds := 3 + len(normAtts)
|
|
s.sqlQueries.Add(int64(expectedCmds))
|
|
br := tx.SendBatch(qCtx, batch)
|
|
var claimed int64
|
|
for cmdIdx := 0; cmdIdx < expectedCmds; cmdIdx++ {
|
|
tag, bErr := br.Exec()
|
|
if bErr != nil {
|
|
_ = br.Close()
|
|
return nil, classifyDBError(bErr)
|
|
}
|
|
if cmdIdx == expectedCmds-1 {
|
|
claimed = tag.RowsAffected()
|
|
}
|
|
}
|
|
if cErr := br.Close(); cErr != nil {
|
|
return nil, classifyDBError(cErr)
|
|
}
|
|
|
|
if claimed == 0 {
|
|
// Another replica won the concurrent idempotency race; roll back duplicate inserts and read winner.
|
|
_ = tx.Rollback(qCtx)
|
|
existing, found, lErr := s.lookupIdempotency(ctx, callerID, chatID, idempotencyKey, reqHash, now)
|
|
if lErr != nil {
|
|
return nil, lErr
|
|
}
|
|
if !found {
|
|
return nil, ErrUnavailable
|
|
}
|
|
return existing, nil
|
|
}
|
|
|
|
if err := tx.Commit(qCtx); err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
|
|
// Invalidate chat history cache immediately on write so any replica reads fresh history (Read-After-Write).
|
|
if s.rdb != nil && mode == "write_through_versioned" {
|
|
rCtx, rCancel := context.WithTimeout(ctx, 300*time.Millisecond)
|
|
verKey := "chat_ver:" + chatID
|
|
if err := s.rdb.Incr(rCtx, verKey).Err(); err != nil {
|
|
log.Printf("WARN: redis cache version increment failed: %v", err)
|
|
} else {
|
|
_ = s.rdb.Expire(rCtx, verKey, 24*time.Hour).Err()
|
|
}
|
|
rCancel()
|
|
}
|
|
|
|
return &model.Message{
|
|
ID: msgID,
|
|
ChatID: chatID,
|
|
SenderID: callerID,
|
|
Text: req.Text,
|
|
CreatedAt: FormatTime(now),
|
|
AttachmentIDs: normAtts,
|
|
}, nil
|
|
}
|
|
|
|
func (s *PostgresStore) checkChatMembership(ctx context.Context, chatID, callerID, mode string) (bool, error) {
|
|
memKey := fmt.Sprintf("chat_mem:%s:%s", chatID, callerID)
|
|
if s.rdb != nil && mode != "disabled" {
|
|
rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond)
|
|
val, err := s.rdb.Get(rCtx, memKey).Result()
|
|
rCancel()
|
|
if err == nil {
|
|
return val == "1", nil
|
|
}
|
|
}
|
|
|
|
queryMembership := func() (bool, error) {
|
|
qCtx, cancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer cancel()
|
|
var isMember bool
|
|
s.sqlQueries.Add(1)
|
|
err := s.pool.QueryRow(qCtx, `
|
|
SELECT EXISTS (
|
|
SELECT 1 FROM chat_members WHERE chat_id = $1::uuid AND user_id = $2::uuid
|
|
)
|
|
`, chatID, callerID).Scan(&isMember)
|
|
if err != nil {
|
|
return false, classifyDBError(err)
|
|
}
|
|
if s.rdb != nil && mode != "disabled" {
|
|
rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond)
|
|
strVal := "0"
|
|
if isMember {
|
|
strVal = "1"
|
|
}
|
|
_ = s.rdb.Set(rCtx, memKey, strVal, 10*time.Minute).Err()
|
|
rCancel()
|
|
}
|
|
return isMember, nil
|
|
}
|
|
|
|
if mode == "write_through_versioned" {
|
|
res, err, _ := s.sfGroup.Do(memKey, func() (interface{}, error) {
|
|
return queryMembership()
|
|
})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return res.(bool), nil
|
|
}
|
|
return queryMembership()
|
|
}
|
|
|
|
func (s *PostgresStore) ListMessages(ctx context.Context, callerID, chatID string, limit int, cursorStr string) (*model.MessagePage, error) {
|
|
if limit < 1 || limit > 100 {
|
|
return nil, fmt.Errorf("%w: limit must be between 1 and 100", ErrBadRequest)
|
|
}
|
|
|
|
scope := "msgs:" + chatID
|
|
var hasCursor bool
|
|
var cursorTime time.Time
|
|
var cursorID string
|
|
if cursorStr != "" {
|
|
var err error
|
|
cursorTime, cursorID, err = DecodeCursor(cursorStr, scope)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
hasCursor = true
|
|
}
|
|
|
|
mode := s.getCacheMode()
|
|
|
|
// 1. Always enforce membership check before returning cached or DB history
|
|
isMember, err := s.checkChatMembership(ctx, chatID, callerID, mode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !isMember {
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
// 2. Determine cache key
|
|
var cacheKey string
|
|
if s.rdb != nil && mode != "disabled" {
|
|
if mode == "naive_ttl" {
|
|
cacheKey = fmt.Sprintf("msg_page:%s:naive:l%d:c%s", chatID, limit, cursorStr)
|
|
} else {
|
|
rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond)
|
|
ver, err := s.rdb.Get(rCtx, "chat_ver:"+chatID).Int64()
|
|
rCancel()
|
|
if err != nil && !errors.Is(err, redis.Nil) {
|
|
log.Printf("WARN: redis version read failed (bypassing cache): %v", err)
|
|
cacheKey = ""
|
|
} else {
|
|
cacheKey = fmt.Sprintf("msg_page:%s:v%d:l%d:c%s", chatID, ver, limit, cursorStr)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Check cache
|
|
if cacheKey != "" {
|
|
rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond)
|
|
cachedBytes, err := s.rdb.Get(rCtx, cacheKey).Bytes()
|
|
rCancel()
|
|
if err == nil {
|
|
var page model.MessagePage
|
|
if json.Unmarshal(cachedBytes, &page) == nil {
|
|
s.cacheHits.Add(1)
|
|
return &page, nil
|
|
}
|
|
} else if !errors.Is(err, redis.Nil) {
|
|
log.Printf("WARN: redis page read failed (falling back to PostgreSQL): %v", err)
|
|
}
|
|
s.cacheMisses.Add(1)
|
|
}
|
|
|
|
fetchFromDB := func() (*model.MessagePage, error) {
|
|
dbCtx, dbCancel := context.WithTimeout(ctx, s.queryTimeout)
|
|
defer dbCancel()
|
|
|
|
var rows pgx.Rows
|
|
var qErr error
|
|
s.sqlQueries.Add(1)
|
|
if !hasCursor {
|
|
rows, qErr = s.pool.Query(dbCtx, `
|
|
SELECT m.id::text, m.chat_id::text, m.sender_id::text, m.text, m.created_at,
|
|
COALESCE(array_agg(ma.attachment_id::text ORDER BY ma.ord ASC) FILTER (WHERE ma.attachment_id IS NOT NULL), '{}') AS attachment_ids
|
|
FROM messages m
|
|
LEFT JOIN message_attachments ma ON ma.message_id = m.id
|
|
WHERE m.chat_id = $1::uuid
|
|
GROUP BY m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
|
ORDER BY m.created_at ASC, m.id ASC
|
|
LIMIT $2
|
|
`, chatID, limit+1)
|
|
} else {
|
|
rows, qErr = s.pool.Query(dbCtx, `
|
|
SELECT m.id::text, m.chat_id::text, m.sender_id::text, m.text, m.created_at,
|
|
COALESCE(array_agg(ma.attachment_id::text ORDER BY ma.ord ASC) FILTER (WHERE ma.attachment_id IS NOT NULL), '{}') AS attachment_ids
|
|
FROM messages m
|
|
LEFT JOIN message_attachments ma ON ma.message_id = m.id
|
|
WHERE m.chat_id = $1::uuid
|
|
AND (m.created_at, m.id) > ($2::timestamptz, $3::uuid)
|
|
GROUP BY m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
|
ORDER BY m.created_at ASC, m.id ASC
|
|
LIMIT $4
|
|
`, chatID, cursorTime, cursorID, limit+1)
|
|
}
|
|
if qErr != nil {
|
|
return nil, classifyDBError(qErr)
|
|
}
|
|
defer rows.Close()
|
|
|
|
items := make([]model.Message, 0, limit)
|
|
for rows.Next() {
|
|
var m model.Message
|
|
var createdAt time.Time
|
|
if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &createdAt, &m.AttachmentIDs); err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
if m.AttachmentIDs == nil {
|
|
m.AttachmentIDs = []string{}
|
|
}
|
|
m.CreatedAt = FormatTime(createdAt)
|
|
items = append(items, m)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
|
|
var nextCursor *string
|
|
if len(items) > limit {
|
|
items = items[:limit]
|
|
last := items[limit-1]
|
|
c := EncodeCursor(scope, last.CreatedAt, last.ID)
|
|
nextCursor = &c
|
|
}
|
|
|
|
page := &model.MessagePage{
|
|
Items: items,
|
|
NextCursor: nextCursor,
|
|
}
|
|
|
|
if cacheKey != "" && s.rdb != nil {
|
|
if payload, mErr := json.Marshal(page); mErr == nil {
|
|
rCtx, rCancel := context.WithTimeout(ctx, 200*time.Millisecond)
|
|
ttl := 30 * time.Second
|
|
if mode == "naive_ttl" {
|
|
ttl = 5 * time.Second
|
|
}
|
|
_ = s.rdb.Set(rCtx, cacheKey, payload, ttl).Err()
|
|
rCancel()
|
|
}
|
|
}
|
|
|
|
return page, nil
|
|
}
|
|
|
|
// 4. Use singleflight in write_through_versioned mode to prevent cache stampede!
|
|
if mode == "write_through_versioned" && cacheKey != "" {
|
|
res, err, shared := s.sfGroup.Do(cacheKey, func() (interface{}, error) {
|
|
return fetchFromDB()
|
|
})
|
|
if shared {
|
|
s.singleflightShared.Add(1)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return res.(*model.MessagePage), nil
|
|
}
|
|
|
|
return fetchFromDB()
|
|
}
|
|
|
|
type dbAttachmentRow struct {
|
|
ID string
|
|
ObjectKey string
|
|
Status string
|
|
CreatedAt time.Time
|
|
Referenced bool
|
|
}
|
|
|
|
func (s *PostgresStore) Reconcile(ctx context.Context, s3 *s3store.Client, gracePeriod time.Duration, dryRun bool) (*ReconcileReport, error) {
|
|
started := time.Now()
|
|
report := &ReconcileReport{
|
|
DryRun: dryRun,
|
|
GracePeriodSeconds: int64(gracePeriod.Seconds()),
|
|
OrphanS3Found: []string{},
|
|
OrphanS3Deleted: []string{},
|
|
OrphanS3SkippedGrace: []string{},
|
|
MissingS3Found: []string{},
|
|
MissingS3MarkedFailed: []string{},
|
|
MissingS3Deleted: []string{},
|
|
MissingS3SkippedGrace: []string{},
|
|
}
|
|
|
|
if s3 == nil {
|
|
return nil, ErrUnavailable
|
|
}
|
|
|
|
conn, err := s.pool.Acquire(ctx)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
defer conn.Release()
|
|
|
|
var locked bool
|
|
report.DBQueries++
|
|
if err := conn.QueryRow(ctx, `SELECT pg_try_advisory_lock(20260401)`).Scan(&locked); err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
if !locked {
|
|
report.LockAcquired = false
|
|
report.DurationMs = float64(time.Since(started).Microseconds()) / 1000.0
|
|
return report, nil
|
|
}
|
|
defer func() {
|
|
uCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
_, _ = conn.Exec(uCtx, `SELECT pg_advisory_unlock(20260401)`)
|
|
cancel()
|
|
}()
|
|
report.LockAcquired = true
|
|
|
|
// 1. Scan S3 objects under attachments/
|
|
report.S3ListCalls++
|
|
s3Objs, err := s3.ListObjects(ctx, "attachments/")
|
|
if err != nil {
|
|
return nil, ErrUnavailable
|
|
}
|
|
report.S3ObjectsScanned = len(s3Objs)
|
|
s3Map := make(map[string]s3store.ObjectSummary, len(s3Objs))
|
|
for _, obj := range s3Objs {
|
|
s3Map[obj.Key] = obj
|
|
}
|
|
|
|
// 2. Scan PostgreSQL attachments + reference state
|
|
qCtx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
|
defer cancel()
|
|
report.DBQueries++
|
|
rows, err := conn.Query(qCtx, `
|
|
SELECT a.id::text, a.object_key, a.status, a.created_at,
|
|
EXISTS (SELECT 1 FROM message_attachments ma WHERE ma.attachment_id = a.id) AS referenced
|
|
FROM attachments a
|
|
`)
|
|
if err != nil {
|
|
return nil, classifyDBError(err)
|
|
}
|
|
var dbRows []dbAttachmentRow
|
|
dbKeyMap := make(map[string]dbAttachmentRow)
|
|
for rows.Next() {
|
|
var r dbAttachmentRow
|
|
if sErr := rows.Scan(&r.ID, &r.ObjectKey, &r.Status, &r.CreatedAt, &r.Referenced); sErr != nil {
|
|
rows.Close()
|
|
return nil, classifyDBError(sErr)
|
|
}
|
|
dbRows = append(dbRows, r)
|
|
dbKeyMap[r.ObjectKey] = r
|
|
}
|
|
if rErr := rows.Err(); rErr != nil {
|
|
rows.Close()
|
|
return nil, classifyDBError(rErr)
|
|
}
|
|
rows.Close()
|
|
report.DBRowsScanned = len(dbRows)
|
|
|
|
now := time.Now().UTC()
|
|
|
|
// 3. Detect & safely clean orphan S3 objects (present in S3, absent in DB)
|
|
for _, obj := range s3Objs {
|
|
if _, exists := dbKeyMap[obj.Key]; exists {
|
|
continue
|
|
}
|
|
age := now.Sub(obj.LastModified.UTC())
|
|
if age <= gracePeriod {
|
|
report.OrphanS3SkippedGrace = append(report.OrphanS3SkippedGrace, obj.Key)
|
|
continue
|
|
}
|
|
report.OrphanS3Found = append(report.OrphanS3Found, obj.Key)
|
|
if !dryRun {
|
|
report.S3RemoveCalls++
|
|
if rErr := s3.RemoveObject(ctx, obj.Key); rErr != nil {
|
|
return nil, ErrUnavailable
|
|
}
|
|
report.OrphanS3Deleted = append(report.OrphanS3Deleted, obj.Key)
|
|
}
|
|
}
|
|
|
|
// 4. Detect & safely reconcile missing S3 objects (present in DB, absent in S3)
|
|
for _, row := range dbRows {
|
|
if _, exists := s3Map[row.ObjectKey]; exists {
|
|
if row.Status == "ready" {
|
|
report.HealthyPreserved++
|
|
}
|
|
continue
|
|
}
|
|
if row.Status == "failed" {
|
|
continue
|
|
}
|
|
age := now.Sub(row.CreatedAt.UTC())
|
|
if age <= gracePeriod {
|
|
report.MissingS3SkippedGrace = append(report.MissingS3SkippedGrace, row.ID)
|
|
continue
|
|
}
|
|
report.MissingS3Found = append(report.MissingS3Found, row.ID)
|
|
if !dryRun {
|
|
if row.Status == "pending" && !row.Referenced {
|
|
report.DBQueries++
|
|
if _, dErr := conn.Exec(qCtx, `DELETE FROM attachments WHERE id = $1::uuid`, row.ID); dErr != nil {
|
|
return nil, classifyDBError(dErr)
|
|
}
|
|
report.MissingS3Deleted = append(report.MissingS3Deleted, row.ID)
|
|
} else {
|
|
report.DBQueries++
|
|
if _, uErr := conn.Exec(qCtx, `
|
|
UPDATE attachments
|
|
SET status = 'failed', error_code = 'missing_s3_object', updated_at = $2::timestamptz
|
|
WHERE id = $1::uuid
|
|
`, row.ID, NowUTC()); uErr != nil {
|
|
return nil, classifyDBError(uErr)
|
|
}
|
|
report.MissingS3MarkedFailed = append(report.MissingS3MarkedFailed, row.ID)
|
|
}
|
|
}
|
|
}
|
|
|
|
report.DurationMs = float64(time.Since(started).Microseconds()) / 1000.0
|
|
return report, nil
|
|
}
|