Разрешите отправку сообщения во время публикации вложения

This commit is contained in:
2026-11-20 10:54:34 +03:00
parent c6a5e9ee47
commit bdc072aab7
3 changed files with 90 additions and 9 deletions
+78
View File
@@ -16,6 +16,7 @@ import (
"messenger/internal/chat"
"messenger/internal/chat/store"
"messenger/internal/pipeline"
"messenger/internal/platform/apperr"
"messenger/internal/platform/ident"
)
@@ -79,6 +80,83 @@ func TestAppendOnceRejectsUnknownAttachmentWithoutClaim(t *testing.T) {
}
}
func TestAppendOnceAcceptsQueuedAttachmentDuringPublish(t *testing.T) {
f := newFixture(t)
alice := f.mustUser(t)
c := f.mustChat(t, alice)
id := f.attachment(t, c.ID, alice, "queued")
m := attachedMessage(t, c.ID, alice, id)
found, err := pipeline.NewStore(db.Pool).PublishOne(f.ctx, func(ctx context.Context, j pipeline.Job) error {
if j.AttachmentID != id {
t.Fatalf("publisher selected %s, want %s", j.AttachmentID, id)
}
// The callback holds the publisher transaction open until AppendOnce returns.
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
_, err := f.messages.AppendOnce(ctx, m, "during-publish", keyTTL)
return err
})
if err != nil || !found {
t.Fatalf("linking queued attachment while awaiting broker confirm: found=%v, err=%v", found, err)
}
assertCounts(t, 1, 1, 1)
}
func TestAppendOnceSeesFailureCommittedWhileValidating(t *testing.T) {
f := newFixture(t)
alice := f.mustUser(t)
c := f.mustChat(t, alice)
id := f.attachment(t, c.ID, alice, "queued")
if _, err := db.Pool.Exec(f.ctx, `UPDATE attachment_jobs SET state='processing',
lease_token=gen_random_uuid(),lease_until=now()+interval '1 hour' WHERE attachment_id=$1`, id); err != nil {
t.Fatal(err)
}
tx, err := db.Pool.Begin(f.ctx)
if err != nil {
t.Fatal(err)
}
defer tx.Rollback(f.ctx)
// Fail and lease recovery both take this lock before deciding the terminal state.
if _, err := tx.Exec(f.ctx, `SELECT job_id FROM attachment_jobs WHERE attachment_id=$1 FOR UPDATE`, id); err != nil {
t.Fatal(err)
}
if _, err := tx.Exec(f.ctx, `UPDATE attachment_jobs SET state='failed',error_code='retry_exhausted',
lease_token=NULL,lease_until=NULL WHERE attachment_id=$1`, id); err != nil {
t.Fatal(err)
}
m := attachedMessage(t, c.ID, alice, id)
ctx, cancel := context.WithTimeout(f.ctx, 5*time.Second)
defer cancel()
done := make(chan error, 1)
go func() {
_, err := f.messages.AppendOnce(ctx, m, "failing", keyTTL)
done <- err
}()
for {
var waiting bool
err := db.Pool.QueryRow(ctx, `SELECT EXISTS (
SELECT FROM pg_stat_activity WHERE $1 = ANY(pg_blocking_pids(pid)))`, tx.Conn().PgConn().PID()).Scan(&waiting)
if err != nil {
t.Fatal(err)
}
if waiting {
break
}
select {
case err := <-done:
t.Fatalf("message finished before the terminal transition: %v", err)
case <-time.After(10 * time.Millisecond):
}
}
if err := tx.Commit(ctx); err != nil {
t.Fatal(err)
}
if err := <-done; err == nil || apperr.KindOf(err) != apperr.KindConflict {
t.Fatalf("message with concurrently failed attachment: %v; want conflict", err)
}
assertCounts(t, 0, 0, 0)
}
func TestAppendOnceAttachmentValidationIsAtomic(t *testing.T) {
for _, status := range []string{"foreign", "failed", "mixed"} {
t.Run(status, func(t *testing.T) {
+6 -4
View File
@@ -18,6 +18,7 @@ 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"
@@ -209,12 +210,13 @@ func validateAttachments(ctx context.Context, tx pgx.Tx, chatID uuid.UUID, ids [
return apperr.NotFound("attachment not found")
}
// 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)
// Terminal failures take FOR UPDATE on the job or file. KEY SHARE orders
// validation with those transitions while allowing an outbox publish in flight.
rows, err = tx.Query(ctx, `SELECT status FROM attachment_states WHERE id=ANY($1::uuid[]) ORDER BY id FOR KEY SHARE`, ids)
if err != nil {
return err
}
states, err := pgx.CollectRows(rows, pgx.RowTo[string])
states, err := pgx.CollectRows(rows, pgx.RowTo[attachment.FileStatus])
if err != nil {
return err
}
@@ -223,7 +225,7 @@ func validateAttachments(ctx context.Context, tx pgx.Tx, chatID uuid.UUID, ids [
}
for _, state := range states {
switch state {
case "queued", "processing", "ready":
case attachment.FileQueued, attachment.FileProcessing, attachment.FileReady:
default:
return apperr.Conflict("attachment processing failed")
}
+6 -5
View File
@@ -18,13 +18,14 @@ func NewStore(pool *pgxpool.Pool) *Store { return &Store{pool: pool} }
// The callback waits for a routed publisher confirm with a finite timeout.
// Losing that confirm or the SQL commit leaves the same envelope deliverable.
// NO KEY UPDATE excludes other publishers but allows messages to link a queued attachment.
func (s *Store) PublishOne(ctx context.Context, publish func(context.Context, Job) error) (bool, error) {
var found bool
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
j := Job{SchemaVersion: 1, Type: "attachment.process.v1"}
err := tx.QueryRow(ctx, `SELECT job_id,attachment_id,pipeline_version,attempt,created_at
FROM attachment_jobs WHERE state='queued' AND published_at IS NULL AND available_at<=clock_timestamp()
ORDER BY available_at,job_id LIMIT 1 FOR UPDATE SKIP LOCKED`).
ORDER BY available_at,job_id LIMIT 1 FOR NO KEY UPDATE SKIP LOCKED`).
Scan(&j.JobID, &j.AttachmentID, &j.PipelineVersion, &j.Attempt, &j.CreatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil
@@ -60,12 +61,12 @@ func (s *Store) Claim(ctx context.Context, j Job, lease time.Duration) (*Claim,
RETURNING j.source_key,f.hash,f.size_bytes,f.mime_type`,
j.JobID, j.AttachmentID, j.PipelineVersion, j.Attempt, j.CreatedAt, c.Token, lease.Microseconds()).
Scan(&c.SourceKey, &c.Hash, &c.SizeBytes, &c.MIMEType)
if !errors.Is(err, pgx.ErrNoRows) {
if err != nil {
return nil, err
}
if err == nil {
return &c, nil
}
if !errors.Is(err, pgx.ErrNoRows) {
return nil, err
}
var incomplete bool
err = s.pool.QueryRow(ctx, `SELECT EXISTS (
SELECT 1 FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id