Переведите приём и чтение вложений на асинхронный контракт
This commit is contained in:
@@ -18,7 +18,6 @@ import (
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/postgres"
|
||||
@@ -138,9 +137,6 @@ const claimSQL = `
|
||||
func (r *Messages) AppendOnce(ctx context.Context, m chat.Message, key string, ttl time.Duration) (chat.Message, error) {
|
||||
var stored chat.Message
|
||||
err := pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
||||
if err := validateAttachments(ctx, tx, m.ChatID, m.AttachmentIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
@@ -151,6 +147,11 @@ func (r *Messages) AppendOnce(ctx context.Context, m chat.Message, key string, t
|
||||
return fmt.Errorf("insert message: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() == 1 {
|
||||
// Only a new message creates attachment links. A replay returns the
|
||||
// committed message even if its attachment has since failed.
|
||||
if err := validateAttachments(ctx, tx, m.ChatID, m.AttachmentIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(m.AttachmentIDs) > 0 {
|
||||
_, err := tx.Exec(ctx, `
|
||||
INSERT INTO message_attachments (chat_id, message_id, attachment_id, ordinal)
|
||||
@@ -192,27 +193,39 @@ func validateAttachments(ctx context.Context, tx pgx.Tx, chatID uuid.UUID, ids [
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
// Shared row locks prevent deletion or a readiness change until joins commit.
|
||||
// Lock attachment identities in the same order for every message. Pipeline
|
||||
// version changes lock the attachment before replacing its current job.
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT f.status
|
||||
FROM attachments a
|
||||
JOIN files f ON f.hash = a.file_hash
|
||||
WHERE a.chat_id = $1 AND a.id = ANY($2::uuid[])
|
||||
ORDER BY a.id
|
||||
FOR SHARE OF a, f`, chatID, ids)
|
||||
SELECT id FROM attachments WHERE chat_id=$1 AND id=ANY($2::uuid[])
|
||||
ORDER BY id FOR SHARE`, chatID, ids)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check attachments: %w", err)
|
||||
return fmt.Errorf("lock attachments: %w", err)
|
||||
}
|
||||
statuses, err := pgx.CollectRows(rows, pgx.RowTo[attachment.FileStatus])
|
||||
locked, err := pgx.CollectRows(rows, pgx.RowTo[uuid.UUID])
|
||||
if err != nil {
|
||||
return fmt.Errorf("read attachment status: %w", err)
|
||||
return err
|
||||
}
|
||||
if len(statuses) != len(ids) {
|
||||
if len(locked) != len(ids) {
|
||||
return apperr.NotFound("attachment not found")
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if status != attachment.FileReady {
|
||||
return apperr.Conflict("attachment is not ready")
|
||||
// A separate statement sees the latest committed transition after a lock wait.
|
||||
// Every referenced attachment must have a current job.
|
||||
rows, err = tx.Query(ctx, `SELECT status FROM attachment_states WHERE id=ANY($1::uuid[]) ORDER BY id FOR SHARE`, ids)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
states, err := pgx.CollectRows(rows, pgx.RowTo[string])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(states) != len(ids) {
|
||||
return apperr.Conflict("attachment processing state unavailable")
|
||||
}
|
||||
for _, state := range states {
|
||||
switch state {
|
||||
case "queued", "processing", "ready":
|
||||
default:
|
||||
return apperr.Conflict("attachment processing failed")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user