Переведите приём и чтение вложений на асинхронный контракт
This commit is contained in:
@@ -2,8 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"crypto/sha256"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -14,78 +13,77 @@ import (
|
||||
"messenger/internal/platform/postgres"
|
||||
)
|
||||
|
||||
type Attachments struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
type Attachments struct{ pool *pgxpool.Pool }
|
||||
|
||||
var _ attachment.AttachmentRepository = (*Attachments)(nil)
|
||||
|
||||
func NewAttachments(pool *pgxpool.Pool) *Attachments {
|
||||
return &Attachments{pool: pool}
|
||||
}
|
||||
func NewAttachments(pool *pgxpool.Pool) *Attachments { return &Attachments{pool: pool} }
|
||||
|
||||
// Publication and garbage collection use the same hash lock. The transaction
|
||||
// spans only publication, never client transfer or image decoding.
|
||||
func (r *Attachments) WithPublication(ctx context.Context, a attachment.Attachment, fn func(attachment.Publication) error) error {
|
||||
func (r *Attachments) Enqueue(ctx context.Context, a attachment.Attachment, stage string, checkStage func() error) error {
|
||||
return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
||||
if _, err := tx.Exec(ctx, `SET LOCAL idle_in_transaction_session_timeout = '35s'`); err != nil {
|
||||
// A request can pause after validation for longer than the GC grace.
|
||||
// Verify the source after serializing with deletion of this exact key.
|
||||
stageHash := sha256.Sum256([]byte(stage))
|
||||
if err := attachment.LockHash(ctx, tx, stageHash[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := checkStage(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := attachment.LockHash(ctx, tx, a.FileHash); err != nil {
|
||||
return err
|
||||
}
|
||||
var status attachment.FileStatus
|
||||
err := tx.QueryRow(ctx, `SELECT status FROM files WHERE hash = $1 FOR UPDATE`, a.FileHash).Scan(&status)
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
if _, err := tx.Exec(ctx, `
|
||||
INSERT INTO files(hash,size_bytes,mime_type,status) VALUES ($1,$2,$3,'pending')
|
||||
ON CONFLICT (hash) DO NOTHING`, a.FileHash, a.SizeBytes, a.MIMEType); err != nil {
|
||||
return err
|
||||
}
|
||||
return fn(publication{tx: tx, attachment: a, ready: status == attachment.FileReady})
|
||||
if _, err := tx.Exec(ctx, `
|
||||
INSERT INTO attachments(id,chat_id,owner_id,filename,file_hash,created_at)
|
||||
VALUES ($1,$2,$3,$4,$5,$6)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.FileHash, a.CreatedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.Exec(ctx, `INSERT INTO attachment_jobs(attachment_id,source_key) VALUES ($1,$2)`, a.ID, stage)
|
||||
return err
|
||||
}))
|
||||
}
|
||||
|
||||
type publication struct {
|
||||
tx pgx.Tx
|
||||
attachment attachment.Attachment
|
||||
ready bool
|
||||
}
|
||||
|
||||
func (p publication) FileReady() bool { return p.ready }
|
||||
|
||||
func (p publication) SaveReady(ctx context.Context) error {
|
||||
a := p.attachment
|
||||
_, err := p.tx.Exec(ctx, `
|
||||
INSERT INTO files (hash, size_bytes, mime_type, status)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (hash) DO UPDATE
|
||||
SET size_bytes = excluded.size_bytes,
|
||||
mime_type = excluded.mime_type,
|
||||
status = excluded.status`,
|
||||
a.FileHash, a.SizeBytes, a.MIMEType, attachment.FileReady)
|
||||
if err != nil {
|
||||
return fmt.Errorf("publish file: %w", err)
|
||||
}
|
||||
_, err = p.tx.Exec(ctx, `
|
||||
INSERT INTO attachments (id, chat_id, owner_id, filename, file_hash, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)`,
|
||||
a.ID, a.ChatID, a.OwnerID, a.Filename, a.FileHash, a.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Attachments) GetForMember(ctx context.Context, id, actor uuid.UUID) (attachment.Attachment, error) {
|
||||
var a attachment.Attachment
|
||||
err := r.pool.QueryRow(ctx, `
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.file_hash,
|
||||
f.size_bytes, f.mime_type, f.status, a.created_at
|
||||
// A single statement returns one snapshot of the published version, even
|
||||
// when a worker completes a newer version during a download.
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.file_hash, f.size_bytes, f.mime_type,
|
||||
s.status, s.error_code, a.created_at, s.updated_at,
|
||||
coalesce(v.variant,''),coalesce(v.object_key,''),coalesce(v.mime_type,''),
|
||||
coalesce(v.size_bytes,0),coalesce(v.width,0),coalesce(v.height,0),v.sha256
|
||||
FROM attachments a
|
||||
JOIN files f ON f.hash = a.file_hash
|
||||
WHERE a.id = $1 AND EXISTS (
|
||||
SELECT 1 FROM chat_members m
|
||||
WHERE m.chat_id = a.chat_id AND m.user_id = $2
|
||||
)`, id, actor).
|
||||
Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.FileHash,
|
||||
&a.SizeBytes, &a.MIMEType, &a.Status, &a.CreatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return attachment.Attachment{}, apperr.NotFound("attachment not found")
|
||||
JOIN chat_members m ON m.chat_id=a.chat_id AND m.user_id=$2
|
||||
JOIN files f ON f.hash=a.file_hash
|
||||
JOIN attachment_states s ON s.id=a.id
|
||||
LEFT JOIN attachment_variants v ON v.attachment_id=a.id AND v.pipeline_version=a.published_version AND s.status='ready'
|
||||
WHERE a.id=$1 ORDER BY v.variant`, id, actor)
|
||||
if err != nil {
|
||||
return a, postgres.Classify(err)
|
||||
}
|
||||
return a, postgres.Classify(err)
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var v attachment.Variant
|
||||
err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.FileHash, &a.SizeBytes, &a.MIMEType,
|
||||
&a.Status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt,
|
||||
&v.Name, &v.ObjectKey, &v.MIMEType, &v.SizeBytes, &v.Width, &v.Height, &v.Hash)
|
||||
if err != nil {
|
||||
return attachment.Attachment{}, postgres.Classify(err)
|
||||
}
|
||||
if v.Name != "" {
|
||||
a.Variants = append(a.Variants, v)
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return attachment.Attachment{}, postgres.Classify(err)
|
||||
}
|
||||
if a.ID == uuid.Nil {
|
||||
return a, apperr.NotFound("attachment not found")
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user