lab5: очередь RabbitMQ, outbox, worker, варианты, fault-injection
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
// Package broker — RabbitMQ для заданий обработки вложений.
|
||||
//
|
||||
// Топология (объявляется идемпотентно при каждом подключении):
|
||||
//
|
||||
// exchange attachments (direct, durable) ──process──► queue attachment.process
|
||||
// quorum-очередь (реплицируемый журнал на диске), x-delivery-limit=10:
|
||||
// сообщение, которое 10 раз вернулось без ack (poison), уходит в DLX
|
||||
// exchange attachments.dlx (fanout, durable) ──► queue attachment.dead (реестр «мёртвых»)
|
||||
//
|
||||
// Publisher confirm подтверждает, что broker устойчиво ПРИНЯЛ сообщение (записал
|
||||
// в quorum-журнал); consumer ack подтверждает, что worker устойчиво ОБРАБОТАЛ его.
|
||||
// Это разные границы: между ними сообщение может быть доставлено несколько раз.
|
||||
package broker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
const (
|
||||
Exchange = "attachments"
|
||||
RoutingKey = "process"
|
||||
Queue = "attachment.process"
|
||||
DeadExchange = "attachments.dlx"
|
||||
DeadQueue = "attachment.dead"
|
||||
DeliveryLimit = 10
|
||||
)
|
||||
|
||||
// Declare объявляет топологию; повторный вызов с теми же аргументами — no-op.
|
||||
func Declare(ch *amqp.Channel) error {
|
||||
if err := ch.ExchangeDeclare(Exchange, "direct", true, false, false, false, nil); err != nil {
|
||||
return fmt.Errorf("declare exchange: %w", err)
|
||||
}
|
||||
if err := ch.ExchangeDeclare(DeadExchange, "fanout", true, false, false, false, nil); err != nil {
|
||||
return fmt.Errorf("declare dlx: %w", err)
|
||||
}
|
||||
if _, err := ch.QueueDeclare(DeadQueue, true, false, false, false, amqp.Table{"x-queue-type": "quorum"}); err != nil {
|
||||
return fmt.Errorf("declare dead queue: %w", err)
|
||||
}
|
||||
if err := ch.QueueBind(DeadQueue, "", DeadExchange, false, nil); err != nil {
|
||||
return fmt.Errorf("bind dead queue: %w", err)
|
||||
}
|
||||
args := amqp.Table{
|
||||
"x-queue-type": "quorum",
|
||||
"x-dead-letter-exchange": DeadExchange,
|
||||
"x-delivery-limit": int32(DeliveryLimit),
|
||||
}
|
||||
if _, err := ch.QueueDeclare(Queue, true, false, false, false, args); err != nil {
|
||||
return fmt.Errorf("declare queue: %w", err)
|
||||
}
|
||||
if err := ch.QueueBind(Queue, RoutingKey, Exchange, false, nil); err != nil {
|
||||
return fmt.Errorf("bind queue: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Publisher публикует с publisher confirms.
|
||||
type Publisher struct {
|
||||
conn *amqp.Connection
|
||||
ch *amqp.Channel
|
||||
}
|
||||
|
||||
func NewPublisher(url string) (*Publisher, error) {
|
||||
conn, err := amqp.DialConfig(url, amqp.Config{Heartbeat: 10 * time.Second, Properties: amqp.Table{"connection_name": "relay"}})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("dial broker: %w", err)
|
||||
}
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := Declare(ch); err != nil {
|
||||
conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := ch.Confirm(false); err != nil {
|
||||
conn.Close()
|
||||
return nil, fmt.Errorf("enable confirms: %w", err)
|
||||
}
|
||||
return &Publisher{conn: conn, ch: ch}, nil
|
||||
}
|
||||
|
||||
func (p *Publisher) Closed() bool { return p.conn.IsClosed() || p.ch.IsClosed() }
|
||||
|
||||
func (p *Publisher) Close() { _ = p.conn.Close() }
|
||||
|
||||
// Publish отправляет persistent-сообщение и ждёт confirm (ack от broker).
|
||||
// nack или таймаут — ошибка: outbox не помечается опубликованным.
|
||||
func (p *Publisher) Publish(ctx context.Context, messageID string, body []byte) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
dc, err := p.ch.PublishWithDeferredConfirmWithContext(ctx, Exchange, RoutingKey, true, false, amqp.Publishing{
|
||||
ContentType: "application/json",
|
||||
DeliveryMode: amqp.Persistent,
|
||||
MessageId: messageID,
|
||||
Timestamp: time.Now(),
|
||||
Type: "attachment.process.v1",
|
||||
Body: body,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("publish: %w", err)
|
||||
}
|
||||
ok, err := dc.WaitContext(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("publish confirm: %w", err)
|
||||
}
|
||||
if !ok {
|
||||
return errors.New("publish nacked by broker")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Consumer — подписка с ручным ack и prefetch.
|
||||
type Consumer struct {
|
||||
conn *amqp.Connection
|
||||
ch *amqp.Channel
|
||||
}
|
||||
|
||||
func NewConsumer(url string, prefetch int, name string) (*Consumer, <-chan amqp.Delivery, error) {
|
||||
conn, err := amqp.DialConfig(url, amqp.Config{Heartbeat: 10 * time.Second, Properties: amqp.Table{"connection_name": name}})
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("dial broker: %w", err)
|
||||
}
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := Declare(ch); err != nil {
|
||||
conn.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
// prefetch = максимум неподтверждённых сообщений у этого worker:
|
||||
// ограничивает конкуренцию и не даёт одному worker забрать всю очередь.
|
||||
if err := ch.Qos(prefetch, 0, false); err != nil {
|
||||
conn.Close()
|
||||
return nil, nil, fmt.Errorf("qos: %w", err)
|
||||
}
|
||||
deliveries, err := ch.Consume(Queue, name, false, false, false, false, nil)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, nil, fmt.Errorf("consume: %w", err)
|
||||
}
|
||||
return &Consumer{conn: conn, ch: ch}, deliveries, nil
|
||||
}
|
||||
|
||||
// Cancel прекращает получение новых сообщений; неподтверждённые вернутся в очередь.
|
||||
func (c *Consumer) Cancel(name string) { _ = c.ch.Cancel(name, false) }
|
||||
|
||||
func (c *Consumer) Close() { _ = c.conn.Close() }
|
||||
|
||||
// Ping проверяет соединение (для readiness/heartbeat-проверок).
|
||||
func Ping(url string) error {
|
||||
conn, err := amqp.DialConfig(url, amqp.Config{Heartbeat: 10 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return conn.Close()
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
Env string
|
||||
SessionTTLSeconds int
|
||||
DatabaseURL string
|
||||
CacheURL string
|
||||
SessionStore string
|
||||
LogLevel slog.Level
|
||||
ShutdownTimeout time.Duration
|
||||
CacheEnabled bool
|
||||
InstanceID string
|
||||
IdempotencyTTL time.Duration
|
||||
DrainDelay time.Duration
|
||||
DBPoolMax int32
|
||||
CacheInvalidation string
|
||||
InternalAddr string
|
||||
S3Endpoint string
|
||||
S3Region string
|
||||
S3Bucket string
|
||||
S3AccessKey string
|
||||
S3SecretKey string
|
||||
MaxUploadBytes int64
|
||||
MaxImagePixels int64
|
||||
// Pipeline (lab5)
|
||||
BrokerURL string
|
||||
PipelineVersion int
|
||||
JobMaxAttempts int
|
||||
JobTimeout time.Duration
|
||||
WorkerConcurrency int
|
||||
StagingRetention time.Duration
|
||||
WebPQuality int
|
||||
PixelBudget int64
|
||||
RetryBaseDelay time.Duration
|
||||
RetryMaxDelay time.Duration
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
return Config{
|
||||
Host: getEnv("APP_HOST", "0.0.0.0"),
|
||||
Port: getEnv("APP_PORT", "8080"),
|
||||
Env: getEnv("APP_ENV", "lab"),
|
||||
SessionTTLSeconds: getInt("SESSION_TTL_SECONDS", 3600),
|
||||
DatabaseURL: readSecretFile("DATABASE_URL_FILE"),
|
||||
CacheURL: readSecretFile("CACHE_URL_FILE"),
|
||||
SessionStore: getEnv("SESSION_STORE", "postgres"),
|
||||
LogLevel: parseLevel(getEnv("LOG_LEVEL", "info")),
|
||||
ShutdownTimeout: parseDuration(getEnv("SHUTDOWN_TIMEOUT", "8s")),
|
||||
CacheEnabled: getEnv("CACHE_ENABLED", "true") == "true",
|
||||
InstanceID: instanceID(),
|
||||
IdempotencyTTL: time.Duration(getInt("IDEMPOTENCY_TTL_SECONDS", 86400)) * time.Second,
|
||||
DrainDelay: parseDuration(getEnv("DRAIN_DELAY", "3s")),
|
||||
DBPoolMax: int32(max(1, getInt("DB_POOL_MAX", 20))),
|
||||
CacheInvalidation: getEnv("CACHE_INVALIDATION", "version"),
|
||||
InternalAddr: getEnv("INTERNAL_ADDR", ":9090"),
|
||||
S3Endpoint: getEnv("S3_ENDPOINT", "http://s3:9000"),
|
||||
S3Region: getEnv("S3_REGION", "us-east-1"),
|
||||
S3Bucket: getEnv("S3_BUCKET", "messenger-attachments"),
|
||||
S3AccessKey: readSecretFile("S3_ACCESS_KEY_ID_FILE"),
|
||||
S3SecretKey: readSecretFile("S3_SECRET_ACCESS_KEY_FILE"),
|
||||
MaxUploadBytes: int64(getInt("MAX_UPLOAD_BYTES", 10*1024*1024)),
|
||||
MaxImagePixels: int64(getInt("MAX_IMAGE_PIXELS", 40_000_000)),
|
||||
BrokerURL: readSecretFile("BROKER_URL_FILE"),
|
||||
PipelineVersion: max(1, getInt("PIPELINE_VERSION", 1)),
|
||||
JobMaxAttempts: max(1, getInt("JOB_MAX_ATTEMPTS", 5)),
|
||||
JobTimeout: time.Duration(max(5, getInt("JOB_TIMEOUT_SECONDS", 60))) * time.Second,
|
||||
WorkerConcurrency: max(1, getInt("WORKER_CONCURRENCY", 2)),
|
||||
StagingRetention: time.Duration(max(60, getInt("STAGING_RETENTION_SECONDS", 86400))) * time.Second,
|
||||
WebPQuality: min(100, max(1, getInt("WEBP_QUALITY", 80))),
|
||||
PixelBudget: int64(max(1_000_000, getInt("WORKER_PIXEL_BUDGET", 60_000_000))),
|
||||
RetryBaseDelay: parseDuration(getEnv("RETRY_BASE_DELAY", "2s")),
|
||||
RetryMaxDelay: parseDuration(getEnv("RETRY_MAX_DELAY", "60s")),
|
||||
}
|
||||
}
|
||||
|
||||
// instanceID — идентификатор реплики для X-Instance-Id. По умолчанию hostname,
|
||||
// который в Docker равен короткому ID контейнера: так stop-one.sh однозначно
|
||||
// находит контейнер. В lab7 его заменит непрозрачный alias.
|
||||
func instanceID() string {
|
||||
if v := os.Getenv("INSTANCE_ID"); v != "" {
|
||||
return v
|
||||
}
|
||||
if h, err := os.Hostname(); err == nil && h != "" {
|
||||
return h
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func (c Config) SessionTTL() time.Duration {
|
||||
return time.Duration(c.SessionTTLSeconds) * time.Second
|
||||
}
|
||||
|
||||
func (c Config) Addr() string { return c.Host + ":" + c.Port }
|
||||
|
||||
func getEnv(key, def string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func getInt(key string, def int) int {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func readSecretFile(envKey string) string {
|
||||
path := os.Getenv(envKey)
|
||||
if path == "" {
|
||||
return ""
|
||||
}
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
slog.Warn("secret file not readable", "path", path, "err", err)
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(string(b))
|
||||
}
|
||||
|
||||
func parseLevel(s string) slog.Level {
|
||||
var l slog.Level
|
||||
if err := l.UnmarshalText([]byte(s)); err != nil {
|
||||
return slog.LevelInfo
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func parseDuration(s string) time.Duration {
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
return 8 * time.Second
|
||||
}
|
||||
return d
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
AttachmentUploading = "uploading" // внутреннее намерение до 202, клиенту не видно
|
||||
AttachmentQueued = "queued"
|
||||
AttachmentProcessing = "processing"
|
||||
AttachmentReady = "ready"
|
||||
AttachmentFailed = "failed"
|
||||
AttachmentMissing = "missing" // карантин reconciler
|
||||
)
|
||||
|
||||
// Variant — результат обработки одной версии pipeline.
|
||||
type Variant struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA256 string `json:"sha256"`
|
||||
ObjectKey string `json:"-"`
|
||||
PipelineVersion int `json:"-"`
|
||||
}
|
||||
|
||||
// Attachment — метаданные вложения. Ключи S3 никогда не уходят клиенту.
|
||||
type Attachment struct {
|
||||
ID string
|
||||
ChatID string
|
||||
OwnerID string
|
||||
Filename string
|
||||
ObjectKey string // текущее место оригинала: staging до ready, конечный prefix после
|
||||
StagingKey string // staging-объект, пока он не удалён после публикации
|
||||
Status string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
SHA256 string
|
||||
ErrorCode string
|
||||
PipelineVersion int // версия, чей результат опубликован (0 — ещё нет)
|
||||
Variants []Variant
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// MarshalJSON — представление по контракту: queued/processing/ready/failed,
|
||||
// варианты текущей опубликованной версии, error_code только у failed.
|
||||
func (a Attachment) MarshalJSON() ([]byte, error) {
|
||||
variants := a.Variants
|
||||
if variants == nil {
|
||||
variants = []Variant{}
|
||||
}
|
||||
var errCode *string
|
||||
if a.Status == AttachmentFailed && a.ErrorCode != "" {
|
||||
ec := a.ErrorCode
|
||||
errCode = &ec
|
||||
}
|
||||
return json.Marshal(struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []Variant `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}{
|
||||
ID: a.ID, ChatID: a.ChatID, OwnerID: a.OwnerID, Filename: a.Filename,
|
||||
ContentType: a.ContentType, SizeBytes: a.SizeBytes, SHA256: a.SHA256,
|
||||
Status: a.Status, Variants: variants, ErrorCode: errCode,
|
||||
CreatedAt: a.CreatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
|
||||
UpdatedAt: a.UpdatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
// Job — сообщение broker по contracts/jobs.schema.json. Только ID и служебные
|
||||
// поля: ни байтов, ни URL, ни credentials.
|
||||
type Job struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
JobID string `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Traceparent string `json:"traceparent,omitempty"`
|
||||
}
|
||||
|
||||
const JobType = "attachment.process.v1"
|
||||
@@ -0,0 +1,30 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (c Chat) MarshalJSON() ([]byte, error) {
|
||||
type Alias Chat
|
||||
members := c.MemberIDs
|
||||
if members == nil {
|
||||
members = []string{}
|
||||
}
|
||||
return json.Marshal(&struct {
|
||||
Alias
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}{
|
||||
Alias: Alias(c),
|
||||
MemberIDs: members,
|
||||
CreatedAt: c.CreatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// AttachmentIDs — в порядке отправки; всегда массив (пустой, если вложений нет).
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
func (m Message) MarshalJSON() ([]byte, error) {
|
||||
type Alias Message
|
||||
if m.AttachmentIDs == nil {
|
||||
m.AttachmentIDs = []string{}
|
||||
}
|
||||
return json.Marshal(&struct {
|
||||
Alias
|
||||
CreatedAt string `json:"created_at"`
|
||||
}{
|
||||
Alias: Alias(m),
|
||||
CreatedAt: m.CreatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Session struct {
|
||||
Token string `json:"token"`
|
||||
UserID string `json:"-"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// MarshalJSON форматирует CreatedAt в RFC3339 без микросекунд.
|
||||
func (u User) MarshalJSON() ([]byte, error) {
|
||||
type Alias User
|
||||
return json.Marshal(&struct {
|
||||
Alias
|
||||
CreatedAt string `json:"created_at"`
|
||||
}{
|
||||
Alias: Alias(u),
|
||||
CreatedAt: u.CreatedAt.UTC().Truncate(time.Second).Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,592 @@
|
||||
// Package imagecheck потоково проверяет PNG/JPEG без декодирования пикселей.
|
||||
//
|
||||
// Проверка идёт по мере чтения (используется параллельно с PUT в S3), память
|
||||
// O(1) от размера изображения: размеры берутся из заголовка (IHDR/SOF) и
|
||||
// сравниваются с лимитом до чтения тела; PNG — CRC каждого chunk, длина
|
||||
// распакованного IDAT-потока (zlib → io.Discard) и байт фильтра каждой строки,
|
||||
// JPEG — структура сегментов и маркер EOI. «Заведомо повреждённые» файлы
|
||||
// (обрыв, битый CRC, мусор вместо данных, чужой формат) дают ErrInvalid,
|
||||
// слишком большие по пикселям — ErrTooLarge.
|
||||
package imagecheck
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
)
|
||||
|
||||
const (
|
||||
PNG = "image/png"
|
||||
JPEG = "image/jpeg"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalid = errors.New("invalid or unsupported image")
|
||||
ErrTooLarge = errors.New("image dimensions exceed limit")
|
||||
)
|
||||
|
||||
var pngSignature = []byte("\x89PNG\r\n\x1a\n")
|
||||
|
||||
// Sniff определяет формат по первым байтам (не по заголовку Content-Type клиента).
|
||||
func Sniff(head []byte) (string, bool) {
|
||||
switch {
|
||||
case bytes.HasPrefix(head, pngSignature):
|
||||
return PNG, true
|
||||
case len(head) >= 3 && head[0] == 0xFF && head[1] == 0xD8 && head[2] == 0xFF:
|
||||
return JPEG, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Result — что удалось узнать при проверке.
|
||||
type Result struct {
|
||||
Format string
|
||||
Width int
|
||||
Height int
|
||||
}
|
||||
|
||||
// Validate читает r до конца и проверяет изображение формата format.
|
||||
// maxPixels ограничивает width*height. Хвост после конца изображения
|
||||
// дочитывается и игнорируется, чтобы не блокировать писателя.
|
||||
func Validate(r io.Reader, format string, maxPixels int64) (Result, error) {
|
||||
br := bufio.NewReaderSize(r, 32*1024)
|
||||
var res Result
|
||||
var err error
|
||||
switch format {
|
||||
case PNG:
|
||||
res, err = validatePNG(br, maxPixels)
|
||||
case JPEG:
|
||||
res, err = validateJPEG(br, maxPixels)
|
||||
default:
|
||||
return Result{}, ErrInvalid
|
||||
}
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, br)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func invalid(format string, args ...any) error {
|
||||
return fmt.Errorf("%w: %s", ErrInvalid, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// --- PNG ---
|
||||
|
||||
func channels(colorType byte) (int, bool) {
|
||||
switch colorType {
|
||||
case 0:
|
||||
return 1, true // grayscale
|
||||
case 2:
|
||||
return 3, true // RGB
|
||||
case 3:
|
||||
return 1, true // palette
|
||||
case 4:
|
||||
return 2, true // gray + alpha
|
||||
case 6:
|
||||
return 4, true // RGBA
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func validBitDepth(colorType, depth byte) bool {
|
||||
switch colorType {
|
||||
case 0:
|
||||
return depth == 1 || depth == 2 || depth == 4 || depth == 8 || depth == 16
|
||||
case 3:
|
||||
return depth == 1 || depth == 2 || depth == 4 || depth == 8
|
||||
case 2, 4, 6:
|
||||
return depth == 8 || depth == 16
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// pngRowLen — длина одной отсканированной строки прохода: 1 байт фильтра +
|
||||
// данные пикселей.
|
||||
func pngRowLen(pw, bitsPerPixel int64) int64 {
|
||||
return 1 + (pw*bitsPerPixel+7)/8
|
||||
}
|
||||
|
||||
// expectedRaw — длина распакованного потока IDAT: строки с байтом фильтра.
|
||||
func expectedRaw(w, h int64, bitsPerPixel int64, interlaced bool) int64 {
|
||||
if !interlaced {
|
||||
return h * pngRowLen(w, bitsPerPixel)
|
||||
}
|
||||
passes := [7][4]int64{{0, 0, 8, 8}, {4, 0, 8, 8}, {0, 4, 4, 8}, {2, 0, 4, 4}, {0, 2, 2, 4}, {1, 0, 2, 2}, {0, 1, 1, 2}}
|
||||
var total int64
|
||||
for _, p := range passes {
|
||||
if w <= p[0] || h <= p[1] {
|
||||
continue
|
||||
}
|
||||
pw := (w - p[0] + p[2] - 1) / p[2]
|
||||
ph := (h - p[1] + p[3] - 1) / p[3]
|
||||
total += ph * pngRowLen(pw, bitsPerPixel)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// pngRows — итератор по (pw, ph) проходов Adam7 или единственного прохода.
|
||||
// Используется, чтобы валидировать байт фильтра каждой строки, не зная
|
||||
// заранее, как разбивается распакованный поток на проходы.
|
||||
type pngPass struct {
|
||||
pw, ph int64
|
||||
}
|
||||
|
||||
func pngPasses(w, h int64, interlaced bool) []pngPass {
|
||||
if !interlaced {
|
||||
return []pngPass{{w, h}}
|
||||
}
|
||||
coords := [7][4]int64{{0, 0, 8, 8}, {4, 0, 8, 8}, {0, 4, 4, 8}, {2, 0, 4, 4}, {0, 2, 2, 4}, {1, 0, 2, 2}, {0, 1, 1, 2}}
|
||||
out := make([]pngPass, 0, len(coords))
|
||||
for _, p := range coords {
|
||||
if w <= p[0] || h <= p[1] {
|
||||
continue
|
||||
}
|
||||
pw := (w - p[0] + p[2] - 1) / p[2]
|
||||
ph := (h - p[1] + p[3] - 1) / p[3]
|
||||
out = append(out, pngPass{pw, ph})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type pngChunk struct {
|
||||
length uint32
|
||||
typ [4]byte
|
||||
}
|
||||
|
||||
func readChunkHeader(br *bufio.Reader) (pngChunk, error) {
|
||||
var hdr [8]byte
|
||||
if _, err := io.ReadFull(br, hdr[:]); err != nil {
|
||||
return pngChunk{}, invalid("truncated chunk header")
|
||||
}
|
||||
c := pngChunk{length: binary.BigEndian.Uint32(hdr[:4])}
|
||||
copy(c.typ[:], hdr[4:])
|
||||
if c.length > 1<<31-1 {
|
||||
return c, invalid("chunk too long")
|
||||
}
|
||||
for _, b := range c.typ {
|
||||
if !((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z')) {
|
||||
return c, invalid("bad chunk type")
|
||||
}
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// readChunkData читает тело chunk в dst (если не nil) и проверяет CRC.
|
||||
func readChunkData(br *bufio.Reader, c pngChunk, dst io.Writer) error {
|
||||
h := crc32.NewIEEE()
|
||||
h.Write(c.typ[:])
|
||||
w := io.Writer(h)
|
||||
if dst != nil {
|
||||
w = io.MultiWriter(h, dst)
|
||||
}
|
||||
if _, err := io.CopyN(w, br, int64(c.length)); err != nil {
|
||||
return invalid("truncated %s chunk", c.typ[:])
|
||||
}
|
||||
var crc [4]byte
|
||||
if _, err := io.ReadFull(br, crc[:]); err != nil {
|
||||
return invalid("truncated CRC")
|
||||
}
|
||||
if binary.BigEndian.Uint32(crc[:]) != h.Sum32() {
|
||||
return invalid("CRC mismatch in %s", c.typ[:])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// idatStream отдаёт данные подряд идущих IDAT-chunk как один поток.
|
||||
type idatStream struct {
|
||||
br *bufio.Reader
|
||||
remaining uint32
|
||||
crc uint32
|
||||
next *pngChunk // первый chunk после последнего IDAT
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *idatStream) Read(p []byte) (int, error) {
|
||||
for s.remaining == 0 {
|
||||
if s.err != nil || s.next != nil {
|
||||
if s.err != nil {
|
||||
return 0, s.err
|
||||
}
|
||||
return 0, io.EOF
|
||||
}
|
||||
if err := s.finishChunk(); err != nil {
|
||||
s.err = err
|
||||
return 0, err
|
||||
}
|
||||
c, err := readChunkHeader(s.br)
|
||||
if err != nil {
|
||||
s.err = err
|
||||
return 0, err
|
||||
}
|
||||
if string(c.typ[:]) != "IDAT" {
|
||||
s.next = &c
|
||||
return 0, io.EOF
|
||||
}
|
||||
s.startChunk(c)
|
||||
}
|
||||
if uint32(len(p)) > s.remaining {
|
||||
p = p[:s.remaining]
|
||||
}
|
||||
n, err := s.br.Read(p)
|
||||
s.crc = crc32.Update(s.crc, crc32.IEEETable, p[:n])
|
||||
s.remaining -= uint32(n)
|
||||
if err != nil && err != io.EOF {
|
||||
s.err = err
|
||||
return n, err
|
||||
}
|
||||
if n == 0 && err == io.EOF {
|
||||
s.err = invalid("truncated IDAT")
|
||||
return 0, s.err
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (s *idatStream) startChunk(c pngChunk) {
|
||||
s.remaining = c.length
|
||||
s.crc = crc32.Update(0, crc32.IEEETable, c.typ[:])
|
||||
}
|
||||
|
||||
func (s *idatStream) finishChunk() error {
|
||||
var crc [4]byte
|
||||
if _, err := io.ReadFull(s.br, crc[:]); err != nil {
|
||||
return invalid("truncated CRC")
|
||||
}
|
||||
if binary.BigEndian.Uint32(crc[:]) != s.crc {
|
||||
return invalid("CRC mismatch in IDAT")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validatePNGStream проверяет распакованный поток: длину, кратность строкам
|
||||
// и байты фильтра (0..4) в начале каждой строки каждого прохода.
|
||||
func validatePNGStream(zr io.Reader, passes []pngPass, bitsPerPixel, want int64) error {
|
||||
total := int64(0)
|
||||
buf := make([]byte, 32*1024)
|
||||
for _, pass := range passes {
|
||||
rowLen := pngRowLen(pass.pw, bitsPerPixel)
|
||||
for y := int64(0); y < pass.ph; y++ {
|
||||
// читаем ровно одну строку, но чанками, чтобы не аллоцировать
|
||||
// rowLen (может быть большой для широких изображений)
|
||||
left := rowLen
|
||||
first := true
|
||||
for left > 0 {
|
||||
chunk := int64(len(buf))
|
||||
if left < chunk {
|
||||
chunk = left
|
||||
}
|
||||
n, err := io.ReadFull(zr, buf[:chunk])
|
||||
if err != nil {
|
||||
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) {
|
||||
return invalid("truncated IDAT (got %d of %d)", total, want)
|
||||
}
|
||||
return invalid("corrupt image data")
|
||||
}
|
||||
if first {
|
||||
if buf[0] > 4 {
|
||||
return invalid("bad row filter %d", buf[0])
|
||||
}
|
||||
first = false
|
||||
}
|
||||
total += int64(n)
|
||||
left -= int64(n)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Проверяем, что распакованный поток ровно want байт (ни больше, ни меньше).
|
||||
var extra [1]byte
|
||||
n, err := zr.Read(extra[:])
|
||||
if n != 0 {
|
||||
return invalid("image data longer than expected")
|
||||
}
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
return invalid("corrupt image data")
|
||||
}
|
||||
if total != want {
|
||||
return invalid("image data length %d, want %d", total, want)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePNG(br *bufio.Reader, maxPixels int64) (Result, error) {
|
||||
res := Result{Format: PNG}
|
||||
sig := make([]byte, 8)
|
||||
if _, err := io.ReadFull(br, sig); err != nil || !bytes.Equal(sig, pngSignature) {
|
||||
return res, invalid("not a PNG")
|
||||
}
|
||||
c, err := readChunkHeader(br)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if string(c.typ[:]) != "IHDR" || c.length != 13 {
|
||||
return res, invalid("IHDR must be first")
|
||||
}
|
||||
var ihdr bytes.Buffer
|
||||
if err := readChunkData(br, c, &ihdr); err != nil {
|
||||
return res, err
|
||||
}
|
||||
b := ihdr.Bytes()
|
||||
w := int64(binary.BigEndian.Uint32(b[0:4]))
|
||||
h := int64(binary.BigEndian.Uint32(b[4:8]))
|
||||
depth, colorType, compression, filter, interlace := b[8], b[9], b[10], b[11], b[12]
|
||||
res.Width, res.Height = int(w), int(h)
|
||||
if w < 1 || h < 1 || w > 1<<31-1 || h > 1<<31-1 {
|
||||
return res, invalid("bad dimensions")
|
||||
}
|
||||
// Лимит пикселей — до чтения и распаковки данных изображения.
|
||||
if w*h > maxPixels {
|
||||
return res, ErrTooLarge
|
||||
}
|
||||
ch, ok := channels(colorType)
|
||||
if !ok || !validBitDepth(colorType, depth) || compression != 0 || filter != 0 || interlace > 1 {
|
||||
return res, invalid("unsupported IHDR")
|
||||
}
|
||||
bpp := int64(ch) * int64(depth)
|
||||
want := expectedRaw(w, h, bpp, interlace == 1)
|
||||
passes := pngPasses(w, h, interlace == 1)
|
||||
|
||||
sawIDAT := false
|
||||
for {
|
||||
c, err := readChunkHeader(br)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
switch string(c.typ[:]) {
|
||||
case "IDAT":
|
||||
if sawIDAT {
|
||||
return res, invalid("non-consecutive IDAT")
|
||||
}
|
||||
sawIDAT = true
|
||||
s := &idatStream{br: br}
|
||||
s.startChunk(c)
|
||||
zr, err := zlib.NewReader(s)
|
||||
if err != nil {
|
||||
return res, invalid("bad zlib header")
|
||||
}
|
||||
// Распаковка и проверка фильтров строк — без хранения пикселей.
|
||||
if err := validatePNGStream(zr, passes, bpp, want); err != nil {
|
||||
// Закрываем zlib, чтобы дочитать хвост CRC в idatStream, но
|
||||
// игнорируем ошибку закрытия: она не важна для результата.
|
||||
_ = zr.Close()
|
||||
return res, err
|
||||
}
|
||||
if err := zr.Close(); err != nil && !errors.Is(err, io.EOF) {
|
||||
return res, invalid("bad zlib trailer")
|
||||
}
|
||||
// Остаток IDAT (например, пустые chunk) дочитываем с проверкой CRC.
|
||||
if _, err := io.Copy(io.Discard, s); err != nil {
|
||||
return res, err
|
||||
}
|
||||
if s.next == nil {
|
||||
return res, invalid("missing IEND")
|
||||
}
|
||||
c = *s.next
|
||||
if string(c.typ[:]) == "IEND" {
|
||||
if err := readChunkData(br, c, nil); err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
if err := readChunkData(br, c, nil); err != nil {
|
||||
return res, err
|
||||
}
|
||||
case "IEND":
|
||||
if !sawIDAT {
|
||||
return res, invalid("no image data")
|
||||
}
|
||||
if err := readChunkData(br, c, nil); err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, nil
|
||||
case "IHDR":
|
||||
return res, invalid("duplicate IHDR")
|
||||
default:
|
||||
if err := readChunkData(br, c, nil); err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- JPEG ---
|
||||
|
||||
func isSOF(m byte) bool {
|
||||
switch m {
|
||||
case 0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// readMarker читает 0xFF (с заполнителями) и код маркера.
|
||||
func readMarker(br *bufio.Reader) (byte, error) {
|
||||
b, err := br.ReadByte()
|
||||
if err != nil {
|
||||
return 0, invalid("truncated JPEG")
|
||||
}
|
||||
if b != 0xFF {
|
||||
return 0, invalid("expected JPEG marker")
|
||||
}
|
||||
for b == 0xFF {
|
||||
if b, err = br.ReadByte(); err != nil {
|
||||
return 0, invalid("truncated JPEG")
|
||||
}
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func segmentLength(br *bufio.Reader) (int, error) {
|
||||
var l [2]byte
|
||||
if _, err := io.ReadFull(br, l[:]); err != nil {
|
||||
return 0, invalid("truncated JPEG segment")
|
||||
}
|
||||
n := int(binary.BigEndian.Uint16(l[:]))
|
||||
if n < 2 {
|
||||
return 0, invalid("bad JPEG segment length")
|
||||
}
|
||||
return n - 2, nil
|
||||
}
|
||||
|
||||
// skipEntropy пропускает сжатые данные скана и возвращает следующий маркер.
|
||||
func skipEntropy(br *bufio.Reader) (byte, error) {
|
||||
for {
|
||||
b, err := br.ReadByte()
|
||||
if err != nil {
|
||||
return 0, invalid("truncated JPEG scan")
|
||||
}
|
||||
if b != 0xFF {
|
||||
continue
|
||||
}
|
||||
for b == 0xFF {
|
||||
if b, err = br.ReadByte(); err != nil {
|
||||
return 0, invalid("truncated JPEG scan")
|
||||
}
|
||||
}
|
||||
if b == 0x00 || (b >= 0xD0 && b <= 0xD7) {
|
||||
continue // байт-заполнитель или RSTn внутри скана
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
}
|
||||
|
||||
// readSOF читает сегмент SOF и проверяет его структуру (ITU T.81, B.2.2):
|
||||
// P(1) Y(2) X(2) Nf(1), затем Nf компонент по 3 байта. n — длина сегмента
|
||||
// без двух байт поля длины. Возвращает (width, height).
|
||||
func readSOF(br *bufio.Reader, n int) (int64, int64, error) {
|
||||
if n < 6 {
|
||||
return 0, 0, invalid("short SOF")
|
||||
}
|
||||
hdr := make([]byte, 6)
|
||||
if _, err := io.ReadFull(br, hdr); err != nil {
|
||||
return 0, 0, invalid("truncated SOF")
|
||||
}
|
||||
// hdr[0] — точность выборки (precision), не число компонент.
|
||||
h := int64(binary.BigEndian.Uint16(hdr[1:3]))
|
||||
w := int64(binary.BigEndian.Uint16(hdr[3:5]))
|
||||
nf := int(hdr[5])
|
||||
if nf == 0 {
|
||||
return 0, 0, invalid("SOF without components")
|
||||
}
|
||||
if n != 6+3*nf {
|
||||
return 0, 0, invalid("SOF length %d, want %d", n, 6+3*nf)
|
||||
}
|
||||
if _, err := br.Discard(3 * nf); err != nil {
|
||||
return 0, 0, invalid("truncated SOF")
|
||||
}
|
||||
return w, h, nil
|
||||
}
|
||||
|
||||
func validateJPEG(br *bufio.Reader, maxPixels int64) (Result, error) {
|
||||
res := Result{Format: JPEG}
|
||||
var soi [2]byte
|
||||
if _, err := io.ReadFull(br, soi[:]); err != nil || soi != [2]byte{0xFF, 0xD8} {
|
||||
return res, invalid("not a JPEG")
|
||||
}
|
||||
sawSOF, sawSOS := false, false
|
||||
marker, err := readMarker(br)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
for {
|
||||
switch {
|
||||
case marker == 0xD9: // EOI
|
||||
if !sawSOF || !sawSOS {
|
||||
return res, invalid("JPEG without frame or scan")
|
||||
}
|
||||
return res, nil
|
||||
case marker == 0xD8:
|
||||
return res, invalid("unexpected SOI")
|
||||
case marker == 0x01 || (marker >= 0xD0 && marker <= 0xD7):
|
||||
// маркеры без длины
|
||||
case isSOF(marker):
|
||||
n, err := segmentLength(br)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if sawSOF {
|
||||
// читаем сегмент, но так и так вернём ошибку
|
||||
_, _, _ = readSOF(br, n)
|
||||
return res, invalid("multiple frames")
|
||||
}
|
||||
w, h, err := readSOF(br, n)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Width, res.Height = int(w), int(h)
|
||||
if w < 1 || h < 1 {
|
||||
return res, invalid("bad JPEG dimensions")
|
||||
}
|
||||
if w*h > maxPixels {
|
||||
return res, ErrTooLarge
|
||||
}
|
||||
sawSOF = true
|
||||
case marker == 0xDA: // SOS
|
||||
if !sawSOF {
|
||||
return res, invalid("scan before frame")
|
||||
}
|
||||
n, err := segmentLength(br)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
// SOS: Ns + Ss + Se + Ah/Al + 2*Ns = 1 + 3 + 2*Ns = 4 + 2*Ns
|
||||
if n < 4 {
|
||||
return res, invalid("short SOS")
|
||||
}
|
||||
nsBuf := make([]byte, 1)
|
||||
if _, err := io.ReadFull(br, nsBuf); err != nil {
|
||||
return res, invalid("truncated SOS")
|
||||
}
|
||||
ns := int(nsBuf[0])
|
||||
if ns < 1 {
|
||||
return res, invalid("SOS without components")
|
||||
}
|
||||
if n != 4+2*ns {
|
||||
return res, invalid("SOS length %d, want %d", n, 4+2*ns)
|
||||
}
|
||||
if _, err := br.Discard(n - 1); err != nil {
|
||||
return res, invalid("truncated SOS")
|
||||
}
|
||||
sawSOS = true
|
||||
if marker, err = skipEntropy(br); err != nil {
|
||||
return res, err
|
||||
}
|
||||
continue
|
||||
default:
|
||||
n, err := segmentLength(br)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if _, err := br.Discard(n); err != nil {
|
||||
return res, invalid("truncated JPEG segment")
|
||||
}
|
||||
}
|
||||
if marker, err = readMarker(br); err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package imagecheck
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const limit = 40_000_000
|
||||
|
||||
func testImage(w, h int) *image.RGBA {
|
||||
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
img.Set(x, y, color.RGBA{uint8(x * 7), uint8(y * 3), uint8(x ^ y), 255})
|
||||
}
|
||||
}
|
||||
return img
|
||||
}
|
||||
|
||||
func encodePNG(t *testing.T, w, h int) []byte {
|
||||
var b bytes.Buffer
|
||||
if err := png.Encode(&b, testImage(w, h)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func encodeJPEG(t *testing.T, w, h int, progressive bool) []byte {
|
||||
var b bytes.Buffer
|
||||
if err := jpeg.Encode(&b, testImage(w, h), &jpeg.Options{Quality: 80}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func chunk(typ string, data []byte) []byte {
|
||||
var b bytes.Buffer
|
||||
binary.Write(&b, binary.BigEndian, uint32(len(data)))
|
||||
b.WriteString(typ)
|
||||
b.Write(data)
|
||||
crc := crc32.NewIEEE()
|
||||
crc.Write([]byte(typ))
|
||||
crc.Write(data)
|
||||
binary.Write(&b, binary.BigEndian, crc.Sum32())
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
// craftPNG собирает PNG вручную: так можно задать любые IHDR и поток данных.
|
||||
func craftPNG(w, h uint32, colorType, depth, interlace byte, raw []byte) []byte {
|
||||
var b bytes.Buffer
|
||||
b.Write(pngSignature)
|
||||
ihdr := make([]byte, 13)
|
||||
binary.BigEndian.PutUint32(ihdr[0:], w)
|
||||
binary.BigEndian.PutUint32(ihdr[4:], h)
|
||||
ihdr[8], ihdr[9], ihdr[12] = depth, colorType, interlace
|
||||
b.Write(chunk("IHDR", ihdr))
|
||||
var z bytes.Buffer
|
||||
zw := zlib.NewWriter(&z)
|
||||
zw.Write(raw)
|
||||
zw.Close()
|
||||
b.Write(chunk("IDAT", z.Bytes()))
|
||||
b.Write(chunk("IEND", nil))
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func check(t *testing.T, name string, data []byte, format string, want error) {
|
||||
t.Helper()
|
||||
_, err := Validate(bytes.NewReader(data), format, limit)
|
||||
if want == nil && err != nil {
|
||||
t.Errorf("%s: unexpected error %v", name, err)
|
||||
}
|
||||
if want != nil && !errors.Is(err, want) {
|
||||
t.Errorf("%s: got %v, want %v", name, err, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidPNGAndJPEG(t *testing.T) {
|
||||
p := encodePNG(t, 320, 200)
|
||||
res, err := Validate(bytes.NewReader(p), PNG, limit)
|
||||
if err != nil || res.Width != 320 || res.Height != 200 {
|
||||
t.Fatalf("png: %+v %v", res, err)
|
||||
}
|
||||
j := encodeJPEG(t, 321, 199, false)
|
||||
res, err = Validate(bytes.NewReader(j), JPEG, limit)
|
||||
if err != nil || res.Width != 321 || res.Height != 199 {
|
||||
t.Fatalf("jpeg: %+v %v", res, err)
|
||||
}
|
||||
check(t, "png with trailing bytes", append(p, []byte("trailer")...), PNG, nil)
|
||||
}
|
||||
|
||||
func TestCourseFixture(t *testing.T) {
|
||||
data, err := os.ReadFile("../../tests/fixtures/sample.png")
|
||||
if err != nil {
|
||||
t.Skip("fixture not found")
|
||||
}
|
||||
res, err := Validate(bytes.NewReader(data), PNG, limit)
|
||||
if err != nil || res.Width != 320 || res.Height != 200 {
|
||||
t.Fatalf("fixture: %+v %v", res, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInterlacedPNG(t *testing.T) {
|
||||
// 10×7 RGB, Adam7: длина сырых данных по семи проходам.
|
||||
w, h := int64(10), int64(7)
|
||||
raw := make([]byte, expectedRaw(w, h, 24, true))
|
||||
check(t, "interlaced", craftPNG(uint32(w), uint32(h), 2, 8, 1, raw), PNG, nil)
|
||||
check(t, "interlaced short data", craftPNG(uint32(w), uint32(h), 2, 8, 1, raw[:len(raw)-1]), PNG, ErrInvalid)
|
||||
}
|
||||
|
||||
func TestBrokenImages(t *testing.T) {
|
||||
p := encodePNG(t, 64, 64)
|
||||
j := encodeJPEG(t, 64, 64, false)
|
||||
|
||||
check(t, "png truncated", p[:len(p)/2], PNG, ErrInvalid)
|
||||
check(t, "png without IEND", p[:len(p)-12], PNG, ErrInvalid)
|
||||
bad := bytes.Clone(p)
|
||||
bad[len(bad)/2] ^= 0xFF
|
||||
check(t, "png corrupted byte", bad, PNG, ErrInvalid)
|
||||
check(t, "jpeg truncated", j[:len(j)/2], JPEG, ErrInvalid)
|
||||
check(t, "jpeg without EOI", j[:len(j)-2], JPEG, ErrInvalid)
|
||||
check(t, "png declared as jpeg", p, JPEG, ErrInvalid)
|
||||
check(t, "text as png", []byte("hello, not an image at all"), PNG, ErrInvalid)
|
||||
check(t, "empty", nil, PNG, ErrInvalid)
|
||||
check(t, "png short raw data", craftPNG(8, 8, 2, 8, 0, make([]byte, 8*(1+24)-1)), PNG, ErrInvalid)
|
||||
check(t, "png bad bit depth", craftPNG(8, 8, 2, 4, 0, make([]byte, 8*(1+12))), PNG, ErrInvalid)
|
||||
}
|
||||
|
||||
func TestPixelLimitBeforeData(t *testing.T) {
|
||||
// «Бомба»: IHDR на 100000×100000, данных почти нет. Должно быть ErrTooLarge,
|
||||
// а не попытка распаковать 30 ГБ.
|
||||
bomb := craftPNG(100000, 100000, 2, 8, 0, []byte{0})
|
||||
check(t, "png bomb", bomb, PNG, ErrTooLarge)
|
||||
// Ровно на границе лимита — допустимо по пикселям (данных не хватает → invalid).
|
||||
check(t, "png at limit", craftPNG(8000, 5000, 0, 1, 0, nil), PNG, ErrInvalid)
|
||||
check(t, "png over limit", craftPNG(8000, 5001, 0, 1, 0, nil), PNG, ErrTooLarge)
|
||||
|
||||
// JPEG: подменяем размеры в SOF0 на 60000×60000.
|
||||
j := encodeJPEG(t, 16, 16, false)
|
||||
i := bytes.Index(j, []byte{0xFF, 0xC0})
|
||||
if i < 0 {
|
||||
t.Fatal("no SOF0")
|
||||
}
|
||||
big := bytes.Clone(j)
|
||||
binary.BigEndian.PutUint16(big[i+5:], 60000)
|
||||
binary.BigEndian.PutUint16(big[i+7:], 60000)
|
||||
check(t, "jpeg bomb", big, JPEG, ErrTooLarge)
|
||||
}
|
||||
|
||||
func TestJPEGSOFStructure(t *testing.T) {
|
||||
// Серый JPEG: одна компонента (Nf=1), длина SOF = 6 + 3*1.
|
||||
var g bytes.Buffer
|
||||
gray := image.NewGray(image.Rect(0, 0, 33, 17))
|
||||
if err := jpeg.Encode(&g, gray, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res, err := Validate(bytes.NewReader(g.Bytes()), JPEG, limit)
|
||||
if err != nil || res.Width != 33 || res.Height != 17 {
|
||||
t.Fatalf("grayscale jpeg: %+v %v", res, err)
|
||||
}
|
||||
// Цветной JPEG с испорченным Nf (длина сегмента больше не сходится).
|
||||
j := encodeJPEG(t, 16, 16, false)
|
||||
i := bytes.Index(j, []byte{0xFF, 0xC0})
|
||||
bad := bytes.Clone(j)
|
||||
bad[i+9] = 7 // Nf: было 3
|
||||
check(t, "jpeg SOF length mismatch", bad, JPEG, ErrInvalid)
|
||||
}
|
||||
|
||||
func TestSniff(t *testing.T) {
|
||||
if f, ok := Sniff(encodePNG(t, 2, 2)); !ok || f != PNG {
|
||||
t.Error("png sniff")
|
||||
}
|
||||
if f, ok := Sniff(encodeJPEG(t, 2, 2, false)); !ok || f != JPEG {
|
||||
t.Error("jpeg sniff")
|
||||
}
|
||||
if _, ok := Sniff([]byte("GIF89a")); ok {
|
||||
t.Error("gif must not be accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
// Package imaging строит варианты вложения по контракту lab5.
|
||||
//
|
||||
// thumbnail JPEG, вписан в 256×256 с сохранением пропорций, без увеличения,
|
||||
// EXIF orientation применён, alpha компонуется на белый,
|
||||
// размеры округляются вниз, минимум 1 px (320×200 → 256×160)
|
||||
// crop JPEG, центрированный квадрат 128×128 (маленькие увеличиваются)
|
||||
// optimized WebP, вписан в 1280×1280 без увеличения; lossy, качество WEBP_QUALITY,
|
||||
// метаданные удалены (cwebp -metadata none; пиксели и так
|
||||
// перекодированы из декодированного изображения, EXIF не переносится)
|
||||
//
|
||||
// Все кодировщики детерминированы: одинаковый вход даёт одинаковые байты, поэтому
|
||||
// повтор job перезаписывает объект тем же содержимым.
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
xdraw "golang.org/x/image/draw"
|
||||
)
|
||||
|
||||
// ErrDecode — изображение не декодируется: постоянная ошибка (повтор не поможет).
|
||||
var ErrDecode = errors.New("cannot decode image")
|
||||
|
||||
// Params — параметры версии pipeline. Новая версия может менять их, не трогая
|
||||
// уже опубликованные результаты старой (ключ эффекта включает версию).
|
||||
type Params struct {
|
||||
ThumbQuality int
|
||||
CropQuality int
|
||||
WebPQuality int
|
||||
}
|
||||
|
||||
// ParamsFor — параметры по номеру версии pipeline.
|
||||
func ParamsFor(version, webpQuality int) Params {
|
||||
p := Params{ThumbQuality: 85, CropQuality: 85, WebPQuality: webpQuality}
|
||||
if version >= 2 {
|
||||
// v2 (трек 5): чуть выше качество превью — другой, но детерминированный результат.
|
||||
p.ThumbQuality, p.CropQuality = 92, 92
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// Output — закодированный вариант.
|
||||
type Output struct {
|
||||
Name string
|
||||
ContentType string
|
||||
Data []byte
|
||||
Width int
|
||||
Height int
|
||||
SHA256 string
|
||||
}
|
||||
|
||||
func newOutput(name, ctype string, data []byte, w, h int) Output {
|
||||
sum := sha256.Sum256(data)
|
||||
return Output{Name: name, ContentType: ctype, Data: data, Width: w, Height: h, SHA256: hex.EncodeToString(sum[:])}
|
||||
}
|
||||
|
||||
// Decode декодирует оригинал, применяет EXIF orientation (JPEG) и компонует
|
||||
// прозрачность на белый фон.
|
||||
func Decode(data []byte) (*image.RGBA, error) {
|
||||
src, _, err := image.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrDecode, err)
|
||||
}
|
||||
b := src.Bounds()
|
||||
out := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
|
||||
draw.Draw(out, out.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
|
||||
draw.Draw(out, out.Bounds(), src, b.Min, draw.Over) // alpha → на белый
|
||||
return Orient(out, ExifOrientation(data)), nil
|
||||
}
|
||||
|
||||
// FitSize — вписать w×h в box×box с сохранением пропорций; без увеличения,
|
||||
// если allowUpscale=false; округление вниз, минимум 1.
|
||||
func FitSize(w, h, box int, allowUpscale bool) (int, int) {
|
||||
if !allowUpscale && w <= box && h <= box {
|
||||
return w, h
|
||||
}
|
||||
if w >= h {
|
||||
nh := h * box / w
|
||||
return box, max(1, nh)
|
||||
}
|
||||
nw := w * box / h
|
||||
return max(1, nw), box
|
||||
}
|
||||
|
||||
func resize(src image.Image, w, h int) *image.RGBA {
|
||||
dst := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||
xdraw.CatmullRom.Scale(dst, dst.Bounds(), src, src.Bounds(), xdraw.Src, nil)
|
||||
return dst
|
||||
}
|
||||
|
||||
func encodeJPEG(img image.Image, quality int) ([]byte, error) {
|
||||
var b bytes.Buffer
|
||||
if err := jpeg.Encode(&b, img, &jpeg.Options{Quality: quality}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
// Thumbnail — JPEG, вписан в 256×256 без увеличения.
|
||||
func Thumbnail(img *image.RGBA, p Params) (Output, error) {
|
||||
w, h := FitSize(img.Bounds().Dx(), img.Bounds().Dy(), 256, false)
|
||||
var src image.Image = img
|
||||
if w != img.Bounds().Dx() || h != img.Bounds().Dy() {
|
||||
src = resize(img, w, h)
|
||||
}
|
||||
data, err := encodeJPEG(src, p.ThumbQuality)
|
||||
if err != nil {
|
||||
return Output{}, err
|
||||
}
|
||||
return newOutput("thumbnail", "image/jpeg", data, w, h), nil
|
||||
}
|
||||
|
||||
// Crop — JPEG, центрированный квадрат 128×128 (маленькие исходники увеличиваются).
|
||||
func Crop(img *image.RGBA, p Params) (Output, error) {
|
||||
b := img.Bounds()
|
||||
side := min(b.Dx(), b.Dy())
|
||||
x0 := b.Min.X + (b.Dx()-side)/2
|
||||
y0 := b.Min.Y + (b.Dy()-side)/2
|
||||
sq := img.SubImage(image.Rect(x0, y0, x0+side, y0+side))
|
||||
data, err := encodeJPEG(resize(sq, 128, 128), p.CropQuality)
|
||||
if err != nil {
|
||||
return Output{}, err
|
||||
}
|
||||
return newOutput("crop", "image/jpeg", data, 128, 128), nil
|
||||
}
|
||||
|
||||
// WebPEncoder кодирует RGBA в WebP.
|
||||
type WebPEncoder func(ctx context.Context, img image.Image, quality int) ([]byte, error)
|
||||
|
||||
// CWebP — кодировщик через cwebp (libwebp-tools): lossy, -metadata none.
|
||||
// Вход — PNG во временном каталоге процесса (эфемерный, удаляется сразу).
|
||||
func CWebP(ctx context.Context, img image.Image, quality int) ([]byte, error) {
|
||||
dir, err := os.MkdirTemp("", "webp-")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
in, out := filepath.Join(dir, "in.png"), filepath.Join(dir, "out.webp")
|
||||
f, err := os.Create(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
enc := png.Encoder{CompressionLevel: png.BestSpeed}
|
||||
if err := enc.Encode(f, img); err != nil {
|
||||
f.Close()
|
||||
return nil, err
|
||||
}
|
||||
f.Close()
|
||||
cmd := exec.CommandContext(ctx, "cwebp", "-quiet", "-q", fmt.Sprint(quality), "-m", "4", "-metadata", "none", in, "-o", out)
|
||||
if msg, err := cmd.CombinedOutput(); err != nil {
|
||||
return nil, fmt.Errorf("cwebp: %v: %s", err, bytes.TrimSpace(msg))
|
||||
}
|
||||
return os.ReadFile(out)
|
||||
}
|
||||
|
||||
// Optimized — WebP, вписан в 1280×1280 без увеличения.
|
||||
func Optimized(ctx context.Context, img *image.RGBA, p Params, enc WebPEncoder) (Output, error) {
|
||||
w, h := FitSize(img.Bounds().Dx(), img.Bounds().Dy(), 1280, false)
|
||||
var src image.Image = img
|
||||
if w != img.Bounds().Dx() || h != img.Bounds().Dy() {
|
||||
src = resize(img, w, h)
|
||||
}
|
||||
data, err := enc(ctx, src, p.WebPQuality)
|
||||
if err != nil {
|
||||
return Output{}, err
|
||||
}
|
||||
if len(data) < 12 || string(data[0:4]) != "RIFF" || string(data[8:12]) != "WEBP" {
|
||||
return Output{}, errors.New("encoder produced non-WebP data")
|
||||
}
|
||||
return newOutput("optimized", "image/webp", data, w, h), nil
|
||||
}
|
||||
|
||||
// --- EXIF orientation ---
|
||||
|
||||
// ExifOrientation возвращает тег 0x0112 из APP1 Exif JPEG (1 — нет поворота).
|
||||
func ExifOrientation(data []byte) int {
|
||||
if len(data) < 4 || data[0] != 0xFF || data[1] != 0xD8 {
|
||||
return 1
|
||||
}
|
||||
i := 2
|
||||
for i+4 <= len(data) && data[i] == 0xFF {
|
||||
marker := data[i+1]
|
||||
if marker == 0xDA || marker == 0xD9 {
|
||||
break
|
||||
}
|
||||
segLen := int(binary.BigEndian.Uint16(data[i+2 : i+4]))
|
||||
if segLen < 2 || i+2+segLen > len(data) {
|
||||
break
|
||||
}
|
||||
seg := data[i+4 : i+2+segLen]
|
||||
if marker == 0xE1 && len(seg) > 14 && string(seg[:6]) == "Exif\x00\x00" {
|
||||
if o := tiffOrientation(seg[6:]); o >= 1 && o <= 8 {
|
||||
return o
|
||||
}
|
||||
}
|
||||
i += 2 + segLen
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func tiffOrientation(t []byte) int {
|
||||
if len(t) < 8 {
|
||||
return 1
|
||||
}
|
||||
var bo binary.ByteOrder
|
||||
switch string(t[:2]) {
|
||||
case "II":
|
||||
bo = binary.LittleEndian
|
||||
case "MM":
|
||||
bo = binary.BigEndian
|
||||
default:
|
||||
return 1
|
||||
}
|
||||
off := int(bo.Uint32(t[4:8]))
|
||||
if off+2 > len(t) {
|
||||
return 1
|
||||
}
|
||||
n := int(bo.Uint16(t[off : off+2]))
|
||||
for k := 0; k < n; k++ {
|
||||
e := off + 2 + 12*k
|
||||
if e+12 > len(t) {
|
||||
return 1
|
||||
}
|
||||
if bo.Uint16(t[e:e+2]) == 0x0112 {
|
||||
return int(bo.Uint16(t[e+8 : e+10]))
|
||||
}
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// Orient применяет EXIF orientation 1..8 (поворот/отражение).
|
||||
func Orient(src *image.RGBA, o int) *image.RGBA {
|
||||
if o <= 1 || o > 8 {
|
||||
return src
|
||||
}
|
||||
b := src.Bounds()
|
||||
w, h := b.Dx(), b.Dy()
|
||||
dw, dh := w, h
|
||||
if o >= 5 {
|
||||
dw, dh = h, w
|
||||
}
|
||||
dst := image.NewRGBA(image.Rect(0, 0, dw, dh))
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
var dx, dy int
|
||||
switch o {
|
||||
case 2:
|
||||
dx, dy = w-1-x, y
|
||||
case 3:
|
||||
dx, dy = w-1-x, h-1-y
|
||||
case 4:
|
||||
dx, dy = x, h-1-y
|
||||
case 5:
|
||||
dx, dy = y, x
|
||||
case 6:
|
||||
dx, dy = h-1-y, x
|
||||
case 7:
|
||||
dx, dy = h-1-y, w-1-x
|
||||
case 8:
|
||||
dx, dy = y, w-1-x
|
||||
}
|
||||
dst.SetRGBA(dx, dy, src.RGBAAt(b.Min.X+x, b.Min.Y+y))
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// SHA256Hex — lowercase hex SHA-256.
|
||||
func SHA256Hex(data []byte) string {
|
||||
sum := sha256.Sum256(data)
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func solid(w, h int, c color.Color) *image.NRGBA {
|
||||
m := image.NewNRGBA(image.Rect(0, 0, w, h))
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
m.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func encPNG(t *testing.T, img image.Image) []byte {
|
||||
var b bytes.Buffer
|
||||
if err := png.Encode(&b, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func TestFitSize(t *testing.T) {
|
||||
cases := []struct {
|
||||
w, h, box int
|
||||
up bool
|
||||
ww, wh int
|
||||
}{
|
||||
{320, 200, 256, false, 256, 160}, // fixture курса
|
||||
{200, 320, 256, false, 160, 256},
|
||||
{100, 50, 256, false, 100, 50}, // без увеличения
|
||||
{5000, 1, 256, false, 256, 1}, // минимум 1 px
|
||||
{1, 5000, 256, false, 1, 256},
|
||||
{257, 255, 256, false, 256, 254}, // округление вниз
|
||||
{40, 30, 128, true, 128, 96},
|
||||
{3000, 2000, 1280, false, 1280, 853},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if w, h := FitSize(c.w, c.h, c.box, c.up); w != c.ww || h != c.wh {
|
||||
t.Errorf("FitSize(%d,%d,%d,%v)=%d×%d want %d×%d", c.w, c.h, c.box, c.up, w, h, c.ww, c.wh)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func jpegSize(t *testing.T, data []byte) (int, int) {
|
||||
cfg, err := jpeg.DecodeConfig(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return cfg.Width, cfg.Height
|
||||
}
|
||||
|
||||
func TestCourseFixtureThumbnail(t *testing.T) {
|
||||
data, err := os.ReadFile("../../tests/fixtures/sample.png")
|
||||
if err != nil {
|
||||
t.Skip("fixture not found")
|
||||
}
|
||||
img, err := Decode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
th, err := Thumbnail(img, ParamsFor(1, 80))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w, h := jpegSize(t, th.Data); w != 256 || h != 160 || th.Width != 256 || th.Height != 160 {
|
||||
t.Fatalf("thumbnail %d×%d (meta %d×%d), want 256×160", w, h, th.Width, th.Height)
|
||||
}
|
||||
again, _ := Thumbnail(img, ParamsFor(1, 80))
|
||||
if !bytes.Equal(th.Data, again.Data) {
|
||||
t.Error("thumbnail encoding is not deterministic")
|
||||
}
|
||||
cr, err := Crop(img, ParamsFor(1, 80))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w, h := jpegSize(t, cr.Data); w != 128 || h != 128 {
|
||||
t.Fatalf("crop %d×%d", w, h)
|
||||
}
|
||||
v2, _ := Thumbnail(img, ParamsFor(2, 80))
|
||||
if bytes.Equal(v2.Data, th.Data) {
|
||||
t.Error("pipeline v2 must produce a different thumbnail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmallImageNotUpscaledButCropIs(t *testing.T) {
|
||||
img, err := Decode(encPNG(t, solid(40, 30, color.RGBA{10, 20, 30, 255})))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
th, _ := Thumbnail(img, ParamsFor(1, 80))
|
||||
if th.Width != 40 || th.Height != 30 {
|
||||
t.Errorf("small thumbnail upscaled to %d×%d", th.Width, th.Height)
|
||||
}
|
||||
cr, _ := Crop(img, ParamsFor(1, 80))
|
||||
if w, h := jpegSize(t, cr.Data); w != 128 || h != 128 {
|
||||
t.Errorf("crop of small image %d×%d", w, h)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlphaOnWhite(t *testing.T) {
|
||||
img, err := Decode(encPNG(t, solid(10, 10, color.NRGBA{0, 0, 0, 0})))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c := img.RGBAAt(5, 5); c.R != 255 || c.G != 255 || c.B != 255 {
|
||||
t.Errorf("transparent pixel composited to %v, want white", c)
|
||||
}
|
||||
half, _ := Decode(encPNG(t, solid(4, 4, color.NRGBA{0, 0, 0, 128})))
|
||||
if c := half.RGBAAt(1, 1); c.R < 120 || c.R > 135 {
|
||||
t.Errorf("50%% black over white = %v, want ~127", c)
|
||||
}
|
||||
}
|
||||
|
||||
// withExif вставляет APP1 Exif с orientation сразу после SOI.
|
||||
func withExif(jpg []byte, orientation uint16) []byte {
|
||||
var tiff bytes.Buffer
|
||||
tiff.WriteString("MM\x00\x2a")
|
||||
binary.Write(&tiff, binary.BigEndian, uint32(8))
|
||||
binary.Write(&tiff, binary.BigEndian, uint16(1))
|
||||
binary.Write(&tiff, binary.BigEndian, uint16(0x0112))
|
||||
binary.Write(&tiff, binary.BigEndian, uint16(3))
|
||||
binary.Write(&tiff, binary.BigEndian, uint32(1))
|
||||
binary.Write(&tiff, binary.BigEndian, orientation)
|
||||
tiff.Write([]byte{0, 0})
|
||||
binary.Write(&tiff, binary.BigEndian, uint32(0))
|
||||
seg := append([]byte("Exif\x00\x00"), tiff.Bytes()...)
|
||||
var out bytes.Buffer
|
||||
out.Write(jpg[:2])
|
||||
out.Write([]byte{0xFF, 0xE1})
|
||||
binary.Write(&out, binary.BigEndian, uint16(len(seg)+2))
|
||||
out.Write(seg)
|
||||
out.Write(jpg[2:])
|
||||
return out.Bytes()
|
||||
}
|
||||
|
||||
func TestExifOrientation(t *testing.T) {
|
||||
// 300×100: слева красный, справа синий.
|
||||
src := image.NewRGBA(image.Rect(0, 0, 300, 100))
|
||||
for y := 0; y < 100; y++ {
|
||||
for x := 0; x < 300; x++ {
|
||||
c := color.RGBA{255, 0, 0, 255}
|
||||
if x >= 150 {
|
||||
c = color.RGBA{0, 0, 255, 255}
|
||||
}
|
||||
src.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
var b bytes.Buffer
|
||||
jpeg.Encode(&b, src, &jpeg.Options{Quality: 95})
|
||||
data := withExif(b.Bytes(), 6) // 90° по часовой
|
||||
if o := ExifOrientation(data); o != 6 {
|
||||
t.Fatalf("orientation %d, want 6", o)
|
||||
}
|
||||
img, err := Decode(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if img.Bounds().Dx() != 100 || img.Bounds().Dy() != 300 {
|
||||
t.Fatalf("rotated size %v, want 100×300", img.Bounds())
|
||||
}
|
||||
// После поворота на 90° по часовой левая (красная) половина оказывается сверху.
|
||||
if c := img.RGBAAt(50, 20); c.R < 200 || c.B > 60 {
|
||||
t.Errorf("top after rotation = %v, want red", c)
|
||||
}
|
||||
th, _ := Thumbnail(img, ParamsFor(1, 80))
|
||||
if th.Width != 85 || th.Height != 256 {
|
||||
t.Errorf("thumbnail of rotated %d×%d, want 85×256", th.Width, th.Height)
|
||||
}
|
||||
if ExifOrientation(b.Bytes()) != 1 {
|
||||
t.Error("no exif must mean orientation 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeGarbage(t *testing.T) {
|
||||
if _, err := Decode([]byte("not an image")); err == nil {
|
||||
t.Fatal("garbage decoded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptimizedWebP(t *testing.T) {
|
||||
if _, err := exec.LookPath("cwebp"); err != nil {
|
||||
t.Skip("cwebp not installed (worker image has libwebp-tools)")
|
||||
}
|
||||
img, _ := Decode(encPNG(t, solid(2000, 1000, color.RGBA{200, 100, 50, 255})))
|
||||
out, err := Optimized(context.Background(), img, ParamsFor(1, 80), CWebP)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out.Width != 1280 || out.Height != 640 || out.ContentType != "image/webp" {
|
||||
t.Fatalf("optimized %+v", out)
|
||||
}
|
||||
again, _ := Optimized(context.Background(), img, ParamsFor(1, 80), CWebP)
|
||||
if !bytes.Equal(out.Data, again.Data) {
|
||||
t.Error("cwebp output not deterministic")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,404 @@
|
||||
// Package reconcile сверяет метаданные вложений (PostgreSQL) и объекты (S3).
|
||||
//
|
||||
// Обход — один merge-проход по двум потокам, отсортированным одинаково
|
||||
// (object_key COLLATE "C" и лексикографический листинг S3), поэтому память
|
||||
// O(1) от числа вложений, а стоимость — O(N) строк + ceil(N/1000) LIST-запросов.
|
||||
//
|
||||
// Расхождения и безопасные действия:
|
||||
//
|
||||
// stale_intent строка uploading старше grace (брошенная загрузка) → удалить объект, затем строку
|
||||
// orphan_object объект без строки, старше grace → удалить объект (после перепроверки строки)
|
||||
// missing_object строка ready без объекта → карантин: status=missing (после перепроверки Stat)
|
||||
// size_mismatch строка ready, размер объекта другой → карантин: status=missing
|
||||
// restored строка missing, объект снова есть и размер совпадает → status=ready
|
||||
// purge_missing строка missing старше grace, ни одно сообщение не ссылается → удалить строку
|
||||
// missing_variant у ready-варианта нет объекта → только отчёт (без автоисправления)
|
||||
//
|
||||
// С lab5 вложения в queued/processing/failed и их staging принадлежат pipeline
|
||||
// (lease, retry, retention): reconciler их не трогает. Объекты без строки
|
||||
// (например, частичные результаты попытки, закончившейся failed) удаляются как
|
||||
// orphan после grace — так же, как в lab4.
|
||||
//
|
||||
// Свежие объекты и намерения (моложе grace) и вложения, на которые ссылаются
|
||||
// сообщения, не удаляются никогда. Режим по умолчанию — dry-run (только отчёт).
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/storage"
|
||||
)
|
||||
|
||||
// Repo — нужные reconciler операции над метаданными.
|
||||
type Repo interface {
|
||||
StreamByKey(ctx context.Context, prefix string, fn func(*domain.Attachment) error) error
|
||||
HasKey(ctx context.Context, key string) (bool, error)
|
||||
DeleteStaleIntent(ctx context.Context, id string, cutoff time.Time) (bool, error)
|
||||
SetStatus(ctx context.Context, id, from, to string) (bool, error)
|
||||
DeleteUnreferencedMissing(ctx context.Context, id string, cutoff time.Time) (bool, error)
|
||||
IsReferenced(ctx context.Context, id string) (bool, error)
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
Prefix string
|
||||
Grace time.Duration
|
||||
Apply bool
|
||||
Now time.Time // общий «сейчас» для строк и объектов (время БД)
|
||||
// MaxItems ограничивает список примеров в отчёте на каждый вид расхождения.
|
||||
MaxItems int
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Kind string `json:"kind"`
|
||||
Key string `json:"key"`
|
||||
AttachmentID string `json:"attachment_id,omitempty"`
|
||||
Action string `json:"action"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
DryRun bool `json:"dry_run"`
|
||||
GraceSeconds float64 `json:"grace_seconds"`
|
||||
Cutoff time.Time `json:"cutoff"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
RowsScanned int `json:"rows_scanned"`
|
||||
ObjectsScanned int `json:"objects_scanned"`
|
||||
Consistent int `json:"consistent"`
|
||||
Found map[string]int `json:"found"`
|
||||
Fixed map[string]int `json:"fixed"`
|
||||
Skipped map[string]int `json:"skipped"`
|
||||
Calls map[string]int `json:"calls"`
|
||||
HeapAllocMB float64 `json:"heap_alloc_mb"`
|
||||
// WaitMS — сколько merge-цикл простоял в ожидании очередного элемента каждого
|
||||
// потока: показывает, кто (S3-листинг или запрос к БД) задаёт время обхода.
|
||||
WaitMS map[string]int64 `json:"wait_ms"`
|
||||
Errors []string `json:"errors"`
|
||||
Items []Item `json:"items"`
|
||||
}
|
||||
|
||||
type reconciler struct {
|
||||
repo Repo
|
||||
store storage.Store
|
||||
opt Options
|
||||
rep *Report
|
||||
items map[string]int
|
||||
}
|
||||
|
||||
func (r *reconciler) note(kind string, it Item) {
|
||||
r.rep.Found[kind]++
|
||||
if r.items[kind] < r.opt.MaxItems {
|
||||
r.items[kind]++
|
||||
r.rep.Items = append(r.rep.Items, it)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *reconciler) fail(format string, args ...any) {
|
||||
if len(r.rep.Errors) < 100 {
|
||||
r.rep.Errors = append(r.rep.Errors, fmt.Sprintf(format, args...))
|
||||
}
|
||||
}
|
||||
|
||||
// Run выполняет один проход. Ошибки отдельных действий попадают в отчёт и не
|
||||
// прерывают обход; ошибка листинга/запроса прерывает проход целиком.
|
||||
func Run(ctx context.Context, repo Repo, store storage.Store, opt Options) (*Report, error) {
|
||||
start := time.Now()
|
||||
if opt.MaxItems == 0 {
|
||||
opt.MaxItems = 50
|
||||
}
|
||||
cutoff := opt.Now.Add(-opt.Grace)
|
||||
rep := &Report{
|
||||
DryRun: !opt.Apply, GraceSeconds: opt.Grace.Seconds(), Cutoff: cutoff,
|
||||
Found: map[string]int{}, Fixed: map[string]int{}, Skipped: map[string]int{}, Calls: map[string]int{},
|
||||
Errors: []string{}, Items: []Item{}, WaitMS: map[string]int64{},
|
||||
}
|
||||
rc := &reconciler{repo: repo, store: store, opt: opt, rep: rep, items: map[string]int{}}
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
objs := make(chan storage.Object, 512)
|
||||
objErr := make(chan error, 1)
|
||||
go func() {
|
||||
defer close(objs)
|
||||
objErr <- store.List(ctx, opt.Prefix, func(o storage.Object) error {
|
||||
select {
|
||||
case objs <- o:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
})
|
||||
}()
|
||||
rows := make(chan *domain.Attachment, 512)
|
||||
rowErr := make(chan error, 1)
|
||||
go func() {
|
||||
defer close(rows)
|
||||
rowErr <- repo.StreamByKey(ctx, opt.Prefix, func(a *domain.Attachment) error {
|
||||
select {
|
||||
case rows <- a:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
})
|
||||
}()
|
||||
|
||||
var waitS3, waitDB time.Duration
|
||||
nextObj := func() (storage.Object, bool) {
|
||||
t := time.Now()
|
||||
o, ok := <-objs
|
||||
waitS3 += time.Since(t)
|
||||
return o, ok
|
||||
}
|
||||
nextRow := func() (*domain.Attachment, bool) {
|
||||
t := time.Now()
|
||||
a, ok := <-rows
|
||||
waitDB += time.Since(t)
|
||||
return a, ok
|
||||
}
|
||||
o, okO := nextObj()
|
||||
a, okA := nextRow()
|
||||
for okO || okA {
|
||||
switch {
|
||||
case okA && (!okO || a.ObjectKey < o.Key):
|
||||
rep.RowsScanned++
|
||||
rc.rowWithoutObject(ctx, a, cutoff)
|
||||
a, okA = nextRow()
|
||||
case okO && (!okA || o.Key < a.ObjectKey):
|
||||
rep.ObjectsScanned++
|
||||
rc.objectWithoutRow(ctx, o, cutoff)
|
||||
o, okO = nextObj()
|
||||
default:
|
||||
rep.RowsScanned++
|
||||
rep.ObjectsScanned++
|
||||
rc.both(ctx, a, o, cutoff)
|
||||
a, okA = nextRow()
|
||||
o, okO = nextObj()
|
||||
}
|
||||
}
|
||||
rep.WaitMS["s3_list"] = waitS3.Milliseconds()
|
||||
rep.WaitMS["db_stream"] = waitDB.Milliseconds()
|
||||
if err := <-objErr; err != nil {
|
||||
return rep, fmt.Errorf("list objects: %w", err)
|
||||
}
|
||||
if err := <-rowErr; err != nil {
|
||||
return rep, fmt.Errorf("stream rows: %w", err)
|
||||
}
|
||||
rep.Calls["s3_list_pages"] = (rep.ObjectsScanned + 999) / 1000
|
||||
var ms runtime.MemStats
|
||||
runtime.ReadMemStats(&ms)
|
||||
rep.HeapAllocMB = float64(ms.HeapAlloc) / (1 << 20)
|
||||
rep.DurationMS = time.Since(start).Milliseconds()
|
||||
return rep, nil
|
||||
}
|
||||
|
||||
func (r *reconciler) rowWithoutObject(ctx context.Context, a *domain.Attachment, cutoff time.Time) {
|
||||
switch a.Status {
|
||||
case "variant":
|
||||
if r.objectExists(ctx, a.ObjectKey) {
|
||||
r.rep.Skipped["recheck_object_present"]++
|
||||
return
|
||||
}
|
||||
r.note("missing_variant", Item{Kind: "missing_variant", Key: a.ObjectKey, AttachmentID: a.ID, Action: "report"})
|
||||
case "staging", domain.AttachmentQueued, domain.AttachmentProcessing, domain.AttachmentFailed:
|
||||
r.rep.Skipped["owned_by_pipeline"]++
|
||||
case domain.AttachmentUploading:
|
||||
if !a.UpdatedAt.Before(cutoff) {
|
||||
r.rep.Skipped["fresh_intent"]++ // загрузка может быть в процессе
|
||||
return
|
||||
}
|
||||
r.staleIntent(ctx, a, false)
|
||||
case domain.AttachmentReady:
|
||||
// Листинг мог начаться раньше, чем объект записан: перепроверяем точечно.
|
||||
if r.objectExists(ctx, a.ObjectKey) {
|
||||
r.rep.Skipped["recheck_object_present"]++
|
||||
return
|
||||
}
|
||||
r.quarantine(ctx, a, "missing_object", "")
|
||||
case domain.AttachmentMissing:
|
||||
r.purgeMissing(ctx, a, cutoff)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *reconciler) objectWithoutRow(ctx context.Context, o storage.Object, cutoff time.Time) {
|
||||
if !o.LastModified.Before(cutoff) {
|
||||
r.rep.Skipped["fresh_object"]++
|
||||
return
|
||||
}
|
||||
// Строка могла появиться после начала обхода — перепроверяем перед удалением.
|
||||
r.rep.Calls["db_haskey"]++
|
||||
has, err := r.repo.HasKey(ctx, o.Key)
|
||||
if err != nil {
|
||||
r.fail("haskey %s: %v", o.Key, err)
|
||||
return
|
||||
}
|
||||
if has {
|
||||
r.rep.Skipped["recheck_row_present"]++
|
||||
return
|
||||
}
|
||||
it := Item{Kind: "orphan_object", Key: o.Key, Action: "delete_object",
|
||||
Detail: fmt.Sprintf("size=%d modified=%s", o.Size, o.LastModified.UTC().Format(time.RFC3339))}
|
||||
if !r.opt.Apply {
|
||||
it.Action = "would_delete_object"
|
||||
r.note("orphan_object", it)
|
||||
return
|
||||
}
|
||||
r.rep.Calls["s3_delete"]++
|
||||
if err := r.store.Delete(ctx, o.Key); err != nil {
|
||||
r.fail("delete orphan %s: %v", o.Key, err)
|
||||
return
|
||||
}
|
||||
r.note("orphan_object", it)
|
||||
r.rep.Fixed["orphan_object"]++
|
||||
}
|
||||
|
||||
func (r *reconciler) both(ctx context.Context, a *domain.Attachment, o storage.Object, cutoff time.Time) {
|
||||
switch a.Status {
|
||||
case "variant":
|
||||
if o.Size != a.SizeBytes {
|
||||
r.note("variant_size_mismatch", Item{Kind: "variant_size_mismatch", Key: a.ObjectKey, AttachmentID: a.ID,
|
||||
Action: "report", Detail: fmt.Sprintf("db=%d s3=%d", a.SizeBytes, o.Size)})
|
||||
return
|
||||
}
|
||||
r.rep.Consistent++
|
||||
case "staging", domain.AttachmentQueued, domain.AttachmentProcessing, domain.AttachmentFailed:
|
||||
r.rep.Consistent++
|
||||
case domain.AttachmentUploading:
|
||||
if !a.UpdatedAt.Before(cutoff) {
|
||||
r.rep.Skipped["fresh_intent"]++
|
||||
return
|
||||
}
|
||||
r.staleIntent(ctx, a, true)
|
||||
case domain.AttachmentReady:
|
||||
if o.Size != a.SizeBytes {
|
||||
r.quarantine(ctx, a, "size_mismatch", fmt.Sprintf("db=%d s3=%d", a.SizeBytes, o.Size))
|
||||
return
|
||||
}
|
||||
r.rep.Consistent++
|
||||
case domain.AttachmentMissing:
|
||||
if o.Size != a.SizeBytes {
|
||||
r.purgeMissing(ctx, a, cutoff)
|
||||
return
|
||||
}
|
||||
it := Item{Kind: "restored", Key: a.ObjectKey, AttachmentID: a.ID, Action: "set_ready"}
|
||||
if !r.opt.Apply {
|
||||
it.Action = "would_set_ready"
|
||||
r.note("restored", it)
|
||||
return
|
||||
}
|
||||
r.rep.Calls["db_update"]++
|
||||
ok, err := r.repo.SetStatus(ctx, a.ID, domain.AttachmentMissing, domain.AttachmentReady)
|
||||
if err != nil {
|
||||
r.fail("restore %s: %v", a.ID, err)
|
||||
return
|
||||
}
|
||||
r.note("restored", it)
|
||||
if ok {
|
||||
r.rep.Fixed["restored"]++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *reconciler) objectExists(ctx context.Context, key string) bool {
|
||||
r.rep.Calls["s3_stat"]++
|
||||
_, err := r.store.Stat(ctx, key)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
if !errors.Is(err, storage.ErrNotFound) {
|
||||
r.fail("stat %s: %v", key, err)
|
||||
return true // при ошибке хранилища ничего не трогаем
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *reconciler) staleIntent(ctx context.Context, a *domain.Attachment, hasObject bool) {
|
||||
it := Item{Kind: "stale_intent", Key: a.ObjectKey, AttachmentID: a.ID, Action: "delete_intent",
|
||||
Detail: "updated_at=" + a.UpdatedAt.UTC().Format(time.RFC3339)}
|
||||
if hasObject {
|
||||
it.Action = "delete_object_and_intent"
|
||||
}
|
||||
if !r.opt.Apply {
|
||||
it.Action = "would_" + it.Action
|
||||
r.note("stale_intent", it)
|
||||
return
|
||||
}
|
||||
// Сначала объект: если упадём между шагами, останется намерение без объекта,
|
||||
// и следующий проход его доберёт (обратный порядок оставил бы сироту без учёта).
|
||||
if hasObject {
|
||||
r.rep.Calls["s3_delete"]++
|
||||
if err := r.store.Delete(ctx, a.ObjectKey); err != nil {
|
||||
r.fail("delete intent object %s: %v", a.ObjectKey, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
r.rep.Calls["db_delete"]++
|
||||
ok, err := r.repo.DeleteStaleIntent(ctx, a.ID, r.opt.Now.Add(-r.opt.Grace))
|
||||
if err != nil {
|
||||
r.fail("delete intent %s: %v", a.ID, err)
|
||||
return
|
||||
}
|
||||
r.note("stale_intent", it)
|
||||
if ok {
|
||||
r.rep.Fixed["stale_intent"]++
|
||||
}
|
||||
}
|
||||
|
||||
func (r *reconciler) quarantine(ctx context.Context, a *domain.Attachment, kind, detail string) {
|
||||
it := Item{Kind: kind, Key: a.ObjectKey, AttachmentID: a.ID, Action: "quarantine", Detail: detail}
|
||||
if !r.opt.Apply {
|
||||
it.Action = "would_quarantine"
|
||||
r.note(kind, it)
|
||||
return
|
||||
}
|
||||
r.rep.Calls["db_update"]++
|
||||
ok, err := r.repo.SetStatus(ctx, a.ID, domain.AttachmentReady, domain.AttachmentMissing)
|
||||
if err != nil {
|
||||
r.fail("quarantine %s: %v", a.ID, err)
|
||||
return
|
||||
}
|
||||
r.note(kind, it)
|
||||
if ok {
|
||||
r.rep.Fixed[kind]++
|
||||
}
|
||||
}
|
||||
|
||||
// purgeMissing удаляет запись карантина, только если она старше grace и на неё
|
||||
// не ссылается ни одно сообщение. Используемые вложения остаются (их история цела).
|
||||
func (r *reconciler) purgeMissing(ctx context.Context, a *domain.Attachment, cutoff time.Time) {
|
||||
if !a.UpdatedAt.Before(cutoff) {
|
||||
r.rep.Skipped["fresh_missing"]++
|
||||
return
|
||||
}
|
||||
r.rep.Calls["db_referenced"]++
|
||||
ref, err := r.repo.IsReferenced(ctx, a.ID)
|
||||
if err != nil {
|
||||
r.fail("referenced %s: %v", a.ID, err)
|
||||
return
|
||||
}
|
||||
if ref {
|
||||
r.rep.Skipped["referenced_missing_kept"]++
|
||||
return
|
||||
}
|
||||
it := Item{Kind: "purge_missing", Key: a.ObjectKey, AttachmentID: a.ID, Action: "delete_row"}
|
||||
if !r.opt.Apply {
|
||||
it.Action = "would_delete_row"
|
||||
r.note("purge_missing", it)
|
||||
return
|
||||
}
|
||||
r.rep.Calls["db_delete"]++
|
||||
ok, err := r.repo.DeleteUnreferencedMissing(ctx, a.ID, cutoff)
|
||||
if err != nil {
|
||||
r.fail("purge %s: %v", a.ID, err)
|
||||
return
|
||||
}
|
||||
r.note("purge_missing", it)
|
||||
if ok {
|
||||
r.rep.Fixed["purge_missing"]++
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/storage"
|
||||
)
|
||||
|
||||
type fakeStore struct{ objs map[string]storage.Object }
|
||||
|
||||
func (f *fakeStore) Put(_ context.Context, key string, r io.Reader, size int64, _ string) error {
|
||||
f.objs[key] = storage.Object{Key: key, Size: size, LastModified: time.Now()}
|
||||
return nil
|
||||
}
|
||||
func (f *fakeStore) Get(context.Context, string) (io.ReadCloser, int64, error) {
|
||||
return io.NopCloser(bytes.NewReader(nil)), 0, nil
|
||||
}
|
||||
func (f *fakeStore) Stat(_ context.Context, key string) (storage.Object, error) {
|
||||
o, ok := f.objs[key]
|
||||
if !ok {
|
||||
return o, storage.ErrNotFound
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
func (f *fakeStore) Delete(_ context.Context, key string) error { delete(f.objs, key); return nil }
|
||||
func (f *fakeStore) Copy(_ context.Context, src, dst string) error {
|
||||
o, ok := f.objs[src]
|
||||
if !ok {
|
||||
return storage.ErrNotFound
|
||||
}
|
||||
o.Key = dst
|
||||
f.objs[dst] = o
|
||||
return nil
|
||||
}
|
||||
func (f *fakeStore) List(_ context.Context, prefix string, fn func(storage.Object) error) error {
|
||||
keys := make([]string, 0, len(f.objs))
|
||||
for k := range f.objs {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
if err := fn(f.objs[k]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeRepo struct {
|
||||
rows map[string]*domain.Attachment // by id
|
||||
refs map[string]bool
|
||||
}
|
||||
|
||||
func (f *fakeRepo) StreamByKey(_ context.Context, _ string, fn func(*domain.Attachment) error) error {
|
||||
list := make([]*domain.Attachment, 0, len(f.rows))
|
||||
for _, a := range f.rows {
|
||||
c := *a
|
||||
list = append(list, &c)
|
||||
}
|
||||
sort.Slice(list, func(i, j int) bool { return list[i].ObjectKey < list[j].ObjectKey })
|
||||
for _, a := range list {
|
||||
if err := fn(a); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (f *fakeRepo) HasKey(_ context.Context, key string) (bool, error) {
|
||||
for _, a := range f.rows {
|
||||
if a.ObjectKey == key {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
func (f *fakeRepo) DeleteStaleIntent(_ context.Context, id string, cutoff time.Time) (bool, error) {
|
||||
a, ok := f.rows[id]
|
||||
if !ok || a.Status != domain.AttachmentUploading || !a.UpdatedAt.Before(cutoff) {
|
||||
return false, nil
|
||||
}
|
||||
delete(f.rows, id)
|
||||
return true, nil
|
||||
}
|
||||
func (f *fakeRepo) SetStatus(_ context.Context, id, from, to string) (bool, error) {
|
||||
a, ok := f.rows[id]
|
||||
if !ok || a.Status != from {
|
||||
return false, nil
|
||||
}
|
||||
a.Status = to
|
||||
a.UpdatedAt = now
|
||||
return true, nil
|
||||
}
|
||||
func (f *fakeRepo) DeleteUnreferencedMissing(_ context.Context, id string, cutoff time.Time) (bool, error) {
|
||||
a, ok := f.rows[id]
|
||||
if !ok || a.Status != domain.AttachmentMissing || !a.UpdatedAt.Before(cutoff) || f.refs[id] {
|
||||
return false, nil
|
||||
}
|
||||
delete(f.rows, id)
|
||||
return true, nil
|
||||
}
|
||||
func (f *fakeRepo) IsReferenced(_ context.Context, id string) (bool, error) { return f.refs[id], nil }
|
||||
|
||||
var now = time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC)
|
||||
|
||||
const grace = time.Hour
|
||||
|
||||
func old() time.Time { return now.Add(-2 * grace) }
|
||||
func fresh() time.Time { return now.Add(-time.Minute) }
|
||||
|
||||
func setup() (*fakeRepo, *fakeStore) {
|
||||
r := &fakeRepo{rows: map[string]*domain.Attachment{}, refs: map[string]bool{}}
|
||||
s := &fakeStore{objs: map[string]storage.Object{}}
|
||||
row := func(id, status string, size int64, at time.Time) {
|
||||
r.rows[id] = &domain.Attachment{ID: id, ObjectKey: "a/c/" + id, Status: status, SizeBytes: size, UpdatedAt: at}
|
||||
}
|
||||
obj := func(id string, size int64, at time.Time) {
|
||||
s.objs["a/c/"+id] = storage.Object{Key: "a/c/" + id, Size: size, LastModified: at}
|
||||
}
|
||||
// согласованное
|
||||
row("ok", domain.AttachmentReady, 10, old())
|
||||
obj("ok", 10, old())
|
||||
// брошенная загрузка с объектом и без
|
||||
row("stale1", domain.AttachmentUploading, 0, old())
|
||||
obj("stale1", 7, old())
|
||||
row("stale2", domain.AttachmentUploading, 0, old())
|
||||
// загрузка в процессе — не трогать
|
||||
row("inflight", domain.AttachmentUploading, 0, fresh())
|
||||
obj("inflight", 5, fresh())
|
||||
// сирота старая и свежая
|
||||
obj("orphan", 3, old())
|
||||
obj("orphan-fresh", 3, fresh())
|
||||
// ready без объекта: используемое и нет
|
||||
row("lost-used", domain.AttachmentReady, 9, old())
|
||||
r.refs["lost-used"] = true
|
||||
row("lost-free", domain.AttachmentReady, 9, old())
|
||||
// подменённый размер
|
||||
row("resized", domain.AttachmentReady, 10, old())
|
||||
obj("resized", 11, old())
|
||||
return r, s
|
||||
}
|
||||
|
||||
func run(t *testing.T, r *fakeRepo, s *fakeStore, apply bool, at time.Time) *Report {
|
||||
t.Helper()
|
||||
rep, err := Run(context.Background(), r, s, Options{Prefix: "a/", Grace: grace, Apply: apply, Now: at})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rep.Errors) > 0 {
|
||||
t.Fatalf("errors: %v", rep.Errors)
|
||||
}
|
||||
return rep
|
||||
}
|
||||
|
||||
func TestDryRunChangesNothing(t *testing.T) {
|
||||
r, s := setup()
|
||||
nRows, nObjs := len(r.rows), len(s.objs)
|
||||
rep := run(t, r, s, false, now)
|
||||
if len(r.rows) != nRows || len(s.objs) != nObjs || r.rows["lost-free"].Status != domain.AttachmentReady {
|
||||
t.Fatal("dry-run mutated state")
|
||||
}
|
||||
want := map[string]int{"stale_intent": 2, "orphan_object": 1, "missing_object": 2, "size_mismatch": 1}
|
||||
for k, v := range want {
|
||||
if rep.Found[k] != v {
|
||||
t.Errorf("found[%s]=%d want %d (%v)", k, rep.Found[k], v, rep.Found)
|
||||
}
|
||||
}
|
||||
if len(rep.Fixed) != 0 {
|
||||
t.Errorf("dry-run fixed %v", rep.Fixed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyFixesAndIsIdempotent(t *testing.T) {
|
||||
r, s := setup()
|
||||
run(t, r, s, true, now)
|
||||
|
||||
for _, id := range []string{"stale1", "stale2"} {
|
||||
if _, ok := r.rows[id]; ok {
|
||||
t.Errorf("stale intent %s not removed", id)
|
||||
}
|
||||
}
|
||||
if _, ok := s.objs["a/c/stale1"]; ok {
|
||||
t.Error("stale intent object not removed")
|
||||
}
|
||||
if _, ok := s.objs["a/c/orphan"]; ok {
|
||||
t.Error("old orphan not removed")
|
||||
}
|
||||
// Свежие — никогда.
|
||||
if _, ok := s.objs["a/c/orphan-fresh"]; !ok {
|
||||
t.Error("fresh orphan deleted")
|
||||
}
|
||||
if _, ok := r.rows["inflight"]; !ok {
|
||||
t.Error("in-flight intent deleted")
|
||||
}
|
||||
if _, ok := s.objs["a/c/inflight"]; !ok {
|
||||
t.Error("in-flight object deleted")
|
||||
}
|
||||
for _, id := range []string{"lost-used", "lost-free", "resized"} {
|
||||
if r.rows[id].Status != domain.AttachmentMissing {
|
||||
t.Errorf("%s not quarantined", id)
|
||||
}
|
||||
}
|
||||
if r.rows["ok"].Status != domain.AttachmentReady {
|
||||
t.Error("consistent row touched")
|
||||
}
|
||||
|
||||
// Повтор сразу: исправлять нечего, карантин ещё моложе grace.
|
||||
rep := run(t, r, s, true, now)
|
||||
if len(rep.Fixed) != 0 {
|
||||
t.Errorf("second pass fixed %v", rep.Fixed)
|
||||
}
|
||||
|
||||
// Через grace: неиспользуемая запись карантина удаляется, используемая остаётся.
|
||||
later := now.Add(2 * grace)
|
||||
run(t, r, s, true, later)
|
||||
if _, ok := r.rows["lost-free"]; ok {
|
||||
t.Error("unreferenced missing row not purged")
|
||||
}
|
||||
if _, ok := r.rows["lost-used"]; !ok {
|
||||
t.Error("referenced missing row deleted")
|
||||
}
|
||||
if _, ok := s.objs["a/c/orphan-fresh"]; ok {
|
||||
t.Error("orphan not removed after grace")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoredObjectLeavesQuarantine(t *testing.T) {
|
||||
r, s := setup()
|
||||
run(t, r, s, true, now)
|
||||
// Объект вернули (например, из бэкапа) с правильным размером.
|
||||
s.objs["a/c/lost-used"] = storage.Object{Key: "a/c/lost-used", Size: 9, LastModified: now}
|
||||
rep := run(t, r, s, true, now)
|
||||
if rep.Fixed["restored"] != 1 || r.rows["lost-used"].Status != domain.AttachmentReady {
|
||||
t.Errorf("restore failed: %v %s", rep.Fixed, r.rows["lost-used"].Status)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
)
|
||||
|
||||
type AttachmentRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewAttachmentRepository(pool *pgxpool.Pool) *AttachmentRepository {
|
||||
return &AttachmentRepository{pool: pool}
|
||||
}
|
||||
|
||||
const attachmentColumns = `id, chat_id, owner_id, filename, object_key, status,
|
||||
COALESCE(content_type, ''), COALESCE(size_bytes, 0), COALESCE(sha256, ''), created_at, updated_at,
|
||||
COALESCE(error_code, ''), pipeline_version, COALESCE(staging_key, '')`
|
||||
|
||||
func scanAttachment(row pgx.Row) (*domain.Attachment, error) {
|
||||
var a domain.Attachment
|
||||
err := row.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey, &a.Status,
|
||||
&a.ContentType, &a.SizeBytes, &a.SHA256, &a.CreatedAt, &a.UpdatedAt,
|
||||
&a.ErrorCode, &a.PipelineVersion, &a.StagingKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
// CreateIntent записывает намерение загрузки (status=uploading) ДО записи в S3.
|
||||
func (r *AttachmentRepository) CreateIntent(ctx context.Context, a *domain.Attachment) error {
|
||||
const q = `
|
||||
INSERT INTO attachments (id, chat_id, owner_id, filename, object_key, staging_key, status, created_at, updated_at)
|
||||
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $5, 'uploading', $6, $6)
|
||||
`
|
||||
if _, err := r.pool.Exec(ctx, q, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.CreatedAt); err != nil {
|
||||
return fmt.Errorf("insert attachment intent: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarkQueued — точка 202. В ОДНОЙ транзакции: метаданные проверенного оригинала
|
||||
// и status=queued, логическое задание (jobs) и сообщение для broker (outbox).
|
||||
// После COMMIT работа не теряется: даже если broker недоступен или relay упал,
|
||||
// outbox будет опубликован позже. Если строки uploading уже нет — ErrNotFound.
|
||||
func (r *AttachmentRepository) MarkQueued(ctx context.Context, a *domain.Attachment, pipelineVersion, maxAttempts int) (string, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'queued', content_type = $2, size_bytes = $3, sha256 = $4, updated_at = $5
|
||||
WHERE id = $1::uuid AND status = 'uploading'
|
||||
`
|
||||
tag, err := tx.Exec(ctx, q, a.ID, a.ContentType, a.SizeBytes, a.SHA256, a.UpdatedAt)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("mark attachment queued: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() != 1 {
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
jobID, err := EnqueueJobTx(ctx, tx, a.ID, pipelineVersion, maxAttempts, "upload")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return "", fmt.Errorf("commit tx: %w", err)
|
||||
}
|
||||
return jobID, nil
|
||||
}
|
||||
|
||||
// Variants — варианты указанной версии pipeline.
|
||||
func (r *AttachmentRepository) Variants(ctx context.Context, attachmentID string, version int) ([]domain.Variant, error) {
|
||||
const q = `
|
||||
SELECT name, content_type, size_bytes, width, height, sha256, object_key, pipeline_version
|
||||
FROM attachment_variants
|
||||
WHERE attachment_id = $1::uuid AND pipeline_version = $2
|
||||
ORDER BY CASE name WHEN 'thumbnail' THEN 1 WHEN 'crop' THEN 2 ELSE 3 END
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, attachmentID, version)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select variants: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []domain.Variant{}
|
||||
for rows.Next() {
|
||||
var v domain.Variant
|
||||
if err := rows.Scan(&v.Name, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256, &v.ObjectKey, &v.PipelineVersion); err != nil {
|
||||
return nil, fmt.Errorf("scan variant: %w", err)
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// DeleteIntent убирает намерение, если загрузка не удалась (компенсация).
|
||||
func (r *AttachmentRepository) DeleteIntent(ctx context.Context, id string) error {
|
||||
const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'uploading'`
|
||||
if _, err := r.pool.Exec(ctx, q, id); err != nil {
|
||||
return fmt.Errorf("delete attachment intent: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetForMember возвращает вложение, если пользователь — участник его чата.
|
||||
// Чужое и несуществующее вложение неразличимы (ErrNotFound).
|
||||
func (r *AttachmentRepository) GetForMember(ctx context.Context, id, userID string) (*domain.Attachment, error) {
|
||||
q := `SELECT ` + attachmentColumns + `
|
||||
FROM attachments a
|
||||
WHERE a.id = $1::uuid
|
||||
AND EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id = a.chat_id AND m.user_id = $2::uuid)`
|
||||
a, err := scanAttachment(r.pool.QueryRow(ctx, q, id, userID))
|
||||
if err != nil && !errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, fmt.Errorf("select attachment: %w", err)
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
|
||||
// --- для reconciler ---
|
||||
|
||||
// StreamByKey отдаёт все известные БД ключи объектов в байтовом порядке (как
|
||||
// ListObjectsV2): оригинал вложения (object_key), отдельный staging-объект и
|
||||
// объекты вариантов. Для staging и вариантов Status — «staging»/«variant», чтобы
|
||||
// reconciler не путал их с оригиналом.
|
||||
func (r *AttachmentRepository) StreamByKey(ctx context.Context, prefix string, fn func(*domain.Attachment) error) error {
|
||||
const q = `
|
||||
SELECT key, id::text, status, size_bytes, updated_at FROM (
|
||||
SELECT object_key AS key, id, status, COALESCE(size_bytes, 0) AS size_bytes, updated_at FROM attachments
|
||||
UNION ALL
|
||||
SELECT staging_key, id, 'staging', COALESCE(size_bytes, 0), updated_at FROM attachments
|
||||
WHERE staging_key IS NOT NULL AND staging_key <> object_key
|
||||
UNION ALL
|
||||
SELECT object_key, attachment_id, 'variant', size_bytes, created_at FROM attachment_variants
|
||||
) k
|
||||
WHERE starts_with(key, $1)
|
||||
ORDER BY key COLLATE "C"`
|
||||
rows, err := r.pool.Query(ctx, q, prefix)
|
||||
if err != nil {
|
||||
return fmt.Errorf("stream object keys: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var a domain.Attachment
|
||||
if err := rows.Scan(&a.ObjectKey, &a.ID, &a.Status, &a.SizeBytes, &a.UpdatedAt); err != nil {
|
||||
return fmt.Errorf("scan object key: %w", err)
|
||||
}
|
||||
if err := fn(&a); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// HasKey — есть ли строка с этим object_key (повторная проверка перед удалением объекта).
|
||||
func (r *AttachmentRepository) HasKey(ctx context.Context, key string) (bool, error) {
|
||||
var ok bool
|
||||
err := r.pool.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM attachments WHERE object_key = $1 OR staging_key = $1)
|
||||
OR EXISTS (SELECT 1 FROM attachment_variants WHERE object_key = $1)`, key).Scan(&ok)
|
||||
return ok, err
|
||||
}
|
||||
|
||||
// DeleteStaleIntent удаляет намерение, только если оно всё ещё uploading и старше cutoff.
|
||||
func (r *AttachmentRepository) DeleteStaleIntent(ctx context.Context, id string, cutoff time.Time) (bool, error) {
|
||||
const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'uploading' AND updated_at < $2`
|
||||
tag, err := r.pool.Exec(ctx, q, id, cutoff)
|
||||
return tag.RowsAffected() == 1, err
|
||||
}
|
||||
|
||||
// SetStatus меняет статус ready↔missing (карантин и восстановление).
|
||||
func (r *AttachmentRepository) SetStatus(ctx context.Context, id, from, to string) (bool, error) {
|
||||
const q = `UPDATE attachments SET status = $3, updated_at = now() WHERE id = $1::uuid AND status = $2`
|
||||
tag, err := r.pool.Exec(ctx, q, id, from, to)
|
||||
return tag.RowsAffected() == 1, err
|
||||
}
|
||||
|
||||
// DeleteUnreferencedMissing удаляет запись карантина старше cutoff, если на неё
|
||||
// не ссылается ни одно сообщение. Блокировка строки (FOR UPDATE) конфликтует с
|
||||
// FOR SHARE при создании сообщения, поэтому гонки «удалили, пока прикрепляли» нет.
|
||||
func (r *AttachmentRepository) DeleteUnreferencedMissing(ctx context.Context, id string, cutoff time.Time) (bool, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
var locked string
|
||||
err = tx.QueryRow(ctx, `SELECT id FROM attachments WHERE id = $1::uuid AND status = 'missing' AND updated_at < $2 FOR UPDATE`, id, cutoff).Scan(&locked)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
var referenced bool
|
||||
if err := tx.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM message_attachments WHERE attachment_id = $1::uuid)`, id).Scan(&referenced); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if referenced {
|
||||
return false, nil
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `DELETE FROM attachments WHERE id = $1::uuid`, id); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// IsReferenced — используется ли вложение каким-либо сообщением.
|
||||
func (r *AttachmentRepository) IsReferenced(ctx context.Context, id string) (bool, error) {
|
||||
var ok bool
|
||||
err := r.pool.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM message_attachments WHERE attachment_id = $1::uuid)`, id).Scan(&ok)
|
||||
return ok, err
|
||||
}
|
||||
|
||||
// TryLock — advisory lock, чтобы одновременно работал один reconciler.
|
||||
func (r *AttachmentRepository) TryLock(ctx context.Context) (func(), bool, error) {
|
||||
conn, err := r.pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
var ok bool
|
||||
if err := conn.QueryRow(ctx, `SELECT pg_try_advisory_lock(4242004)`).Scan(&ok); err != nil || !ok {
|
||||
conn.Release()
|
||||
return nil, false, err
|
||||
}
|
||||
return func() {
|
||||
_, _ = conn.Exec(context.Background(), `SELECT pg_advisory_unlock(4242004)`)
|
||||
conn.Release()
|
||||
}, true, nil
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
)
|
||||
|
||||
type ChatRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewChatRepository(pool *pgxpool.Pool) *ChatRepository {
|
||||
return &ChatRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *ChatRepository) Create(ctx context.Context, chat *domain.Chat) error {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
const insertChat = `INSERT INTO chats (id, title, created_at) VALUES ($1::uuid, $2, $3)`
|
||||
if _, err := tx.Exec(ctx, insertChat, chat.ID, chat.Title, chat.CreatedAt); err != nil {
|
||||
return fmt.Errorf("insert chat: %w", err)
|
||||
}
|
||||
|
||||
const insertMember = `INSERT INTO chat_members (chat_id, user_id) VALUES ($1::uuid, $2::uuid)`
|
||||
for _, uid := range chat.MemberIDs {
|
||||
if _, err := tx.Exec(ctx, insertMember, chat.ID, uid); err != nil {
|
||||
return fmt.Errorf("insert member %s: %w", uid, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("commit tx: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ChatRepository) GetByID(ctx context.Context, id string) (*domain.Chat, error) {
|
||||
const q = `SELECT id, title, created_at FROM chats WHERE id = $1::uuid`
|
||||
var c domain.Chat
|
||||
err := r.pool.QueryRow(ctx, q, id).Scan(&c.ID, &c.Title, &c.CreatedAt)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("select chat: %w", err)
|
||||
}
|
||||
|
||||
const qMembers = `SELECT user_id FROM chat_members WHERE chat_id = $1::uuid ORDER BY user_id`
|
||||
rows, err := r.pool.Query(ctx, qMembers, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select members: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
c.MemberIDs = []string{}
|
||||
for rows.Next() {
|
||||
var uid string
|
||||
if err := rows.Scan(&uid); err != nil {
|
||||
return nil, fmt.Errorf("scan member: %w", err)
|
||||
}
|
||||
c.MemberIDs = append(c.MemberIDs, uid)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate members: %w", err)
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (r *ChatRepository) IsMember(ctx context.Context, chatID, userID string) (bool, error) {
|
||||
const q = `SELECT 1 FROM chat_members WHERE chat_id = $1::uuid AND user_id = $2::uuid`
|
||||
var one int
|
||||
err := r.pool.QueryRow(ctx, q, chatID, userID).Scan(&one)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return false, fmt.Errorf("check membership: %w", err)
|
||||
}
|
||||
const qChat = `SELECT 1 FROM chats WHERE id = $1::uuid`
|
||||
err = r.pool.QueryRow(ctx, qChat, chatID).Scan(&one)
|
||||
if err == nil {
|
||||
return false, nil
|
||||
}
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return false, repository.ErrNotFound
|
||||
}
|
||||
return false, fmt.Errorf("check chat exists: %w", err)
|
||||
}
|
||||
|
||||
// MemberVersion одним запросом возвращает версию истории чата и признак членства.
|
||||
// Несуществующий чат — ErrNotFound.
|
||||
func (r *ChatRepository) MemberVersion(ctx context.Context, chatID, userID string) (int64, bool, error) {
|
||||
const q = `
|
||||
SELECT c.version,
|
||||
EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id = c.id AND m.user_id = $2::uuid)
|
||||
FROM chats c
|
||||
WHERE c.id = $1::uuid
|
||||
`
|
||||
var version int64
|
||||
var member bool
|
||||
err := r.pool.QueryRow(ctx, q, chatID, userID).Scan(&version, &member)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return 0, false, repository.ErrNotFound
|
||||
}
|
||||
return 0, false, fmt.Errorf("select chat version: %w", err)
|
||||
}
|
||||
return version, member, nil
|
||||
}
|
||||
|
||||
func (r *ChatRepository) ListByMember(ctx context.Context, userID string, cursor time.Time, cursorID string, limit int) ([]*domain.Chat, error) {
|
||||
const qChats = `
|
||||
SELECT c.id, c.title, c.created_at
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1::uuid
|
||||
AND (c.created_at, c.id) > ($2::timestamptz, $3::uuid)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $4
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, qChats, userID, cursor, cursorID, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list chats: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
chats := []*domain.Chat{}
|
||||
var chatIDs []string
|
||||
for rows.Next() {
|
||||
var c domain.Chat
|
||||
if err := rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan chat: %w", err)
|
||||
}
|
||||
c.MemberIDs = []string{}
|
||||
chats = append(chats, &c)
|
||||
chatIDs = append(chatIDs, c.ID)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate chats: %w", err)
|
||||
}
|
||||
if len(chats) == 0 {
|
||||
return chats, nil
|
||||
}
|
||||
|
||||
const qMembers = `SELECT chat_id, user_id FROM chat_members WHERE chat_id = ANY($1::uuid[]) ORDER BY chat_id, user_id`
|
||||
memberRows, err := r.pool.Query(ctx, qMembers, chatIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list members: %w", err)
|
||||
}
|
||||
defer memberRows.Close()
|
||||
|
||||
byChat := make(map[string][]string)
|
||||
for memberRows.Next() {
|
||||
var chatID, uid string
|
||||
if err := memberRows.Scan(&chatID, &uid); err != nil {
|
||||
return nil, fmt.Errorf("scan member: %w", err)
|
||||
}
|
||||
byChat[chatID] = append(byChat[chatID], uid)
|
||||
}
|
||||
if err := memberRows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate members: %w", err)
|
||||
}
|
||||
for _, c := range chats {
|
||||
if members, ok := byChat[c.ID]; ok {
|
||||
c.MemberIDs = members
|
||||
}
|
||||
}
|
||||
return chats, nil
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
)
|
||||
|
||||
type MessageRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewMessageRepository(pool *pgxpool.Pool) *MessageRepository {
|
||||
return &MessageRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *MessageRepository) Create(ctx context.Context, msg *domain.Message) error {
|
||||
const q = `
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5)
|
||||
`
|
||||
if _, err := r.pool.Exec(ctx, q, msg.ID, msg.ChatID, msg.SenderID, msg.Text, msg.CreatedAt); err != nil {
|
||||
return fmt.Errorf("insert message: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateIdempotent вставляет сообщение и ключ в одной транзакции.
|
||||
// Конкурентные запросы с одним ключом сериализуются уникальным индексом
|
||||
// idempotency_keys: второй INSERT ждёт завершения первой транзакции, после
|
||||
// COMMIT получает конфликт (DO NOTHING), откатывает своё сообщение и читает
|
||||
// уже сохранённое. Если первая транзакция откатилась (транзиентная ошибка),
|
||||
// вторая просто продолжает — неуспешный результат не фиксируется.
|
||||
func (r *MessageRepository) CreateIdempotent(ctx context.Context, msg *domain.Message, key, requestHash string, ttl time.Duration) (*domain.Message, bool, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
// Ключ старше TTL больше не защищает от повтора — освобождаем его.
|
||||
const deleteExpired = `
|
||||
DELETE FROM idempotency_keys
|
||||
WHERE user_id = $1::uuid AND chat_id = $2::uuid AND key = $3
|
||||
AND created_at < now() - make_interval(secs => $4)
|
||||
`
|
||||
if _, err := tx.Exec(ctx, deleteExpired, msg.SenderID, msg.ChatID, key, ttl.Seconds()); err != nil {
|
||||
return nil, false, fmt.Errorf("delete expired key: %w", err)
|
||||
}
|
||||
|
||||
const insertMsg = `
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5)
|
||||
`
|
||||
if _, err := tx.Exec(ctx, insertMsg, msg.ID, msg.ChatID, msg.SenderID, msg.Text, msg.CreatedAt); err != nil {
|
||||
return nil, false, fmt.Errorf("insert message: %w", err)
|
||||
}
|
||||
|
||||
if len(msg.AttachmentIDs) > 0 {
|
||||
if err := attachToMessage(ctx, tx, msg); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
}
|
||||
|
||||
const insertKey = `
|
||||
INSERT INTO idempotency_keys (user_id, chat_id, key, request_hash, message_id)
|
||||
VALUES ($1::uuid, $2::uuid, $3, $4, $5::uuid)
|
||||
ON CONFLICT (user_id, chat_id, key) DO NOTHING
|
||||
`
|
||||
tag, err := tx.Exec(ctx, insertKey, msg.SenderID, msg.ChatID, key, requestHash, msg.ID)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("insert idempotency key: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() == 1 {
|
||||
// Версия истории растёт атомарно с сообщением: после COMMIT любое чтение
|
||||
// увидит новую версию и не попадёт в кэш, записанный до этого сообщения.
|
||||
const bumpVersion = `UPDATE chats SET version = version + 1 WHERE id = $1::uuid`
|
||||
if _, err := tx.Exec(ctx, bumpVersion, msg.ChatID); err != nil {
|
||||
return nil, false, fmt.Errorf("bump chat version: %w", err)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, false, fmt.Errorf("commit tx: %w", err)
|
||||
}
|
||||
return msg, true, nil
|
||||
}
|
||||
|
||||
// Ключ уже занят подтверждённой записью: наше сообщение не сохраняем.
|
||||
_ = tx.Rollback(ctx)
|
||||
|
||||
const selectStored = `
|
||||
SELECT k.request_hash, m.id, m.chat_id, m.sender_id, m.text, m.created_at,
|
||||
` + attachmentIDsExpr + `
|
||||
FROM idempotency_keys k
|
||||
JOIN messages m ON m.id = k.message_id
|
||||
WHERE k.user_id = $1::uuid AND k.chat_id = $2::uuid AND k.key = $3
|
||||
`
|
||||
var storedHash string
|
||||
var m domain.Message
|
||||
err = r.pool.QueryRow(ctx, selectStored, msg.SenderID, msg.ChatID, key).
|
||||
Scan(&storedHash, &m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &m.AttachmentIDs)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("select stored message: %w", err)
|
||||
}
|
||||
if storedHash != requestHash {
|
||||
return nil, false, repository.ErrConflict
|
||||
}
|
||||
return &m, false, nil
|
||||
}
|
||||
|
||||
// DeleteExpiredKeys удаляет ключи старше TTL. Безопасно выполнять с любой реплики.
|
||||
func (r *MessageRepository) DeleteExpiredKeys(ctx context.Context, ttl time.Duration) (int64, error) {
|
||||
const q = `DELETE FROM idempotency_keys WHERE created_at < now() - make_interval(secs => $1)`
|
||||
tag, err := r.pool.Exec(ctx, q, ttl.Seconds())
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("delete expired keys: %w", err)
|
||||
}
|
||||
return tag.RowsAffected(), nil
|
||||
}
|
||||
|
||||
func (r *MessageRepository) ListByChat(ctx context.Context, chatID string, cursor time.Time, cursorID string, limit int) ([]*domain.Message, error) {
|
||||
q := `
|
||||
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, ` + attachmentIDsExpr + `
|
||||
FROM messages m
|
||||
WHERE m.chat_id = $1::uuid
|
||||
AND (m.created_at, m.id) > ($2::timestamptz, $3::uuid)
|
||||
ORDER BY m.created_at, m.id
|
||||
LIMIT $4
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, chatID, cursor, cursorID, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list messages: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
msgs := []*domain.Message{}
|
||||
for rows.Next() {
|
||||
var m domain.Message
|
||||
if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &m.AttachmentIDs); err != nil {
|
||||
return nil, fmt.Errorf("scan message: %w", err)
|
||||
}
|
||||
msgs = append(msgs, &m)
|
||||
}
|
||||
return msgs, rows.Err()
|
||||
}
|
||||
|
||||
// attachmentIDsExpr — id вложений сообщения m в порядке отправки (всегда массив).
|
||||
const attachmentIDsExpr = `ARRAY(SELECT ma.attachment_id::text FROM message_attachments ma
|
||||
WHERE ma.message_id = m.id ORDER BY ma.position)`
|
||||
|
||||
// attachToMessage проверяет и привязывает вложения в транзакции сообщения.
|
||||
// FOR SHARE блокирует строки вложений до COMMIT: reconciler (FOR UPDATE) не
|
||||
// удалит вложение, которое сейчас прикрепляют. Вложение должно быть из того же
|
||||
// чата и в статусе ready; чужое, неизвестное и незавершённое — ErrNotFound.
|
||||
func attachToMessage(ctx context.Context, tx pgx.Tx, msg *domain.Message) error {
|
||||
const q = `
|
||||
SELECT id::text, status FROM attachments
|
||||
WHERE id = ANY($1::uuid[]) AND chat_id = $2::uuid
|
||||
FOR SHARE
|
||||
`
|
||||
rows, err := tx.Query(ctx, q, msg.AttachmentIDs, msg.ChatID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("select attachments: %w", err)
|
||||
}
|
||||
status := map[string]string{}
|
||||
for rows.Next() {
|
||||
var id, st string
|
||||
if err := rows.Scan(&id, &st); err != nil {
|
||||
rows.Close()
|
||||
return fmt.Errorf("scan attachment: %w", err)
|
||||
}
|
||||
status[id] = st
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return fmt.Errorf("select attachments: %w", err)
|
||||
}
|
||||
for _, id := range msg.AttachmentIDs {
|
||||
switch status[id] {
|
||||
case domain.AttachmentReady, domain.AttachmentQueued, domain.AttachmentProcessing:
|
||||
// с lab5 можно ссылаться на ещё обрабатываемое вложение
|
||||
case domain.AttachmentFailed, domain.AttachmentMissing:
|
||||
return repository.ErrConflict
|
||||
default:
|
||||
return repository.ErrNotFound // чужое, неизвестное, не принятое (uploading)
|
||||
}
|
||||
}
|
||||
batch := &pgx.Batch{}
|
||||
for i, id := range msg.AttachmentIDs {
|
||||
batch.Queue(`INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1::uuid, $2::uuid, $3)`,
|
||||
msg.ID, id, i)
|
||||
}
|
||||
if err := tx.SendBatch(ctx, batch).Close(); err != nil {
|
||||
return fmt.Errorf("insert message attachments: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,570 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
)
|
||||
|
||||
// PipelineRepository — задания, outbox и фиксация результатов обработки.
|
||||
//
|
||||
// Все переходы job выполняются в транзакциях с блокировкой строки job (FOR UPDATE),
|
||||
// а фиксация результата защищена fencing-токеном (lease_owner + attempt): worker,
|
||||
// потерявший lease, не может опубликовать результат поверх новой попытки.
|
||||
type PipelineRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPipelineRepository(pool *pgxpool.Pool) *PipelineRepository {
|
||||
return &PipelineRepository{pool: pool}
|
||||
}
|
||||
|
||||
// JobRow — состояние логической работы.
|
||||
type JobRow struct {
|
||||
JobID string
|
||||
AttachmentID string
|
||||
PipelineVersion int
|
||||
State string
|
||||
Attempt int
|
||||
MaxAttempts int
|
||||
NextAttemptAt time.Time
|
||||
LeaseUntil *time.Time
|
||||
LeaseOwner string
|
||||
LastError string
|
||||
}
|
||||
|
||||
func jobPayload(jobID, attachmentID string, version, attempt int, created time.Time) ([]byte, error) {
|
||||
return json.Marshal(domain.Job{
|
||||
SchemaVersion: 1, JobID: jobID, Type: domain.JobType, AttachmentID: attachmentID,
|
||||
PipelineVersion: version, Attempt: attempt,
|
||||
CreatedAt: created.UTC().Truncate(time.Second).Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
func event(ctx context.Context, tx pgx.Tx, jobID, ev, detail string) error {
|
||||
_, err := tx.Exec(ctx, `INSERT INTO job_events (job_id, event, detail) VALUES ($1::uuid, $2, NULLIF($3, ''))`, jobID, ev, detail)
|
||||
return err
|
||||
}
|
||||
|
||||
func outboxTx(ctx context.Context, tx pgx.Tx, jobID, attachmentID string, version, attempt int, available time.Time) error {
|
||||
payload, err := jobPayload(jobID, attachmentID, version, attempt, time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO outbox (job_id, payload, available_at) VALUES ($1::uuid, $2, $3)`, jobID, payload, available)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert outbox: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnqueueJobTx создаёт логическую работу (attachment, version) и её первое
|
||||
// сообщение в outbox. Если работа этой версии уже есть — ничего не создаёт и
|
||||
// возвращает существующий job_id (ключ эффекта не дублируется).
|
||||
func EnqueueJobTx(ctx context.Context, tx pgx.Tx, attachmentID string, version, maxAttempts int, reason string) (string, error) {
|
||||
jobID := uuid.NewString()
|
||||
var got string
|
||||
err := tx.QueryRow(ctx, `
|
||||
INSERT INTO jobs (job_id, attachment_id, pipeline_version, state, max_attempts)
|
||||
VALUES ($1::uuid, $2::uuid, $3, 'queued', $4)
|
||||
ON CONFLICT (attachment_id, pipeline_version) DO NOTHING
|
||||
RETURNING job_id::text`, jobID, attachmentID, version, maxAttempts).Scan(&got)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
err = tx.QueryRow(ctx, `SELECT job_id::text FROM jobs WHERE attachment_id = $1::uuid AND pipeline_version = $2`,
|
||||
attachmentID, version).Scan(&got)
|
||||
return got, err
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("insert job: %w", err)
|
||||
}
|
||||
if err := outboxTx(ctx, tx, got, attachmentID, version, 1, time.Now()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return got, event(ctx, tx, got, "enqueued", reason)
|
||||
}
|
||||
|
||||
func lockJob(ctx context.Context, tx pgx.Tx, jobID string) (*JobRow, error) {
|
||||
var j JobRow
|
||||
var owner, lastErr *string
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT job_id::text, attachment_id::text, pipeline_version, state, attempt, max_attempts,
|
||||
next_attempt_at, lease_until, lease_owner, last_error
|
||||
FROM jobs WHERE job_id = $1::uuid FOR UPDATE`, jobID).
|
||||
Scan(&j.JobID, &j.AttachmentID, &j.PipelineVersion, &j.State, &j.Attempt, &j.MaxAttempts,
|
||||
&j.NextAttemptAt, &j.LeaseUntil, &owner, &lastErr)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("lock job: %w", err)
|
||||
}
|
||||
if owner != nil {
|
||||
j.LeaseOwner = *owner
|
||||
}
|
||||
if lastErr != nil {
|
||||
j.LastError = *lastErr
|
||||
}
|
||||
return &j, nil
|
||||
}
|
||||
|
||||
// ClaimOutcome — решение worker по доставленному сообщению.
|
||||
type ClaimOutcome int
|
||||
|
||||
const (
|
||||
Claimed ClaimOutcome = iota // аренда получена — обрабатывать
|
||||
AlreadyDone // эффект уже опубликован — ack без работы
|
||||
Busy // валидная аренда у другого worker — ack (дубль)
|
||||
NotDue // ранняя доставка до next_attempt_at — ack, outbox доставит позже
|
||||
Terminal // failed / задания нет — ack
|
||||
ExhaustedNow // попытки исчерпаны при захвате — уже помечено failed
|
||||
)
|
||||
|
||||
// Claim захватывает работу: queued (и уже наступило next_attempt_at) или
|
||||
// processing с истёкшим lease. Номер попытки растёт при каждом захвате.
|
||||
func (r *PipelineRepository) Claim(ctx context.Context, jobID, owner string, lease time.Duration) (ClaimOutcome, *JobRow, *domain.Attachment, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
j, err := lockJob(ctx, tx, jobID)
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return Terminal, nil, nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
var now time.Time
|
||||
if err := tx.QueryRow(ctx, `SELECT now()`).Scan(&now); err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
switch {
|
||||
case j.State == "done":
|
||||
return AlreadyDone, j, nil, nil
|
||||
case j.State == "failed":
|
||||
return Terminal, j, nil, nil
|
||||
case j.State == "processing" && j.LeaseUntil != nil && j.LeaseUntil.After(now):
|
||||
return Busy, j, nil, nil
|
||||
case j.State == "queued" && j.NextAttemptAt.After(now): // время relay и claim — одно (часы PostgreSQL)
|
||||
return NotDue, j, nil, nil
|
||||
}
|
||||
a, err := scanAttachment(tx.QueryRow(ctx, `SELECT `+attachmentColumns+` FROM attachments WHERE id = $1::uuid FOR UPDATE`, j.AttachmentID))
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return Terminal, j, nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
// Результат этой или более новой версии уже опубликован — повтор не нужен.
|
||||
if a.Status == domain.AttachmentReady && a.PipelineVersion >= j.PipelineVersion {
|
||||
if _, err := tx.Exec(ctx, `UPDATE jobs SET state = 'done', lease_until = NULL, updated_at = now() WHERE job_id = $1::uuid`, jobID); err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
if err := event(ctx, tx, jobID, "noop_already_published", fmt.Sprintf("published_version=%d", a.PipelineVersion)); err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
return AlreadyDone, j, a, tx.Commit(ctx)
|
||||
}
|
||||
takeover := j.State == "processing"
|
||||
j.Attempt++
|
||||
if j.Attempt > j.MaxAttempts {
|
||||
if err := failTx(ctx, tx, j, "retry_exhausted", "attempts exhausted at claim"); err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
return ExhaustedNow, j, a, tx.Commit(ctx)
|
||||
}
|
||||
until := now.Add(lease)
|
||||
if _, err := tx.Exec(ctx, `
|
||||
UPDATE jobs SET state = 'processing', attempt = $2, lease_until = $3, lease_owner = $4, updated_at = now()
|
||||
WHERE job_id = $1::uuid`, jobID, j.Attempt, until, owner); err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
if a.Status == domain.AttachmentQueued {
|
||||
if _, err := tx.Exec(ctx, `UPDATE attachments SET status = 'processing', updated_at = now() WHERE id = $1::uuid`, a.ID); err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
}
|
||||
ev := "claimed"
|
||||
if takeover {
|
||||
ev = "claimed_after_lease_expiry"
|
||||
}
|
||||
if err := event(ctx, tx, jobID, ev, fmt.Sprintf("attempt=%d owner=%s", j.Attempt, owner)); err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
j.State, j.LeaseUntil, j.LeaseOwner = "processing", &until, owner
|
||||
return Claimed, j, a, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// ExtendLease продлевает аренду (heartbeat), пока worker действительно работает.
|
||||
// false — аренда потеряна (её забрал другой worker после истечения).
|
||||
func (r *PipelineRepository) ExtendLease(ctx context.Context, jobID, owner string, attempt int, lease time.Duration) (bool, error) {
|
||||
tag, err := r.pool.Exec(ctx, `
|
||||
UPDATE jobs SET lease_until = now() + make_interval(secs => $4), updated_at = now()
|
||||
WHERE job_id = $1::uuid AND state = 'processing' AND lease_owner = $2 AND attempt = $3`,
|
||||
jobID, owner, attempt, lease.Seconds())
|
||||
return tag.RowsAffected() == 1, err
|
||||
}
|
||||
|
||||
// CompleteResult — что сделала фиксация.
|
||||
type CompleteResult struct {
|
||||
Applied bool // результат этой попытки зафиксирован
|
||||
Published bool // attachment переведён на эту версию
|
||||
LostLease bool // fencing: попытка уже не владеет job
|
||||
NewerPublished int // если опубликована более новая версия — её номер
|
||||
}
|
||||
|
||||
// Complete атомарно публикует результат: варианты (ON CONFLICT DO NOTHING по
|
||||
// ключу эффекта), attachment → ready на этой версии (только если опубликованная
|
||||
// версия меньше — старый результат не перезаписывает новый), job → done.
|
||||
func (r *PipelineRepository) Complete(ctx context.Context, jobID, owner string, attempt int, finalKey string, variants []domain.Variant) (CompleteResult, error) {
|
||||
var res CompleteResult
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
j, err := lockJob(ctx, tx, jobID)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if j.State != "processing" || j.LeaseOwner != owner || j.Attempt != attempt {
|
||||
res.LostLease = true
|
||||
return res, event(ctx, tx, jobID, "result_rejected_lost_lease", fmt.Sprintf("attempt=%d owner=%s", attempt, owner))
|
||||
}
|
||||
batch := &pgx.Batch{}
|
||||
for _, v := range variants {
|
||||
batch.Queue(`
|
||||
INSERT INTO attachment_variants (attachment_id, pipeline_version, name, object_key, content_type, size_bytes, width, height, sha256)
|
||||
VALUES ($1::uuid, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (attachment_id, pipeline_version, name) DO NOTHING`,
|
||||
j.AttachmentID, j.PipelineVersion, v.Name, v.ObjectKey, v.ContentType, v.SizeBytes, v.Width, v.Height, v.SHA256)
|
||||
}
|
||||
if err := tx.SendBatch(ctx, batch).Close(); err != nil {
|
||||
return res, fmt.Errorf("insert variants: %w", err)
|
||||
}
|
||||
tag, err := tx.Exec(ctx, `
|
||||
UPDATE attachments
|
||||
SET status = 'ready', pipeline_version = $2, object_key = $3, error_code = NULL, updated_at = now()
|
||||
WHERE id = $1::uuid AND pipeline_version < $2 AND status <> 'missing'`,
|
||||
j.AttachmentID, j.PipelineVersion, finalKey)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("publish attachment: %w", err)
|
||||
}
|
||||
res.Published = tag.RowsAffected() == 1
|
||||
if !res.Published {
|
||||
_ = tx.QueryRow(ctx, `SELECT pipeline_version FROM attachments WHERE id = $1::uuid`, j.AttachmentID).Scan(&res.NewerPublished)
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `UPDATE jobs SET state = 'done', lease_until = NULL, last_error = NULL, updated_at = now() WHERE job_id = $1::uuid`, jobID); err != nil {
|
||||
return res, err
|
||||
}
|
||||
ev, detail := "done", fmt.Sprintf("attempt=%d", attempt)
|
||||
if !res.Published {
|
||||
ev, detail = "done_not_published_newer_exists", fmt.Sprintf("attempt=%d published_version=%d", attempt, res.NewerPublished)
|
||||
}
|
||||
if err := event(ctx, tx, jobID, ev, detail); err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Applied = true
|
||||
return res, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func failTx(ctx context.Context, tx pgx.Tx, j *JobRow, code, detail string) error {
|
||||
if _, err := tx.Exec(ctx, `
|
||||
UPDATE jobs SET state = 'failed', lease_until = NULL, last_error = $2, updated_at = now()
|
||||
WHERE job_id = $1::uuid`, j.JobID, code+": "+detail); err != nil {
|
||||
return err
|
||||
}
|
||||
// Ready-вложение (переобработка новой версии) не роняется в failed: старый
|
||||
// результат остаётся опубликованным.
|
||||
if _, err := tx.Exec(ctx, `
|
||||
UPDATE attachments SET status = 'failed', error_code = $2, updated_at = now()
|
||||
WHERE id = $1::uuid AND status IN ('queued', 'processing')`, j.AttachmentID, code); err != nil {
|
||||
return err
|
||||
}
|
||||
return event(ctx, tx, j.JobID, "failed", code+": "+detail)
|
||||
}
|
||||
|
||||
// RetryOrFail: временная ошибка с оставшимися попытками → queued с backoff и
|
||||
// новым сообщением outbox (available_at в будущем); иначе → failed.
|
||||
func (r *PipelineRepository) RetryOrFail(ctx context.Context, jobID, owner string, attempt int, code, detail string, permanent bool, backoff time.Duration) (failed bool, err error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
j, err := lockJob(ctx, tx, jobID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if j.State != "processing" || j.LeaseOwner != owner || j.Attempt != attempt {
|
||||
return false, event(ctx, tx, jobID, "error_ignored_lost_lease", code)
|
||||
}
|
||||
if permanent || j.Attempt >= j.MaxAttempts {
|
||||
if !permanent {
|
||||
code = "retry_exhausted"
|
||||
}
|
||||
if err := failTx(ctx, tx, j, code, detail); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, tx.Commit(ctx)
|
||||
}
|
||||
var next time.Time
|
||||
if err := tx.QueryRow(ctx, `
|
||||
UPDATE jobs SET state = 'queued', lease_until = NULL, lease_owner = NULL, last_error = $2,
|
||||
next_attempt_at = now() + make_interval(secs => $3), updated_at = now()
|
||||
WHERE job_id = $1::uuid RETURNING next_attempt_at`, jobID, code+": "+detail, backoff.Seconds()).Scan(&next); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `UPDATE attachments SET status = 'queued', updated_at = now() WHERE id = $1::uuid AND status = 'processing'`, j.AttachmentID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := outboxTx(ctx, tx, jobID, j.AttachmentID, j.PipelineVersion, j.Attempt+1, next); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := event(ctx, tx, jobID, "retry_scheduled", fmt.Sprintf("%s; next_attempt=%d in %s", code, j.Attempt+1, backoff.Round(time.Millisecond))); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return false, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// SweepExpiredLeases возвращает в очередь работу, чей worker умер (lease истёк).
|
||||
func (r *PipelineRepository) SweepExpiredLeases(ctx context.Context) (int, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT job_id::text FROM jobs
|
||||
WHERE state = 'processing' AND lease_until < now()
|
||||
ORDER BY lease_until LIMIT 100 FOR UPDATE SKIP LOCKED`)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var ids []string
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return 0, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
rows.Close()
|
||||
for _, id := range ids {
|
||||
j, err := lockJob(ctx, tx, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if j.Attempt >= j.MaxAttempts {
|
||||
if err := failTx(ctx, tx, j, "retry_exhausted", "lease expired on last attempt"); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
UPDATE jobs SET state = 'queued', lease_until = NULL, lease_owner = NULL, next_attempt_at = now(),
|
||||
last_error = 'lease_expired', updated_at = now() WHERE job_id = $1::uuid`, id); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `UPDATE attachments SET status = 'queued', updated_at = now() WHERE id = $1::uuid AND status = 'processing'`, j.AttachmentID); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := outboxTx(ctx, tx, id, j.AttachmentID, j.PipelineVersion, j.Attempt+1, time.Now()); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err := event(ctx, tx, id, "lease_expired_requeued", fmt.Sprintf("owner=%s attempt=%d", j.LeaseOwner, j.Attempt)); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return len(ids), tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// OutboxMessage — неопубликованное сообщение.
|
||||
type OutboxMessage struct {
|
||||
ID int64
|
||||
JobID string
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
// PublishDue публикует созревшие сообщения outbox. Строки блокируются
|
||||
// FOR UPDATE SKIP LOCKED (несколько relay не мешают друг другу); published_at
|
||||
// ставится только после publisher confirm. Падение между confirm и COMMIT даёт
|
||||
// повторную публикацию — это допустимо: consumer идемпотентен.
|
||||
func (r *PipelineRepository) PublishDue(ctx context.Context, limit int, publish func(context.Context, OutboxMessage) error, afterPublish func()) (int, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
rows, err := tx.Query(ctx, `
|
||||
SELECT id, job_id::text, payload FROM outbox
|
||||
WHERE published_at IS NULL AND available_at <= now()
|
||||
ORDER BY id LIMIT $1 FOR UPDATE SKIP LOCKED`, limit)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var msgs []OutboxMessage
|
||||
for rows.Next() {
|
||||
var m OutboxMessage
|
||||
if err := rows.Scan(&m.ID, &m.JobID, &m.Payload); err != nil {
|
||||
rows.Close()
|
||||
return 0, err
|
||||
}
|
||||
msgs = append(msgs, m)
|
||||
}
|
||||
rows.Close()
|
||||
sent := 0
|
||||
for _, m := range msgs {
|
||||
if err := publish(ctx, m); err != nil {
|
||||
_, _ = tx.Exec(ctx, `UPDATE outbox SET publish_attempts = publish_attempts + 1 WHERE id = $1`, m.ID)
|
||||
_ = tx.Commit(ctx)
|
||||
return sent, err
|
||||
}
|
||||
if afterPublish != nil {
|
||||
afterPublish() // точка внедрения сбоя «после confirm до COMMIT»
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `UPDATE outbox SET published_at = now(), publish_attempts = publish_attempts + 1 WHERE id = $1`, m.ID); err != nil {
|
||||
return sent, err
|
||||
}
|
||||
sent++
|
||||
}
|
||||
return sent, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// Replay — явный операторский повтор failed-работы (failed → queued) с аудитом.
|
||||
// Успешную работу повторить нельзя: «повторить» не означает исполнить дубль.
|
||||
func (r *PipelineRepository) Replay(ctx context.Context, attachmentID string, version int, actor string) (string, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
var jobID string
|
||||
if err := tx.QueryRow(ctx, `SELECT job_id::text FROM jobs WHERE attachment_id = $1::uuid AND pipeline_version = $2`,
|
||||
attachmentID, version).Scan(&jobID); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
j, err := lockJob(ctx, tx, jobID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if j.State != "failed" {
|
||||
_ = event(ctx, tx, jobID, "replay_refused", "state="+j.State+" actor="+actor)
|
||||
_ = tx.Commit(ctx)
|
||||
return jobID, fmt.Errorf("%w: job is %s, only failed jobs can be replayed", repository.ErrConflict, j.State)
|
||||
}
|
||||
var staging *string
|
||||
if err := tx.QueryRow(ctx, `SELECT staging_key FROM attachments WHERE id = $1::uuid FOR UPDATE`, attachmentID).Scan(&staging); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if staging == nil {
|
||||
_ = event(ctx, tx, jobID, "replay_refused", "staging removed by retention, actor="+actor)
|
||||
_ = tx.Commit(ctx)
|
||||
return jobID, fmt.Errorf("%w: source removed by staging retention", repository.ErrConflict)
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
UPDATE jobs SET state = 'queued', attempt = 0, next_attempt_at = now(), last_error = NULL, updated_at = now()
|
||||
WHERE job_id = $1::uuid`, jobID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `UPDATE attachments SET status = 'queued', error_code = NULL, updated_at = now() WHERE id = $1::uuid AND status = 'failed'`, attachmentID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := outboxTx(ctx, tx, jobID, attachmentID, version, 1, time.Now()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := event(ctx, tx, jobID, "replay", "actor="+actor); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return jobID, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// Reprocess ставит работу новой версии pipeline для уже обработанного вложения
|
||||
// (трек 2). Вложение остаётся ready со старыми вариантами до публикации новых.
|
||||
func (r *PipelineRepository) Reprocess(ctx context.Context, attachmentID string, version, maxAttempts int) (string, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
id, err := EnqueueJobTx(ctx, tx, attachmentID, version, maxAttempts, "reprocess")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// FailedJobs — реестр failed для оператора.
|
||||
func (r *PipelineRepository) FailedJobs(ctx context.Context, limit int) ([]JobRow, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT job_id::text, attachment_id::text, pipeline_version, state, attempt, max_attempts, COALESCE(last_error, '')
|
||||
FROM jobs WHERE state = 'failed' ORDER BY updated_at DESC LIMIT $1`, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []JobRow
|
||||
for rows.Next() {
|
||||
var j JobRow
|
||||
if err := rows.Scan(&j.JobID, &j.AttachmentID, &j.PipelineVersion, &j.State, &j.Attempt, &j.MaxAttempts, &j.LastError); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, j)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// StagingCandidate — staging-объект, который можно удалить.
|
||||
type StagingCandidate struct {
|
||||
AttachmentID string
|
||||
Key string
|
||||
Status string
|
||||
}
|
||||
|
||||
// StagingToClean: ready с отдельным staging-объектом (результат уже опубликован
|
||||
// не менее readyGrace назад) и failed старше retention (replay больше невозможен).
|
||||
func (r *PipelineRepository) StagingToClean(ctx context.Context, readyGrace, failedRetention time.Duration, limit int) ([]StagingCandidate, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id::text, staging_key, status FROM attachments
|
||||
WHERE staging_key IS NOT NULL AND (
|
||||
(status = 'ready' AND staging_key <> object_key AND updated_at < now() - make_interval(secs => $1))
|
||||
OR (status = 'failed' AND updated_at < now() - make_interval(secs => $2)))
|
||||
ORDER BY updated_at LIMIT $3`, readyGrace.Seconds(), failedRetention.Seconds(), limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []StagingCandidate
|
||||
for rows.Next() {
|
||||
var c StagingCandidate
|
||||
if err := rows.Scan(&c.AttachmentID, &c.Key, &c.Status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, c)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ClearStaging отмечает, что staging-объект удалён (только если ключ не менялся).
|
||||
func (r *PipelineRepository) ClearStaging(ctx context.Context, attachmentID, key string) error {
|
||||
_, err := r.pool.Exec(ctx, `UPDATE attachments SET staging_key = NULL WHERE id = $1::uuid AND staging_key = $2`, attachmentID, key)
|
||||
return err
|
||||
}
|
||||
|
||||
// Attachment — доверенные метаданные для worker.
|
||||
func (r *PipelineRepository) Attachment(ctx context.Context, id string) (*domain.Attachment, error) {
|
||||
return scanAttachment(r.pool.QueryRow(ctx, `SELECT `+attachmentColumns+` FROM attachments WHERE id = $1::uuid`, id))
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// NewPool создаёт пул соединений с PostgreSQL.
|
||||
// Параметры ограничены, чтобы не перегрузить БД.
|
||||
func NewPool(ctx context.Context, dsn string, maxConns int32) (*pgxpool.Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse dsn: %w", err)
|
||||
}
|
||||
|
||||
cfg.MaxConns = maxConns
|
||||
cfg.MinConns = min(2, maxConns)
|
||||
cfg.MaxConnIdleTime = 5 * time.Minute
|
||||
cfg.MaxConnLifetime = 30 * time.Minute
|
||||
cfg.HealthCheckPeriod = 30 * time.Second
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create pool: %w", err)
|
||||
}
|
||||
|
||||
// Проверяем, что соединение работает.
|
||||
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
if err := pool.Ping(pingCtx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping: %w", err)
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
)
|
||||
|
||||
type UserRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewUserRepository(pool *pgxpool.Pool) *UserRepository {
|
||||
return &UserRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *UserRepository) Create(ctx context.Context, user *domain.User, passwordHash string) error {
|
||||
const q = `
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES ($1::uuid, $2, $3, $4, $5)
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q, user.ID, user.Username, user.DisplayName, passwordHash, user.CreatedAt)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
return repository.ErrConflict
|
||||
}
|
||||
return fmt.Errorf("insert user: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetByID(ctx context.Context, id string) (*domain.User, error) {
|
||||
const q = `SELECT id, username, display_name, created_at FROM users WHERE id = $1::uuid`
|
||||
var u domain.User
|
||||
err := r.pool.QueryRow(ctx, q, id).Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("select user by id: %w", err)
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetByUsername(ctx context.Context, username string) (*domain.User, error) {
|
||||
const q = `SELECT id, username, display_name, created_at FROM users WHERE username = $1`
|
||||
var u domain.User
|
||||
err := r.pool.QueryRow(ctx, q, username).Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("select user by username: %w", err)
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) GetPasswordHash(ctx context.Context, userID string) (string, error) {
|
||||
const q = `SELECT password_hash FROM users WHERE id = $1::uuid`
|
||||
var hash string
|
||||
err := r.pool.QueryRow(ctx, q, userID).Scan(&hash)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", repository.ErrNotFound
|
||||
}
|
||||
return "", fmt.Errorf("select password hash: %w", err)
|
||||
}
|
||||
return hash, nil
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
)
|
||||
|
||||
// Ключ кэша истории всегда содержит версию чата из PostgreSQL (chats.version),
|
||||
// которая растёт в одной транзакции с сообщением. Поэтому после подтверждённой
|
||||
// записи следующее чтение идёт по новому ключу, даже если Redis отказал в
|
||||
// любой команде: устаревшие ключи просто истекают по TTL.
|
||||
//
|
||||
// CACHE_INVALIDATION влияет только на стоимость записи (эксперимент трека 1):
|
||||
// - "version": запись не обращается к Redis;
|
||||
// - "scan": дополнительно удаляет ключи чата через SCAN, как в lab2 —
|
||||
// O(размер keyspace/100) запросов к Redis на запись. На корректность не влияет.
|
||||
const (
|
||||
InvalidationScan = "scan"
|
||||
InvalidationVersion = "version"
|
||||
)
|
||||
|
||||
type MessageCache struct {
|
||||
client *redis.Client
|
||||
scanOnSet bool
|
||||
}
|
||||
|
||||
func NewMessageCache(client *redis.Client, mode string) *MessageCache {
|
||||
return &MessageCache{client: client, scanOnSet: mode == InvalidationScan}
|
||||
}
|
||||
|
||||
type cacheEntry struct {
|
||||
Items []*domain.Message `json:"items"`
|
||||
NextCursor string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
func cacheKey(chatID string, version int64, cursor string, limit int) string {
|
||||
return fmt.Sprintf("messages:%s:v%d:%s:%d", chatID, version, cursor, limit)
|
||||
}
|
||||
|
||||
func (c *MessageCache) Get(ctx context.Context, chatID string, version int64, cursor string, limit int) ([]*domain.Message, string, bool) {
|
||||
raw, err := c.client.Get(ctx, cacheKey(chatID, version, cursor, limit)).Bytes()
|
||||
if err != nil {
|
||||
return nil, "", false
|
||||
}
|
||||
var e cacheEntry
|
||||
if err := json.Unmarshal(raw, &e); err != nil {
|
||||
return nil, "", false
|
||||
}
|
||||
return e.Items, e.NextCursor, true
|
||||
}
|
||||
|
||||
func (c *MessageCache) Set(ctx context.Context, chatID string, version int64, cursor string, limit int, items []*domain.Message, next string) {
|
||||
e := cacheEntry{Items: items, NextCursor: next}
|
||||
raw, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
slog.Warn("marshal cache entry failed", "err", err)
|
||||
return
|
||||
}
|
||||
if err := c.client.Set(ctx, cacheKey(chatID, version, cursor, limit), raw, 30*time.Second).Err(); err != nil {
|
||||
// Redis недоступен — не падаем, работаем без кэша.
|
||||
slog.Warn("redis set failed", "chat_id", chatID, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// OnWrite вызывается после успешной записи сообщения. Свежесть чтения уже
|
||||
// обеспечена ростом chats.version в транзакции записи; в режиме scan здесь
|
||||
// только воспроизводится стоимость удаления старых ключей (lab2), и ошибка
|
||||
// Redis не может сделать историю устаревшей.
|
||||
func (c *MessageCache) OnWrite(ctx context.Context, chatID string) {
|
||||
if !c.scanOnSet {
|
||||
return
|
||||
}
|
||||
if err := c.Invalidate(ctx, chatID); err != nil {
|
||||
slog.Warn("redis scan invalidation failed", "chat_id", chatID, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Invalidate удаляет все ключи messages:{chatID}:* через SCAN.
|
||||
func (c *MessageCache) Invalidate(ctx context.Context, chatID string) error {
|
||||
pattern := fmt.Sprintf("messages:%s:*", chatID)
|
||||
iter := c.client.Scan(ctx, 0, pattern, 100).Iterator()
|
||||
for iter.Next(ctx) {
|
||||
_ = c.client.Del(ctx, iter.Val()).Err()
|
||||
}
|
||||
return iter.Err()
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
)
|
||||
|
||||
type SessionRepository struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func NewSessionRepository(client *redis.Client) *SessionRepository {
|
||||
return &SessionRepository{client: client}
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Create(ctx context.Context, s *domain.Session) error {
|
||||
key := "session:" + s.Token
|
||||
ttl := time.Until(s.ExpiresAt)
|
||||
if ttl <= 0 {
|
||||
return fmt.Errorf("session already expired")
|
||||
}
|
||||
if err := r.client.Set(ctx, key, s.UserID, ttl).Err(); err != nil {
|
||||
return fmt.Errorf("set session: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SessionRepository) GetByToken(ctx context.Context, token string) (*domain.Session, error) {
|
||||
key := "session:" + token
|
||||
userID, err := r.client.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("get session: %w", err)
|
||||
}
|
||||
ttl, err := r.client.TTL(ctx, key).Result()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ttl: %w", err)
|
||||
}
|
||||
return &domain.Session{
|
||||
Token: token,
|
||||
UserID: userID,
|
||||
ExpiresAt: time.Now().UTC().Add(ttl),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Delete(ctx context.Context, token string) error {
|
||||
return r.client.Del(ctx, "session:"+token).Err()
|
||||
}
|
||||
|
||||
func (r *SessionRepository) DeleteExpired(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewClient(ctx context.Context, url string) (*redis.Client, error) {
|
||||
opts, err := redis.ParseURL(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse redis url: %w", err)
|
||||
}
|
||||
client := redis.NewClient(opts)
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
return nil, fmt.Errorf("ping redis: %w", err)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
)
|
||||
|
||||
var ErrNotFound = errNotFound{}
|
||||
|
||||
type errNotFound struct{}
|
||||
|
||||
func (errNotFound) Error() string { return "not found" }
|
||||
|
||||
var ErrConflict = errConflict{}
|
||||
|
||||
type errConflict struct{}
|
||||
|
||||
func (errConflict) Error() string { return "conflict" }
|
||||
|
||||
// UserRepository
|
||||
type UserRepository interface {
|
||||
Create(ctx context.Context, user *domain.User, passwordHash string) error
|
||||
GetByID(ctx context.Context, id string) (*domain.User, error)
|
||||
GetByUsername(ctx context.Context, username string) (*domain.User, error)
|
||||
GetPasswordHash(ctx context.Context, userID string) (string, error)
|
||||
}
|
||||
|
||||
// ChatRepository
|
||||
type ChatRepository interface {
|
||||
Create(ctx context.Context, chat *domain.Chat) error
|
||||
GetByID(ctx context.Context, id string) (*domain.Chat, error)
|
||||
IsMember(ctx context.Context, chatID, userID string) (bool, error)
|
||||
// MemberVersion — версия истории чата (растёт с каждым сообщением) и членство.
|
||||
MemberVersion(ctx context.Context, chatID, userID string) (version int64, member bool, err error)
|
||||
ListByMember(ctx context.Context, userID string, cursor time.Time, cursorID string, limit int) ([]*domain.Chat, error)
|
||||
}
|
||||
|
||||
// MessageRepository
|
||||
type MessageRepository interface {
|
||||
Create(ctx context.Context, msg *domain.Message) error
|
||||
// CreateIdempotent атомарно создаёт сообщение и запись ключа идемпотентности.
|
||||
// Если ключ уже есть с тем же requestHash — возвращает ранее созданное сообщение
|
||||
// и created=false; с другим requestHash — ErrConflict.
|
||||
CreateIdempotent(ctx context.Context, msg *domain.Message, key, requestHash string, ttl time.Duration) (stored *domain.Message, created bool, err error)
|
||||
DeleteExpiredKeys(ctx context.Context, ttl time.Duration) (int64, error)
|
||||
ListByChat(ctx context.Context, chatID string, cursor time.Time, cursorID string, limit int) ([]*domain.Message, error)
|
||||
}
|
||||
|
||||
// SessionRepository
|
||||
type SessionRepository interface {
|
||||
Create(ctx context.Context, s *domain.Session) error
|
||||
GetByToken(ctx context.Context, token string) (*domain.Session, error)
|
||||
Delete(ctx context.Context, token string) error
|
||||
DeleteExpired(ctx context.Context) error
|
||||
}
|
||||
@@ -0,0 +1,370 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg" // DecodeConfig для раннего отказа по размерам
|
||||
_ "image/png"
|
||||
"io"
|
||||
"log/slog"
|
||||
"mime"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/imagecheck"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
"github.com/charawein/mtusi2026lab5/internal/storage"
|
||||
)
|
||||
|
||||
// Раскладка bucket (все ключи назначает сервер, filename в них не попадает):
|
||||
//
|
||||
// s/{chat}/{id} — staging: проверенный оригинал до обработки
|
||||
// o/{chat}/{id} — опубликованный оригинал (worker копирует из staging)
|
||||
// v/{chat}/{id}/p{ver}/{variant} — варианты версии pipeline ver
|
||||
// a/{chat}/{id} — оригиналы lab4 (остаются на месте)
|
||||
const (
|
||||
ObjectPrefix = "a/"
|
||||
StagingPrefix = "s/"
|
||||
OriginalPrefix = "o/"
|
||||
VariantPrefix = "v/"
|
||||
)
|
||||
|
||||
type AttachmentRepository interface {
|
||||
CreateIntent(ctx context.Context, a *domain.Attachment) error
|
||||
MarkQueued(ctx context.Context, a *domain.Attachment, pipelineVersion, maxAttempts int) (string, error)
|
||||
DeleteIntent(ctx context.Context, id string) error
|
||||
GetForMember(ctx context.Context, id, userID string) (*domain.Attachment, error)
|
||||
Variants(ctx context.Context, attachmentID string, version int) ([]domain.Variant, error)
|
||||
}
|
||||
|
||||
type AttachmentService struct {
|
||||
repo AttachmentRepository
|
||||
chats repository.ChatRepository
|
||||
store storage.Store
|
||||
maxBytes int64
|
||||
maxPixels int64
|
||||
pipelineVersion int
|
||||
maxAttempts int
|
||||
}
|
||||
|
||||
func NewAttachmentService(repo AttachmentRepository, chats repository.ChatRepository, store storage.Store, maxBytes, maxPixels int64, pipelineVersion, maxAttempts int) *AttachmentService {
|
||||
return &AttachmentService{repo: repo, chats: chats, store: store, maxBytes: maxBytes, maxPixels: maxPixels,
|
||||
pipelineVersion: pipelineVersion, maxAttempts: maxAttempts}
|
||||
}
|
||||
|
||||
// validateFilename: 1–255 code points, не только пробелы, без / \ NUL и управляющих.
|
||||
func validateFilename(s string) bool {
|
||||
if !utf8.ValidString(s) || strings.TrimSpace(s) == "" {
|
||||
return false
|
||||
}
|
||||
if n := utf8.RuneCountInString(s); n < 1 || n > 255 {
|
||||
return false
|
||||
}
|
||||
for _, r := range s {
|
||||
if r == '/' || r == '\\' || r == 0 || unicode.IsControl(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// declaredType нормализует Content-Type запроса; допустимы только PNG/JPEG.
|
||||
func declaredType(h string) (string, bool) {
|
||||
mt, _, err := mime.ParseMediaType(h)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
mt = strings.ToLower(mt)
|
||||
return mt, mt == imagecheck.PNG || mt == imagecheck.JPEG
|
||||
}
|
||||
|
||||
// Upload принимает сырые байты одного изображения и возвращает снимок queued (202).
|
||||
//
|
||||
// Порядок: намерение в PostgreSQL (uploading) → потоковый PUT в S3-staging,
|
||||
// параллельно SHA-256 и структурная проверка изображения → ОДНА транзакция:
|
||||
// status=queued + задание + сообщение outbox. 202 возвращается только после
|
||||
// COMMIT: в этот момент устойчиво сохранены проверенный оригинал (staging),
|
||||
// метаданные и намерение обработки; broker для 202 не нужен (outbox опубликует).
|
||||
//
|
||||
// Ошибка до MarkQueued (проверка, PUT, обрыв клиента) компенсируется сразу:
|
||||
// удаляются объект и намерение. Ошибка самого MarkQueued — неопределённый исход
|
||||
// (COMMIT мог пройти): объект НЕ удаляется; остаток uploading+объект уберёт
|
||||
// reconciler после grace. Состояния «queued без оригинала» не бывает.
|
||||
func (s *AttachmentService) Upload(ctx context.Context, ownerID, chatID, filename, contentType string, body io.Reader, contentLength int64) (*domain.Attachment, error) {
|
||||
parsed, err := uuid.Parse(chatID)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
chatID = parsed.String()
|
||||
ok, err := s.chats.IsMember(ctx, chatID, ownerID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if !validateFilename(filename) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
declared, ok := declaredType(contentType)
|
||||
if !ok {
|
||||
return nil, ErrUnsupportedMedia
|
||||
}
|
||||
// Лимит размера — до чтения тела.
|
||||
if contentLength > s.maxBytes {
|
||||
return nil, ErrPayloadTooLarge
|
||||
}
|
||||
size := contentLength
|
||||
if size < 0 {
|
||||
// Без Content-Length (chunked): буфер не больше лимита + 1 байт.
|
||||
buf, err := io.ReadAll(io.LimitReader(body, s.maxBytes+1))
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
if int64(len(buf)) > s.maxBytes {
|
||||
return nil, ErrPayloadTooLarge
|
||||
}
|
||||
body, size = bytes.NewReader(buf), int64(len(buf))
|
||||
}
|
||||
if size == 0 {
|
||||
return nil, ErrUnsupportedMedia
|
||||
}
|
||||
|
||||
// Формат — по содержимому; подмена Content-Type → 415.
|
||||
br := bufio.NewReaderSize(body, 64*1024)
|
||||
head, _ := br.Peek(int(min(size, 64*1024)))
|
||||
format, ok := imagecheck.Sniff(head)
|
||||
if !ok || format != declared {
|
||||
return nil, ErrUnsupportedMedia
|
||||
}
|
||||
// Ранний отказ по пикселям, если заголовок уместился в первые 64 KiB.
|
||||
if cfg, _, err := image.DecodeConfig(bytes.NewReader(head)); err == nil {
|
||||
if int64(cfg.Width)*int64(cfg.Height) > s.maxPixels {
|
||||
return nil, ErrPayloadTooLarge
|
||||
}
|
||||
}
|
||||
|
||||
now := nowUTC()
|
||||
id := newID()
|
||||
a := &domain.Attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: ownerID, Filename: filename,
|
||||
ObjectKey: StagingPrefix + chatID + "/" + id,
|
||||
Status: domain.AttachmentUploading, CreatedAt: now, UpdatedAt: now,
|
||||
}
|
||||
if err := s.repo.CreateIntent(ctx, a); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sum, putErr := s.putValidated(ctx, a.ObjectKey, br, size, format)
|
||||
if putErr != nil {
|
||||
s.compensate(ctx, a, putErr)
|
||||
return nil, putErr
|
||||
}
|
||||
|
||||
a.Status = domain.AttachmentQueued
|
||||
a.ContentType = format
|
||||
a.SizeBytes = size
|
||||
a.SHA256 = sum
|
||||
a.StagingKey = a.ObjectKey
|
||||
a.UpdatedAt = nowUTC()
|
||||
// Байты уже сохранены: доводим учёт до конца, даже если клиент отвалился.
|
||||
mctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
defer cancel()
|
||||
if _, err := s.repo.MarkQueued(mctx, a, s.pipelineVersion, s.maxAttempts); err != nil {
|
||||
s.markReadyFailed(ctx, a, err)
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
var errClientBody = errors.New("request body read failed")
|
||||
|
||||
// trackingReader запоминает ошибку чтения, отличную от EOF.
|
||||
type trackingReader struct {
|
||||
r io.Reader
|
||||
err error
|
||||
}
|
||||
|
||||
func (t *trackingReader) Read(p []byte) (int, error) {
|
||||
n, err := t.r.Read(p)
|
||||
if err != nil && err != io.EOF {
|
||||
t.err = err
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
type bodyReader struct{ r io.Reader }
|
||||
|
||||
func (b bodyReader) Read(p []byte) (int, error) {
|
||||
n, err := b.r.Read(p)
|
||||
if err != nil && err != io.EOF {
|
||||
return n, errors.Join(errClientBody, err)
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// putValidated пишет объект в S3 и одновременно проверяет изображение и считает
|
||||
// SHA-256. Байты проходят потоком (pipe), в памяти — только буферы чтения.
|
||||
func (s *AttachmentService) putValidated(ctx context.Context, key string, body io.Reader, size int64, format string) (string, error) {
|
||||
pr, pw := io.Pipe()
|
||||
valErr := make(chan error, 1)
|
||||
go func() {
|
||||
tr := &trackingReader{r: pr}
|
||||
_, err := imagecheck.Validate(tr, format, s.maxPixels)
|
||||
if tr.err != nil {
|
||||
// Поток оборвали снаружи (PUT в S3 упал или клиент отвалился):
|
||||
// изображение тут ни при чём, причину определит putErr.
|
||||
valErr <- nil
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
pr.CloseWithError(err) // прерывает PUT: дальше писать некуда
|
||||
}
|
||||
valErr <- err
|
||||
}()
|
||||
|
||||
h := sha256.New()
|
||||
src := io.TeeReader(io.TeeReader(bodyReader{body}, h), pw)
|
||||
putErr := s.store.Put(ctx, key, src, size, format)
|
||||
if putErr != nil {
|
||||
pw.CloseWithError(putErr)
|
||||
} else {
|
||||
pw.Close()
|
||||
}
|
||||
vErr := <-valErr
|
||||
|
||||
switch {
|
||||
case errors.Is(vErr, imagecheck.ErrTooLarge):
|
||||
return "", ErrPayloadTooLarge
|
||||
case errors.Is(vErr, imagecheck.ErrInvalid):
|
||||
return "", ErrUnsupportedMedia
|
||||
case errors.Is(putErr, errClientBody):
|
||||
return "", ErrInvalidInput
|
||||
case putErr != nil:
|
||||
slog.Warn("attachment upload to s3 failed", "err", putErr)
|
||||
return "", ErrUnavailable
|
||||
case vErr != nil:
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// markReadyFailed: исход UPDATE неизвестен — объект оставляем. Пробуем убрать
|
||||
// намерение (DELETE ... WHERE status='uploading' безопасен при любом исходе:
|
||||
// если UPDATE уже закоммичен, он ничего не удалит). Если и это не удалось —
|
||||
// uploading+объект уберёт reconciler после grace period.
|
||||
func (s *AttachmentService) markReadyFailed(ctx context.Context, a *domain.Attachment, cause error) {
|
||||
cctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := s.repo.DeleteIntent(cctx, a.ID); err != nil {
|
||||
slog.Warn("mark ready failed, intent left for reconciler", "attachment_id", a.ID, "cause", cause.Error(), "err", err)
|
||||
return
|
||||
}
|
||||
slog.Warn("mark ready failed, object left for reconciler", "attachment_id", a.ID, "cause", cause.Error())
|
||||
}
|
||||
|
||||
// compensate убирает следы неудачной загрузки. Работает и после обрыва клиента
|
||||
// (контекст запроса отменён), поэтому использует отдельный контекст.
|
||||
func (s *AttachmentService) compensate(ctx context.Context, a *domain.Attachment, cause error) {
|
||||
cctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := s.store.Delete(cctx, a.ObjectKey); err != nil {
|
||||
slog.Warn("compensation: delete object failed, reconciler will retry", "attachment_id", a.ID, "err", err)
|
||||
}
|
||||
if err := s.repo.DeleteIntent(cctx, a.ID); err != nil {
|
||||
slog.Warn("compensation: delete intent failed, reconciler will retry", "attachment_id", a.ID, "err", err)
|
||||
}
|
||||
slog.Info("attachment upload compensated", "attachment_id", a.ID, "cause", cause.Error())
|
||||
}
|
||||
|
||||
// Get возвращает метаданные вложения участнику его чата (queued/processing/
|
||||
// ready/failed); у ready — варианты опубликованной версии pipeline.
|
||||
func (s *AttachmentService) Get(ctx context.Context, userID, id string) (*domain.Attachment, error) {
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
a, err := s.repo.GetForMember(ctx, parsed.String(), userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
switch a.Status {
|
||||
case domain.AttachmentReady:
|
||||
if a.PipelineVersion > 0 {
|
||||
if a.Variants, err = s.repo.Variants(ctx, a.ID, a.PipelineVersion); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return a, nil
|
||||
case domain.AttachmentQueued, domain.AttachmentProcessing, domain.AttachmentFailed:
|
||||
return a, nil
|
||||
case domain.AttachmentMissing:
|
||||
// Метаданные есть, байтов нет (обнаружил reconciler): ошибка сервера, не клиента.
|
||||
return nil, ErrUnavailable
|
||||
default:
|
||||
return nil, ErrNotFound // загрузка не завершена — объекта для клиента ещё нет
|
||||
}
|
||||
}
|
||||
|
||||
// Content — что отдавать клиенту: тип, размер и ключ объекта.
|
||||
type Content struct {
|
||||
ContentType string
|
||||
Size int64
|
||||
Key string
|
||||
}
|
||||
|
||||
// Open открывает оригинал или вариант на чтение. Вызывающий закрывает ReadCloser.
|
||||
// queued/processing/failed → 409 (объект не готов), отсутствующий у ready
|
||||
// вариант → 404.
|
||||
func (s *AttachmentService) Open(ctx context.Context, userID, id, variant string) (*Content, io.ReadCloser, error) {
|
||||
a, err := s.Get(ctx, userID, id)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if a.Status != domain.AttachmentReady {
|
||||
return nil, nil, ErrConflict
|
||||
}
|
||||
c := &Content{ContentType: a.ContentType, Size: a.SizeBytes, Key: a.ObjectKey}
|
||||
if variant != "" && variant != "original" {
|
||||
c = nil
|
||||
for _, v := range a.Variants {
|
||||
if v.Name == variant {
|
||||
c = &Content{ContentType: v.ContentType, Size: v.SizeBytes, Key: v.ObjectKey}
|
||||
}
|
||||
}
|
||||
if c == nil {
|
||||
return nil, nil, ErrNotFound
|
||||
}
|
||||
}
|
||||
rc, size, err := s.store.Get(ctx, c.Key)
|
||||
if err != nil {
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
slog.Error("attachment object missing in s3", "attachment_id", a.ID)
|
||||
} else {
|
||||
slog.Warn("s3 get failed", "attachment_id", a.ID, "err", err)
|
||||
}
|
||||
return nil, nil, ErrUnavailable
|
||||
}
|
||||
if size != c.Size {
|
||||
rc.Close()
|
||||
slog.Error("attachment size mismatch", "attachment_id", a.ID, "variant", variant, "s3", size, "db", c.Size)
|
||||
return nil, nil, ErrUnavailable
|
||||
}
|
||||
return c, rc, nil
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/storage"
|
||||
)
|
||||
|
||||
// failingStore читает failAfter байт и падает — как PUT в недоступный S3.
|
||||
type failingStore struct {
|
||||
storage.Store
|
||||
failAfter int64
|
||||
got []byte
|
||||
}
|
||||
|
||||
func (f *failingStore) Put(_ context.Context, _ string, r io.Reader, size int64, _ string) error {
|
||||
if f.failAfter < 0 {
|
||||
b, err := io.ReadAll(r)
|
||||
f.got = b
|
||||
return err
|
||||
}
|
||||
b, err := io.ReadAll(io.LimitReader(r, f.failAfter))
|
||||
f.got = b
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return errors.New("s3 put: connection refused")
|
||||
}
|
||||
|
||||
type errReader struct{ data []byte }
|
||||
|
||||
func (e *errReader) Read(p []byte) (int, error) {
|
||||
if len(e.data) == 0 {
|
||||
return 0, errors.New("connection reset by peer")
|
||||
}
|
||||
n := copy(p, e.data)
|
||||
e.data = e.data[n:]
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func samplePNG(t *testing.T) []byte {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 200, 150))
|
||||
for y := 0; y < 150; y++ {
|
||||
for x := 0; x < 200; x++ {
|
||||
img.Set(x, y, color.RGBA{uint8(x), uint8(y), uint8(x ^ y), 255})
|
||||
}
|
||||
}
|
||||
var b bytes.Buffer
|
||||
if err := png.Encode(&b, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
func TestPutValidatedClassifiesCause(t *testing.T) {
|
||||
data := samplePNG(t)
|
||||
size := int64(len(data))
|
||||
svc := &AttachmentService{maxBytes: 10 << 20, maxPixels: 40_000_000}
|
||||
|
||||
// S3 падает посреди потока: это 503, а не «битое изображение».
|
||||
svc.store = &failingStore{failAfter: size / 3}
|
||||
if _, err := svc.putValidated(context.Background(), "k", bytes.NewReader(data), size, "image/png"); !errors.Is(err, ErrUnavailable) {
|
||||
t.Errorf("s3 failure mid-stream: got %v, want ErrUnavailable", err)
|
||||
}
|
||||
// S3 падает сразу.
|
||||
svc.store = &failingStore{failAfter: 0}
|
||||
if _, err := svc.putValidated(context.Background(), "k", bytes.NewReader(data), size, "image/png"); !errors.Is(err, ErrUnavailable) {
|
||||
t.Errorf("s3 failure at start: got %v, want ErrUnavailable", err)
|
||||
}
|
||||
// Клиент оборвал тело: 400, не 415 и не 503.
|
||||
svc.store = &failingStore{failAfter: -1}
|
||||
if _, err := svc.putValidated(context.Background(), "k", &errReader{data: data[:len(data)/2]}, size, "image/png"); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("client body error: got %v, want ErrInvalidInput", err)
|
||||
}
|
||||
// Битое изображение при исправном S3: 415.
|
||||
bad := bytes.Clone(data)
|
||||
bad[len(bad)/2] ^= 0xFF
|
||||
svc.store = &failingStore{failAfter: -1}
|
||||
if _, err := svc.putValidated(context.Background(), "k", bytes.NewReader(bad), size, "image/png"); !errors.Is(err, ErrUnsupportedMedia) {
|
||||
t.Errorf("corrupt image: got %v, want ErrUnsupportedMedia", err)
|
||||
}
|
||||
// Всё хорошо: SHA-256 и байты целиком дошли до S3.
|
||||
ok := &failingStore{failAfter: -1}
|
||||
svc.store = ok
|
||||
sum, err := svc.putValidated(context.Background(), "k", bytes.NewReader(data), size, "image/png")
|
||||
if err != nil || len(sum) != 64 || !bytes.Equal(ok.got, data) {
|
||||
t.Errorf("valid upload: sum=%q err=%v bytes_equal=%v", sum, err, bytes.Equal(ok.got, data))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
)
|
||||
|
||||
type AuthService struct {
|
||||
users repository.UserRepository
|
||||
sessions repository.SessionRepository
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func NewAuthService(users repository.UserRepository, sessions repository.SessionRepository, ttl time.Duration) *AuthService {
|
||||
return &AuthService{users: users, sessions: sessions, ttl: ttl}
|
||||
}
|
||||
|
||||
func (s *AuthService) Login(ctx context.Context, username, password string) (*domain.Session, *domain.User, error) {
|
||||
u, err := s.users.GetByUsername(ctx, username)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
_, _ = hashPassword("dummy-password-for-timing")
|
||||
return nil, nil, ErrUnauthorized
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
hash, err := s.users.GetPasswordHash(ctx, u.ID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err := verifyPassword(password, hash); err != nil {
|
||||
if errors.Is(err, ErrPasswordMismatch) {
|
||||
return nil, nil, ErrUnauthorized
|
||||
}
|
||||
return nil, nil, fmt.Errorf("verify password: %w", err)
|
||||
}
|
||||
|
||||
token, err := generateToken()
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("generate token: %w", err)
|
||||
}
|
||||
|
||||
now := nowUTC()
|
||||
sess := &domain.Session{
|
||||
Token: token,
|
||||
UserID: u.ID,
|
||||
ExpiresAt: now.Add(s.ttl),
|
||||
CreatedAt: now,
|
||||
}
|
||||
if err := s.sessions.Create(ctx, sess); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return sess, u, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) ValidateSession(ctx context.Context, token string) (*domain.User, error) {
|
||||
sess, err := s.sessions.GetByToken(ctx, token)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
// Redis недоступен — возвращаем 401, чтобы клиент перелогинился,
|
||||
// либо 503, если хотим явно показать проблему зависимости.
|
||||
return nil, ErrUnauthorized // ← было: return nil, err
|
||||
}
|
||||
if nowUTC().After(sess.ExpiresAt) {
|
||||
_ = s.sessions.Delete(ctx, token)
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
u, err := s.users.GetByID(ctx, sess.UserID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) Logout(ctx context.Context, token string) error {
|
||||
return s.sessions.Delete(ctx, token)
|
||||
}
|
||||
|
||||
func generateToken() (string, error) {
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(b), nil
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
)
|
||||
|
||||
type ChatService struct {
|
||||
chats repository.ChatRepository
|
||||
users repository.UserRepository
|
||||
}
|
||||
|
||||
func NewChatService(chats repository.ChatRepository, users repository.UserRepository) *ChatService {
|
||||
return &ChatService{chats: chats, users: users}
|
||||
}
|
||||
|
||||
func (s *ChatService) Create(ctx context.Context, callerID, title string, memberIDs []string) (*domain.Chat, error) {
|
||||
if !validateTitle(title) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
if len(memberIDs) == 0 || len(memberIDs) > 100 {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
// Нормализация UUID: убираем разницу регистра.
|
||||
normalized := make([]string, 0, len(memberIDs))
|
||||
seen := make(map[string]struct{}, len(memberIDs))
|
||||
callerFound := false
|
||||
callerParsed, err := uuid.Parse(callerID)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
callerID = callerParsed.String()
|
||||
|
||||
for _, id := range memberIDs {
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
id = parsed.String()
|
||||
if _, dup := seen[id]; dup {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
normalized = append(normalized, id)
|
||||
if id == callerID {
|
||||
callerFound = true
|
||||
}
|
||||
}
|
||||
if !callerFound {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
for id := range seen {
|
||||
if _, err := s.users.GetByID(ctx, id); err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
c := &domain.Chat{
|
||||
ID: newID(),
|
||||
Title: title,
|
||||
MemberIDs: normalized,
|
||||
CreatedAt: nowUTC(),
|
||||
}
|
||||
if err := s.chats.Create(ctx, c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) Get(ctx context.Context, callerID, chatID string) (*domain.Chat, error) {
|
||||
parsed, err := uuid.Parse(chatID)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
chatID = parsed.String()
|
||||
|
||||
ok, err := s.chats.IsMember(ctx, chatID, callerID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
c, err := s.chats.GetByID(ctx, chatID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) List(ctx context.Context, callerID, cursor string, limit int) ([]*domain.Chat, string, error) {
|
||||
if limit < 1 || limit > 100 {
|
||||
return nil, "", ErrInvalidInput
|
||||
}
|
||||
|
||||
cursorTime := time.Unix(0, 0).UTC()
|
||||
cursorID := "00000000-0000-0000-0000-000000000000"
|
||||
if cursor != "" {
|
||||
t, id, err := decodeCursor(cursor, callerID)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
cursorTime, cursorID = t, id
|
||||
}
|
||||
|
||||
items, err := s.chats.ListByMember(ctx, callerID, cursorTime, cursorID, limit+1)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if items == nil {
|
||||
items = []*domain.Chat{}
|
||||
}
|
||||
|
||||
var next string
|
||||
if len(items) > limit {
|
||||
items = items[:limit]
|
||||
last := items[limit-1]
|
||||
next = encodeCursor(callerID, last.CreatedAt, last.ID)
|
||||
}
|
||||
return items, next, nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func encodeCursor(scope string, createdAt time.Time, id string) string {
|
||||
raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(raw))
|
||||
}
|
||||
|
||||
func decodeCursor(cursor, expectedScope string) (time.Time, string, error) {
|
||||
raw, err := base64.RawURLEncoding.DecodeString(cursor)
|
||||
if err != nil {
|
||||
return time.Time{}, "", ErrInvalidInput
|
||||
}
|
||||
parts := strings.SplitN(string(raw), "|", 3)
|
||||
if len(parts) != 3 {
|
||||
return time.Time{}, "", ErrInvalidInput
|
||||
}
|
||||
if _, err := uuid.Parse(parts[0]); err != nil {
|
||||
return time.Time{}, "", ErrInvalidInput
|
||||
}
|
||||
if parts[0] != expectedScope {
|
||||
return time.Time{}, "", ErrInvalidInput
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339Nano, parts[1])
|
||||
if err != nil {
|
||||
return time.Time{}, "", ErrInvalidInput
|
||||
}
|
||||
if _, err := uuid.Parse(parts[2]); err != nil {
|
||||
return time.Time{}, "", ErrInvalidInput
|
||||
}
|
||||
return t, parts[2], nil
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package service
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrInvalidInput = errors.New("invalid input")
|
||||
ErrUnauthorized = errors.New("unauthorized")
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrUnavailable = errors.New("unavailable")
|
||||
ErrPasswordMismatch = errors.New("password mismatch")
|
||||
ErrPayloadTooLarge = errors.New("payload too large")
|
||||
ErrUnsupportedMedia = errors.New("unsupported media type")
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func newID() string {
|
||||
var b [16]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
panic(fmt.Sprintf("crypto/rand failed: %v", err))
|
||||
}
|
||||
b[6] = (b[6] & 0x0f) | 0x40
|
||||
b[8] = (b[8] & 0x3f) | 0x80
|
||||
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16])
|
||||
}
|
||||
|
||||
// nowUTC возвращает время UTC с точностью до секунд.
|
||||
// PostgreSQL и Python-клиент тестов ожидают формат без наносекунд.
|
||||
func nowUTC() time.Time {
|
||||
return time.Now().UTC().Truncate(time.Second)
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
redisrepo "github.com/charawein/mtusi2026lab5/internal/repository/redis"
|
||||
)
|
||||
|
||||
type MessageService struct {
|
||||
messages repository.MessageRepository
|
||||
chats repository.ChatRepository
|
||||
cache *redisrepo.MessageCache
|
||||
idempotencyTTL time.Duration
|
||||
}
|
||||
|
||||
func NewMessageService(messages repository.MessageRepository, chats repository.ChatRepository, cache *redisrepo.MessageCache, idempotencyTTL time.Duration) *MessageService {
|
||||
return &MessageService{messages: messages, chats: chats, cache: cache, idempotencyTTL: idempotencyTTL}
|
||||
}
|
||||
|
||||
var idempotencyKeyRe = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
// requestHash — отпечаток семантики тела запроса. Хешируем уже разобранные поля,
|
||||
// а не сырые байты: порядок ключей JSON не важен, отсутствующий attachment_ids
|
||||
// равен [], порядок самих вложений значим.
|
||||
func requestHash(text string, attachmentIDs []string) string {
|
||||
if attachmentIDs == nil {
|
||||
attachmentIDs = []string{}
|
||||
}
|
||||
raw, _ := json.Marshal(struct {
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}{Text: text, AttachmentIDs: attachmentIDs})
|
||||
sum := sha256.Sum256(raw)
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// Send создаёт сообщение с обязательным Idempotency-Key. Повтор с тем же ключом
|
||||
// и телом возвращает исходное сообщение, с другим телом — ErrConflict.
|
||||
// Доступ к чату проверяется при каждом повторе.
|
||||
func (s *MessageService) Send(ctx context.Context, senderID, chatID, text string, attachmentIDs []string, idempotencyKey string) (*domain.Message, error) {
|
||||
if !idempotencyKeyRe.MatchString(idempotencyKey) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
parsed, err := uuid.Parse(chatID)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
chatID = parsed.String()
|
||||
|
||||
ids, ok := normalizeAttachmentIDs(attachmentIDs)
|
||||
if !ok {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
// С вложениями допустим пустой text; иначе — обычные правила текста.
|
||||
if !(len(ids) > 0 && text == "") && !validateText(text) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
ok, err = s.chats.IsMember(ctx, chatID, senderID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
m := &domain.Message{
|
||||
ID: newID(),
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: nowUTC(),
|
||||
|
||||
AttachmentIDs: ids,
|
||||
}
|
||||
stored, created, err := s.messages.CreateIdempotent(ctx, m, idempotencyKey, requestHash(text, ids), s.idempotencyTTL)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, repository.ErrConflict):
|
||||
return nil, ErrConflict
|
||||
case errors.Is(err, repository.ErrNotFound):
|
||||
return nil, ErrNotFound // чужое/неизвестное/незавершённое вложение
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if !created {
|
||||
return stored, nil
|
||||
}
|
||||
|
||||
// Версия чата уже увеличена в транзакции записи; здесь только режим scan.
|
||||
if s.cache != nil {
|
||||
s.cache.OnWrite(ctx, chatID)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (s *MessageService) List(ctx context.Context, callerID, chatID, cursor string, limit int) ([]*domain.Message, string, error) {
|
||||
parsed, err := uuid.Parse(chatID)
|
||||
if err != nil {
|
||||
return nil, "", ErrInvalidInput
|
||||
}
|
||||
chatID = parsed.String()
|
||||
|
||||
if limit < 1 || limit > 100 {
|
||||
return nil, "", ErrInvalidInput
|
||||
}
|
||||
|
||||
// Членство и версия истории — одним запросом к PostgreSQL. Версия читается
|
||||
// до истории, поэтому под ключом версии v лежат данные не старее v.
|
||||
version, ok, err := s.chats.MemberVersion(ctx, chatID, callerID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, "", ErrNotFound
|
||||
}
|
||||
return nil, "", err
|
||||
}
|
||||
if !ok {
|
||||
return nil, "", ErrNotFound
|
||||
}
|
||||
|
||||
if s.cache != nil {
|
||||
if items, next, hit := s.cache.Get(ctx, chatID, version, cursor, limit); hit {
|
||||
return items, next, nil
|
||||
}
|
||||
}
|
||||
|
||||
cursorTime := time.Unix(0, 0).UTC()
|
||||
cursorID := "00000000-0000-0000-0000-000000000000"
|
||||
if cursor != "" {
|
||||
t, id, err := decodeCursor(cursor, chatID)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
cursorTime, cursorID = t, id
|
||||
}
|
||||
|
||||
items, err := s.messages.ListByChat(ctx, chatID, cursorTime, cursorID, limit+1)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if items == nil {
|
||||
items = []*domain.Message{}
|
||||
}
|
||||
|
||||
var next string
|
||||
if len(items) > limit {
|
||||
items = items[:limit]
|
||||
last := items[limit-1]
|
||||
next = encodeCursor(chatID, last.CreatedAt, last.ID)
|
||||
}
|
||||
|
||||
if s.cache != nil {
|
||||
s.cache.Set(ctx, chatID, version, cursor, limit, items, next)
|
||||
}
|
||||
return items, next, nil
|
||||
}
|
||||
|
||||
// normalizeAttachmentIDs: 0–10 уникальных UUID; приводит к каноническому виду.
|
||||
func normalizeAttachmentIDs(in []string) ([]string, bool) {
|
||||
if len(in) > 10 {
|
||||
return nil, false
|
||||
}
|
||||
out := make([]string, 0, len(in))
|
||||
seen := make(map[string]bool, len(in))
|
||||
for _, raw := range in {
|
||||
u, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
id := u.String()
|
||||
if seen[id] {
|
||||
return nil, false
|
||||
}
|
||||
seen[id] = true
|
||||
out = append(out, id)
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
const (
|
||||
argonTime = 1
|
||||
argonMemory = 64 * 1024
|
||||
argonThreads = 4
|
||||
argonKeyLen = 32
|
||||
argonSaltLen = 16
|
||||
)
|
||||
|
||||
func hashPassword(password string) (string, error) {
|
||||
salt := make([]byte, argonSaltLen)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return "", fmt.Errorf("generate salt: %w", err)
|
||||
}
|
||||
hash := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen)
|
||||
return fmt.Sprintf(
|
||||
"$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version, argonMemory, argonTime, argonThreads,
|
||||
base64.RawStdEncoding.EncodeToString(salt),
|
||||
base64.RawStdEncoding.EncodeToString(hash),
|
||||
), nil
|
||||
}
|
||||
|
||||
func verifyPassword(password, encoded string) error {
|
||||
parts := strings.Split(encoded, "$")
|
||||
if len(parts) != 6 || parts[1] != "argon2id" {
|
||||
return fmt.Errorf("invalid hash format")
|
||||
}
|
||||
var version int
|
||||
if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil {
|
||||
return fmt.Errorf("parse version: %w", err)
|
||||
}
|
||||
if version != argon2.Version {
|
||||
return fmt.Errorf("incompatible version: %d", version)
|
||||
}
|
||||
var mem uint32
|
||||
var iters uint32
|
||||
var threads uint8
|
||||
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &mem, &iters, &threads); err != nil {
|
||||
return fmt.Errorf("parse params: %w", err)
|
||||
}
|
||||
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
|
||||
if err != nil {
|
||||
return fmt.Errorf("decode salt: %w", err)
|
||||
}
|
||||
want, err := base64.RawStdEncoding.DecodeString(parts[5])
|
||||
if err != nil {
|
||||
return fmt.Errorf("decode hash: %w", err)
|
||||
}
|
||||
got := argon2.IDKey([]byte(password), salt, iters, mem, threads, uint32(len(want)))
|
||||
if subtle.ConstantTimeCompare(got, want) != 1 {
|
||||
return ErrPasswordMismatch
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
users repository.UserRepository
|
||||
}
|
||||
|
||||
func NewUserService(users repository.UserRepository) *UserService {
|
||||
return &UserService{users: users}
|
||||
}
|
||||
|
||||
// Register создаёт нового пользователя. Возвращает ErrInvalidInput при невалидных
|
||||
// данных и ErrConflict, если username занят.
|
||||
func (s *UserService) Register(ctx context.Context, username, displayName, password string) (*domain.User, error) {
|
||||
if !validateUsername(username) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
if !validateDisplayName(displayName) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
if !validatePassword(password) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
|
||||
hash, err := hashPassword(password)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("hash password: %w", err)
|
||||
}
|
||||
|
||||
u := &domain.User{
|
||||
ID: newID(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: nowUTC(),
|
||||
}
|
||||
if err := s.users.Create(ctx, u, hash); err != nil {
|
||||
if errors.Is(err, repository.ErrConflict) {
|
||||
return nil, ErrConflict
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// GetByID возвращает пользователя. Возвращает ErrNotFound, если такого нет.
|
||||
// Используется middleware аутентификации для проверки X-User-Id.
|
||||
func (s *UserService) GetByID(ctx context.Context, id string) (*domain.User, error) {
|
||||
u, err := s.users.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var usernameRe = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
// validatePassword: 12–128 code points и не только пробельные символы
|
||||
// (обязательная строка по контракту не может состоять из одних пробелов).
|
||||
func validatePassword(s string) bool {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
return false
|
||||
}
|
||||
n := utf8.RuneCountInString(s)
|
||||
return n >= 12 && n <= 128
|
||||
}
|
||||
|
||||
// validateUsername проверяет формат username по README: строго [a-z0-9_]{3,32}.
|
||||
func validateUsername(s string) bool {
|
||||
return usernameRe.MatchString(s)
|
||||
}
|
||||
|
||||
// validateDisplayName проверяет display_name: 1–100 Unicode code points,
|
||||
// не состоит только из пробельных символов.
|
||||
func validateDisplayName(s string) bool {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
return false
|
||||
}
|
||||
n := utf8.RuneCountInString(s)
|
||||
return n >= 1 && n <= 100
|
||||
}
|
||||
|
||||
// validateTitle проверяет title чата: 1–100 code points, не только пробелы.
|
||||
func validateTitle(s string) bool {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
return false
|
||||
}
|
||||
n := utf8.RuneCountInString(s)
|
||||
return n >= 1 && n <= 100
|
||||
}
|
||||
|
||||
// validateText проверяет text сообщения: 1–4000 code points, не только пробелы.
|
||||
// В lab4 появится возможность отправить пустой text, если есть attachment_ids.
|
||||
func validateText(s string) bool {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
return false
|
||||
}
|
||||
n := utf8.RuneCountInString(s)
|
||||
return n >= 1 && n <= 4000
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidatePassword(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
in string
|
||||
want bool
|
||||
}{
|
||||
{"обычный", "correct-horse-12", true},
|
||||
{"ровно 12", strings.Repeat("a", 12), true},
|
||||
{"128 code points", strings.Repeat("я", 128), true},
|
||||
{"двоеточие допустимо", "pass:word:1234", true},
|
||||
{"пробелы внутри допустимы", " pass word ", true},
|
||||
{"короче 12", strings.Repeat("a", 11), false},
|
||||
{"длиннее 128", strings.Repeat("a", 129), false},
|
||||
{"12 пробелов", strings.Repeat(" ", 12), false},
|
||||
{"пробельные символы разных видов", strings.Repeat(" \t\n ", 4), false},
|
||||
{"пусто", "", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := validatePassword(c.in); got != c.want {
|
||||
t.Errorf("%s: validatePassword(%q) = %v, want %v", c.name, c.in, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateText(t *testing.T) {
|
||||
if validateText(strings.Repeat(" ", 5)) {
|
||||
t.Error("whitespace-only text must be rejected")
|
||||
}
|
||||
if !validateText("привет") {
|
||||
t.Error("normal text must be accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// Package storage — приватное S3-хранилище байтов вложений (MinIO).
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
// ErrNotFound — объекта нет в bucket.
|
||||
var ErrNotFound = errors.New("object not found")
|
||||
|
||||
// Object — элемент листинга.
|
||||
type Object struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
// Store — то, что нужно приложению и reconciler от объектного хранилища.
|
||||
type Store interface {
|
||||
Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error
|
||||
Get(ctx context.Context, key string) (io.ReadCloser, int64, error)
|
||||
Stat(ctx context.Context, key string) (Object, error)
|
||||
Delete(ctx context.Context, key string) error
|
||||
// List отдаёт объекты с префиксом в лексикографическом (байтовом) порядке ключей.
|
||||
List(ctx context.Context, prefix string, fn func(Object) error) error
|
||||
// Copy — серверное копирование объекта (байты не проходят через приложение).
|
||||
Copy(ctx context.Context, src, dst string) error
|
||||
}
|
||||
|
||||
type S3 struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewS3(endpoint, region, bucket, accessKey, secretKey string) (*S3, error) {
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil || u.Host == "" {
|
||||
return nil, fmt.Errorf("bad S3 endpoint %q", endpoint)
|
||||
}
|
||||
c, err := minio.New(u.Host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
|
||||
Secure: u.Scheme == "https",
|
||||
Region: region,
|
||||
// По умолчанию minio-go повторяет до 10 раз; при недоступном S3 клиент
|
||||
// должен быстро получить 503, а не ждать таймаута LB.
|
||||
MaxRetries: 2,
|
||||
BucketLookup: minio.BucketLookupPath,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 client: %w", err)
|
||||
}
|
||||
return &S3{client: c, bucket: bucket}, nil
|
||||
}
|
||||
|
||||
func isNotFound(err error) bool {
|
||||
code := minio.ToErrorResponse(err).Code
|
||||
return code == "NoSuchKey" || code == "NotFound"
|
||||
}
|
||||
|
||||
// Put пишет объект одним PUT, читая r потоково (size известен заранее).
|
||||
func (s *S3) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
DisableMultipart: true,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("s3 put: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S3) Get(ctx context.Context, key string) (io.ReadCloser, int64, error) {
|
||||
obj, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("s3 get: %w", err)
|
||||
}
|
||||
// GetObject ленивый: ошибки (нет объекта, S3 недоступен) видны на Stat.
|
||||
st, err := obj.Stat()
|
||||
if err != nil {
|
||||
obj.Close()
|
||||
if isNotFound(err) {
|
||||
return nil, 0, ErrNotFound
|
||||
}
|
||||
return nil, 0, fmt.Errorf("s3 stat: %w", err)
|
||||
}
|
||||
return obj, st.Size, nil
|
||||
}
|
||||
|
||||
func (s *S3) Stat(ctx context.Context, key string) (Object, error) {
|
||||
st, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
if isNotFound(err) {
|
||||
return Object{}, ErrNotFound
|
||||
}
|
||||
return Object{}, fmt.Errorf("s3 stat: %w", err)
|
||||
}
|
||||
return Object{Key: st.Key, Size: st.Size, LastModified: st.LastModified}, nil
|
||||
}
|
||||
|
||||
// Delete идемпотентен: удаление отсутствующего объекта не ошибка.
|
||||
func (s *S3) Delete(ctx context.Context, key string) error {
|
||||
if err := s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{}); err != nil && !isNotFound(err) {
|
||||
return fmt.Errorf("s3 delete: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S3) List(ctx context.Context, prefix string, fn func(Object) error) error {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
for o := range s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Prefix: prefix, Recursive: true}) {
|
||||
if o.Err != nil {
|
||||
return fmt.Errorf("s3 list: %w", o.Err)
|
||||
}
|
||||
if strings.HasSuffix(o.Key, "/") {
|
||||
continue
|
||||
}
|
||||
if err := fn(Object{Key: o.Key, Size: o.Size, LastModified: o.LastModified}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S3) Copy(ctx context.Context, src, dst string) error {
|
||||
_, err := s.client.CopyObject(ctx,
|
||||
minio.CopyDestOptions{Bucket: s.bucket, Object: dst},
|
||||
minio.CopySrcOptions{Bucket: s.bucket, Object: src})
|
||||
if err != nil {
|
||||
if isNotFound(err) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("s3 copy: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/service"
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
type AttachmentHandler struct {
|
||||
attachments *service.AttachmentService
|
||||
}
|
||||
|
||||
func NewAttachmentHandler(attachments *service.AttachmentService) *AttachmentHandler {
|
||||
return &AttachmentHandler{attachments: attachments}
|
||||
}
|
||||
|
||||
// Upload: POST /api/v1/chats/{chat_id}/attachments?filename=... с сырыми байтами PNG/JPEG.
|
||||
func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
||||
uid := httputil.UserIDFromContext(r.Context())
|
||||
if uid == "" {
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
if len(q["filename"]) != 1 {
|
||||
httputil.WriteError(w, r, service.ErrInvalidInput)
|
||||
return
|
||||
}
|
||||
// MaxBytesReader — второй рубеж: не дать прочитать больше лимита + 1 байт,
|
||||
// даже если Content-Length солгал.
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 10*1024*1024+1)
|
||||
a, err := h.attachments.Upload(r.Context(), uid, r.PathValue("chat_id"), q.Get("filename"),
|
||||
r.Header.Get("Content-Type"), r.Body, r.ContentLength)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
// 202: оригинал устойчиво принят, обработка асинхронна; готовность — по GET.
|
||||
httputil.WriteJSON(w, r, http.StatusAccepted, a)
|
||||
}
|
||||
|
||||
// Get: GET /api/v1/attachments/{attachment_id} — метаданные.
|
||||
func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
uid := httputil.UserIDFromContext(r.Context())
|
||||
a, err := h.attachments.Get(r.Context(), uid, r.PathValue("attachment_id"))
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
httputil.WriteJSON(w, r, http.StatusOK, a)
|
||||
}
|
||||
|
||||
// Content: GET /api/v1/attachments/{attachment_id}/content[?variant=original] — байты
|
||||
// через авторизованный API, без redirect на S3.
|
||||
func (h *AttachmentHandler) Content(w http.ResponseWriter, r *http.Request) {
|
||||
uid := httputil.UserIDFromContext(r.Context())
|
||||
variant := r.URL.Query().Get("variant")
|
||||
switch variant {
|
||||
case "", "original", "thumbnail", "crop", "optimized":
|
||||
default:
|
||||
httputil.WriteError(w, r, service.ErrInvalidInput)
|
||||
return
|
||||
}
|
||||
c, rc, err := h.attachments.Open(r.Context(), uid, r.PathValue("attachment_id"), variant)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
defer rc.Close()
|
||||
w.Header().Set("Content-Type", c.ContentType)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(c.Size, 10))
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if n, err := io.Copy(w, rc); err != nil {
|
||||
// Заголовок уже отправлен: клиент увидит обрыв по Content-Length.
|
||||
slog.Warn("attachment download interrupted", "attachment_id", r.PathValue("attachment_id"), "sent", n, "err", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/service"
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
auth *service.AuthService
|
||||
}
|
||||
|
||||
func NewAuthHandler(auth *service.AuthService) *AuthHandler {
|
||||
return &AuthHandler{auth: auth}
|
||||
}
|
||||
|
||||
type sessionResponse struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
User *domain.User `json:"user"`
|
||||
}
|
||||
|
||||
func (h *AuthHandler) CreateSession(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, ok := parseBasicAuth(r.Header.Get("Authorization"))
|
||||
if !ok {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
sess, u, err := h.auth.Login(r.Context(), username, password)
|
||||
if err != nil {
|
||||
if err == service.ErrUnauthorized {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
}
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Формат RFC3339 без микросекунд: "2026-10-09T19:00:36Z".
|
||||
// Python-клиент тестов ожидает 'Z' в конце строки.
|
||||
httputil.WriteJSON(w, r, http.StatusCreated, sessionResponse{
|
||||
Token: sess.Token,
|
||||
TokenType: "Bearer",
|
||||
ExpiresAt: sess.ExpiresAt.UTC().Truncate(time.Second).Format(time.RFC3339),
|
||||
User: u,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AuthHandler) RevokeSession(w http.ResponseWriter, r *http.Request) {
|
||||
token := bearerToken(r)
|
||||
if token == "" {
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`)
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
if err := h.auth.Logout(r.Context(), token); err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func parseBasicAuth(h string) (string, string, bool) {
|
||||
const prefix = "Basic "
|
||||
if !strings.HasPrefix(h, prefix) {
|
||||
return "", "", false
|
||||
}
|
||||
raw, err := base64.StdEncoding.DecodeString(h[len(prefix):])
|
||||
if err != nil {
|
||||
return "", "", false
|
||||
}
|
||||
parts := strings.SplitN(string(raw), ":", 2)
|
||||
if len(parts) != 2 {
|
||||
return "", "", false
|
||||
}
|
||||
return parts[0], parts[1], true
|
||||
}
|
||||
|
||||
func bearerToken(r *http.Request) string {
|
||||
h := r.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(h, "Bearer ") {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimPrefix(h, "Bearer ")
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/service"
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
type ChatHandler struct {
|
||||
chats *service.ChatService
|
||||
}
|
||||
|
||||
func NewChatHandler(chats *service.ChatService) *ChatHandler {
|
||||
return &ChatHandler{chats: chats}
|
||||
}
|
||||
|
||||
func (h *ChatHandler) CreateChat(w http.ResponseWriter, r *http.Request) {
|
||||
uid := httputil.UserIDFromContext(r.Context())
|
||||
if uid == "" {
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
if err := httputil.DecodeJSON(r, &body); err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
c, err := h.chats.Create(r.Context(), uid, body.Title, body.MemberIDs)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
httputil.WriteJSON(w, r, http.StatusCreated, c)
|
||||
}
|
||||
|
||||
func (h *ChatHandler) ListChats(w http.ResponseWriter, r *http.Request) {
|
||||
uid := httputil.UserIDFromContext(r.Context())
|
||||
if uid == "" {
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
limit, err := httputil.ParseLimit(r)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
cursor := r.URL.Query().Get("cursor")
|
||||
|
||||
items, next, err := h.chats.List(r.Context(), uid, cursor, limit)
|
||||
if err != nil {
|
||||
// ВРЕМЕННО: логируем точную ошибку
|
||||
slog.Error("ListChats failed", "err", err, "uid", uid, "cursor", cursor, "limit", limit)
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
httputil.WriteJSON(w, r, http.StatusOK, httputil.PageResponse{Items: items, NextCursor: httputil.NextOrNull(next)})
|
||||
}
|
||||
|
||||
func (h *ChatHandler) GetChat(w http.ResponseWriter, r *http.Request) {
|
||||
uid := httputil.UserIDFromContext(r.Context())
|
||||
if uid == "" {
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
chatID := r.PathValue("chat_id")
|
||||
if chatID == "" {
|
||||
httputil.WriteError(w, r, service.ErrInvalidInput)
|
||||
return
|
||||
}
|
||||
|
||||
c, err := h.chats.Get(r.Context(), uid, chatID)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
httputil.WriteJSON(w, r, http.StatusOK, c)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/service"
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
type HealthHandler struct {
|
||||
pool *pgxpool.Pool
|
||||
redis *redis.Client
|
||||
draining *atomic.Bool
|
||||
}
|
||||
|
||||
func NewHealthHandler(pool *pgxpool.Pool, redisClient *redis.Client, draining *atomic.Bool) *HealthHandler {
|
||||
return &HealthHandler{pool: pool, redis: redisClient, draining: draining}
|
||||
}
|
||||
|
||||
// Live проверяет только жизнь процесса.
|
||||
func (h *HealthHandler) Live(w http.ResponseWriter, r *http.Request) {
|
||||
httputil.WriteJSON(w, r, http.StatusOK, healthResponse{Status: "ok"})
|
||||
}
|
||||
|
||||
// Ready проверяет доступность PostgreSQL и Redis.
|
||||
// После SIGTERM возвращает 503, чтобы LB исключил реплику до закрытия listener.
|
||||
func (h *HealthHandler) Ready(w http.ResponseWriter, r *http.Request) {
|
||||
if h.draining.Load() {
|
||||
httputil.WriteError(w, r, service.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := h.pool.Ping(ctx); err != nil {
|
||||
httputil.WriteError(w, r, service.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
if err := h.redis.Ping(ctx).Err(); err != nil {
|
||||
httputil.WriteError(w, r, service.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
httputil.WriteJSON(w, r, http.StatusOK, healthResponse{Status: "ok"})
|
||||
}
|
||||
|
||||
type healthResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"runtime"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/config"
|
||||
)
|
||||
|
||||
// NewInternalStatsHandler отдаёт счётчики пула PostgreSQL и рантайма Go.
|
||||
// Нужен для поиска точки насыщения пула в экспериментах трека 1:
|
||||
// рост empty_acquire_count/acquire_duration означает ожидание свободного соединения.
|
||||
func NewInternalStatsHandler(pool *pgxpool.Pool, cfg config.Config) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /internal/stats", func(w http.ResponseWriter, r *http.Request) {
|
||||
st := pool.Stat()
|
||||
body := map[string]any{
|
||||
"instance_id": cfg.InstanceID,
|
||||
"cache_invalidation": cfg.CacheInvalidation,
|
||||
"pool": map[string]any{
|
||||
"max_conns": st.MaxConns(),
|
||||
"total_conns": st.TotalConns(),
|
||||
"acquired_conns": st.AcquiredConns(),
|
||||
"idle_conns": st.IdleConns(),
|
||||
"acquire_count": st.AcquireCount(),
|
||||
"empty_acquire_count": st.EmptyAcquireCount(),
|
||||
"canceled_acquire_count": st.CanceledAcquireCount(),
|
||||
"acquire_duration_ms": st.AcquireDuration().Milliseconds(),
|
||||
},
|
||||
"goroutines": runtime.NumGoroutine(),
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(body)
|
||||
})
|
||||
return mux
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/service"
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
type MessageHandler struct {
|
||||
messages *service.MessageService
|
||||
}
|
||||
|
||||
func NewMessageHandler(messages *service.MessageService) *MessageHandler {
|
||||
return &MessageHandler{messages: messages}
|
||||
}
|
||||
|
||||
// attachmentIDs — поле контракта, которое обязано быть JSON-массивом строк.
|
||||
// null недопустим (в отличие от отсутствия поля — его трактуем как пустой
|
||||
// список и пробрасываем дальше как nil-срез).
|
||||
type attachmentIDs struct {
|
||||
present bool
|
||||
value []string
|
||||
}
|
||||
|
||||
var errAttachmentIDsNull = errors.New("attachment_ids must be an array, not null")
|
||||
|
||||
func (a *attachmentIDs) UnmarshalJSON(b []byte) error {
|
||||
a.present = true
|
||||
if string(b) == "null" {
|
||||
return errAttachmentIDsNull
|
||||
}
|
||||
var v []string
|
||||
if err := json.Unmarshal(b, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
a.value = v
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *MessageHandler) SendMessage(w http.ResponseWriter, r *http.Request) {
|
||||
uid := httputil.UserIDFromContext(r.Context())
|
||||
if uid == "" {
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
chatID := r.PathValue("chat_id")
|
||||
if chatID == "" {
|
||||
httputil.WriteError(w, r, service.ErrInvalidInput)
|
||||
return
|
||||
}
|
||||
|
||||
var body struct {
|
||||
Text *string `json:"text"`
|
||||
AttachmentIDs attachmentIDs `json:"attachment_ids"`
|
||||
}
|
||||
if err := httputil.DecodeJSON(r, &body); err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
if body.Text == nil { // text обязателен по контракту, даже пустой
|
||||
httputil.WriteError(w, r, service.ErrInvalidInput)
|
||||
return
|
||||
}
|
||||
// null в attachment_ids — нарушение контракта; отсутствие поля считаем
|
||||
// пустым списком.
|
||||
if body.AttachmentIDs.present && body.AttachmentIDs.value == nil {
|
||||
// present == true и value == nil возможно только если был null —
|
||||
// но UnmarshalJSON уже вернул ошибку. Оставляем защиту на будущее.
|
||||
httputil.WriteError(w, r, service.ErrInvalidInput)
|
||||
return
|
||||
}
|
||||
|
||||
// Повтор с тем же ключом возвращает тот же 201 и то же сообщение.
|
||||
key := r.Header.Get("Idempotency-Key")
|
||||
m, err := h.messages.Send(r.Context(), uid, chatID, *body.Text, body.AttachmentIDs.value, key)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
httputil.WriteJSON(w, r, http.StatusCreated, m)
|
||||
}
|
||||
|
||||
func (h *MessageHandler) ListMessages(w http.ResponseWriter, r *http.Request) {
|
||||
uid := httputil.UserIDFromContext(r.Context())
|
||||
if uid == "" {
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
chatID := r.PathValue("chat_id")
|
||||
if chatID == "" {
|
||||
httputil.WriteError(w, r, service.ErrInvalidInput)
|
||||
return
|
||||
}
|
||||
|
||||
limit, err := httputil.ParseLimit(r)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
cursor := r.URL.Query().Get("cursor")
|
||||
|
||||
items, next, err := h.messages.List(r.Context(), uid, chatID, cursor, limit)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
httputil.WriteJSON(w, r, http.StatusOK, httputil.PageResponse{Items: items, NextCursor: httputil.NextOrNull(next)})
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/service"
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
// Auth проверяет Authorization: Bearer <token> через AuthService.
|
||||
func Auth(auth *service.AuthService) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
header := r.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(header, "Bearer ") {
|
||||
writeUnauthorized(w, r)
|
||||
return
|
||||
}
|
||||
token := strings.TrimPrefix(header, "Bearer ")
|
||||
if token == "" {
|
||||
writeUnauthorized(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
u, err := auth.ValidateSession(r.Context(), token)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrUnauthorized) {
|
||||
writeUnauthorized(w, r)
|
||||
return
|
||||
}
|
||||
slog.Error("auth: unexpected", "err", err)
|
||||
httputil.WriteError(w, r, nil)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := httputil.ContextWithUserID(r.Context(), u.ID)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func writeUnauthorized(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`)
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
// InstanceID добавляет X-Instance-Id ко всем ответам API, включая ошибки и health.
|
||||
// Значение задаётся один раз при старте процесса и не меняется до его завершения.
|
||||
func InstanceID(id string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Instance-Id", id)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
type statusRecorder struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func (r *statusRecorder) WriteHeader(code int) {
|
||||
r.status = code
|
||||
r.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func Logging(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
rec := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
|
||||
next.ServeHTTP(rec, r)
|
||||
slog.Info("http request",
|
||||
"request_id", httputil.RequestIDFrom(r.Context()),
|
||||
"method", r.Method,
|
||||
"path", r.URL.Path,
|
||||
"status", rec.status,
|
||||
"duration_ms", time.Since(start).Milliseconds(),
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
func Recovery(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
slog.Error("panic in handler",
|
||||
"panic", rec,
|
||||
"request_id", httputil.RequestIDFrom(r.Context()),
|
||||
"method", r.Method,
|
||||
"path", r.URL.Path,
|
||||
)
|
||||
httputil.WriteError(w, r, nil)
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
func RequestID(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.Header.Get("X-Request-Id")
|
||||
if id == "" {
|
||||
id = newRequestID()
|
||||
}
|
||||
w.Header().Set("X-Request-Id", id)
|
||||
ctx := httputil.ContextWithRequestID(r.Context(), id)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func newRequestID() string {
|
||||
var b [12]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
panic(fmt.Sprintf("crypto/rand failed: %v", err))
|
||||
}
|
||||
return fmt.Sprintf("%x", b[:])
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Timeout ограничивает время выполнения запроса.
|
||||
func Timeout(d time.Duration) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), d)
|
||||
defer cancel()
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/service"
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/http/middleware"
|
||||
)
|
||||
|
||||
func NewRouter(
|
||||
users *service.UserService,
|
||||
chats *service.ChatService,
|
||||
messages *service.MessageService,
|
||||
attachments *service.AttachmentService,
|
||||
auth *service.AuthService,
|
||||
pool *pgxpool.Pool,
|
||||
redisClient *redis.Client,
|
||||
instanceID string,
|
||||
draining *atomic.Bool,
|
||||
) http.Handler {
|
||||
userH := NewUserHandler(users)
|
||||
chatH := NewChatHandler(chats)
|
||||
msgH := NewMessageHandler(messages)
|
||||
authH := NewAuthHandler(auth)
|
||||
attH := NewAttachmentHandler(attachments)
|
||||
healthH := NewHealthHandler(pool, redisClient, draining)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// Таймаут на запрос: обычный API — 5 s, передача файлов — 30 s.
|
||||
short := middleware.Timeout(5 * time.Second)
|
||||
long := middleware.Timeout(30 * time.Second)
|
||||
authMW := func(h http.Handler) http.Handler { return short(middleware.Auth(auth)(h)) }
|
||||
fileMW := func(h http.Handler) http.Handler { return long(middleware.Auth(auth)(h)) }
|
||||
|
||||
mux.Handle("GET /health/live", short(http.HandlerFunc(healthH.Live)))
|
||||
mux.Handle("GET /health/ready", short(http.HandlerFunc(healthH.Ready)))
|
||||
mux.Handle("POST /api/v1/users", short(http.HandlerFunc(userH.CreateUser)))
|
||||
mux.Handle("POST /api/v1/auth/sessions", short(http.HandlerFunc(authH.CreateSession)))
|
||||
|
||||
mux.Handle("DELETE /api/v1/auth/sessions/current", authMW(http.HandlerFunc(authH.RevokeSession)))
|
||||
mux.Handle("GET /api/v1/users/me", authMW(http.HandlerFunc(userH.CurrentUser)))
|
||||
mux.Handle("POST /api/v1/chats", authMW(http.HandlerFunc(chatH.CreateChat)))
|
||||
mux.Handle("GET /api/v1/chats", authMW(http.HandlerFunc(chatH.ListChats)))
|
||||
mux.Handle("GET /api/v1/chats/{chat_id}", authMW(http.HandlerFunc(chatH.GetChat)))
|
||||
mux.Handle("POST /api/v1/chats/{chat_id}/messages", authMW(http.HandlerFunc(msgH.SendMessage)))
|
||||
mux.Handle("GET /api/v1/chats/{chat_id}/messages", authMW(http.HandlerFunc(msgH.ListMessages)))
|
||||
mux.Handle("POST /api/v1/chats/{chat_id}/attachments", fileMW(http.HandlerFunc(attH.Upload)))
|
||||
mux.Handle("GET /api/v1/attachments/{attachment_id}", authMW(http.HandlerFunc(attH.Get)))
|
||||
mux.Handle("GET /api/v1/attachments/{attachment_id}/content", fileMW(http.HandlerFunc(attH.Content)))
|
||||
|
||||
handler := middleware.Logging(mux)
|
||||
handler = middleware.Recovery(handler)
|
||||
handler = middleware.RequestID(handler)
|
||||
handler = middleware.InstanceID(instanceID)(handler)
|
||||
return handler
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/service"
|
||||
"github.com/charawein/mtusi2026lab5/internal/transport/httputil"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
users *service.UserService
|
||||
}
|
||||
|
||||
func NewUserHandler(users *service.UserService) *UserHandler {
|
||||
return &UserHandler{users: users}
|
||||
}
|
||||
|
||||
func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
var body struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if err := httputil.DecodeJSON(r, &body); err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
u, err := h.users.Register(r.Context(), body.Username, body.DisplayName, body.Password)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
httputil.WriteJSON(w, r, http.StatusCreated, u)
|
||||
}
|
||||
|
||||
func (h *UserHandler) CurrentUser(w http.ResponseWriter, r *http.Request) {
|
||||
uid := httputil.UserIDFromContext(r.Context())
|
||||
if uid == "" {
|
||||
httputil.WriteError(w, r, service.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
u, err := h.users.GetByID(r.Context(), uid)
|
||||
if err != nil {
|
||||
httputil.WriteError(w, r, err)
|
||||
return
|
||||
}
|
||||
httputil.WriteJSON(w, r, http.StatusOK, u)
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package httputil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/service"
|
||||
)
|
||||
|
||||
// requestIDKey — ключ контекста для X-Request-Id.
|
||||
type requestIDKey struct{}
|
||||
|
||||
// ContextWithRequestID кладёт request_id в контекст. Используется middleware.RequestID.
|
||||
func ContextWithRequestID(ctx context.Context, id string) context.Context {
|
||||
return context.WithValue(ctx, requestIDKey{}, id)
|
||||
}
|
||||
|
||||
// RequestIDFrom извлекает request_id из контекста.
|
||||
func RequestIDFrom(ctx context.Context) string {
|
||||
if v, ok := ctx.Value(requestIDKey{}).(string); ok {
|
||||
return v
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// userIDKey — ключ контекста для аутентифицированного user_id.
|
||||
type userIDKey struct{}
|
||||
|
||||
// ContextWithUserID кладёт user_id в контекст. Используется middleware.Auth.
|
||||
func ContextWithUserID(ctx context.Context, id string) context.Context {
|
||||
return context.WithValue(ctx, userIDKey{}, id)
|
||||
}
|
||||
|
||||
// UserIDFromContext извлекает user_id из контекста.
|
||||
func UserIDFromContext(ctx context.Context) string {
|
||||
if v, ok := ctx.Value(userIDKey{}).(string); ok {
|
||||
return v
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// errorBody — формат тела ошибки по contracts/openapi.json.
|
||||
type errorBody struct {
|
||||
Error errorDetail `json:"error"`
|
||||
}
|
||||
|
||||
type errorDetail struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
|
||||
// WriteJSON пишет JSON-ответ с заданным статусом.
|
||||
func WriteJSON(w http.ResponseWriter, r *http.Request, status int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
if v == nil {
|
||||
return
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(v); err != nil {
|
||||
slog.Error("failed to encode response", "err", err, "request_id", RequestIDFrom(r.Context()))
|
||||
}
|
||||
}
|
||||
|
||||
// WriteError мапит доменную ошибку в HTTP-статус и пишет тело в формате контракта.
|
||||
func WriteError(w http.ResponseWriter, r *http.Request, err error) {
|
||||
status, code := mapError(err)
|
||||
reqID := RequestIDFrom(r.Context())
|
||||
if reqID == "" {
|
||||
reqID = "missing-request-id"
|
||||
}
|
||||
body := errorBody{Error: errorDetail{
|
||||
Code: code,
|
||||
Message: messageFor(code),
|
||||
RequestID: reqID,
|
||||
}}
|
||||
WriteJSON(w, r, status, body)
|
||||
}
|
||||
|
||||
func mapError(err error) (int, string) {
|
||||
switch {
|
||||
case errors.Is(err, service.ErrInvalidInput):
|
||||
return http.StatusBadRequest, "invalid_request"
|
||||
case errors.Is(err, service.ErrUnauthorized):
|
||||
return http.StatusUnauthorized, "unauthorized"
|
||||
case errors.Is(err, service.ErrNotFound):
|
||||
return http.StatusNotFound, "not_found"
|
||||
case errors.Is(err, service.ErrConflict):
|
||||
return http.StatusConflict, "conflict"
|
||||
case errors.Is(err, service.ErrUnavailable):
|
||||
return http.StatusServiceUnavailable, "unavailable"
|
||||
case errors.Is(err, service.ErrPayloadTooLarge):
|
||||
return http.StatusRequestEntityTooLarge, "payload_too_large"
|
||||
case errors.Is(err, service.ErrUnsupportedMedia):
|
||||
return http.StatusUnsupportedMediaType, "unsupported_media_type"
|
||||
default:
|
||||
return http.StatusInternalServerError, "internal_error"
|
||||
}
|
||||
}
|
||||
|
||||
func messageFor(code string) string {
|
||||
switch code {
|
||||
case "invalid_request":
|
||||
return "Неверный запрос"
|
||||
case "unauthorized":
|
||||
return "Требуется аутентификация"
|
||||
case "not_found":
|
||||
return "Объект не найден"
|
||||
case "conflict":
|
||||
return "Конфликт"
|
||||
case "internal_error":
|
||||
return "Внутренняя ошибка"
|
||||
case "unavailable":
|
||||
return "Сервис недоступен"
|
||||
case "payload_too_large":
|
||||
return "Слишком большой файл или изображение"
|
||||
case "unsupported_media_type":
|
||||
return "Поддерживаются только корректные PNG и JPEG"
|
||||
default:
|
||||
return "Ошибка"
|
||||
}
|
||||
}
|
||||
|
||||
// DecodeJSON парсит тело запроса в v, строго запрещая неизвестные поля.
|
||||
func DecodeJSON(r *http.Request, v any) error {
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(v); err != nil {
|
||||
return service.ErrInvalidInput
|
||||
}
|
||||
if dec.More() {
|
||||
return service.ErrInvalidInput
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseLimit читает query-параметр limit с дефолтом и границами по контракту.
|
||||
func ParseLimit(r *http.Request) (int, error) {
|
||||
s := r.URL.Query().Get("limit")
|
||||
if s == "" {
|
||||
return 50, nil
|
||||
}
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil || n < 1 || n > 100 {
|
||||
return 0, service.ErrInvalidInput
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// PageResponse — универсальный ответ пагинации.
|
||||
type PageResponse struct {
|
||||
Items any `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// NextOrNull превращает пустую строку в nil (для сериализации в null).
|
||||
func NextOrNull(s string) *string {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
return &s
|
||||
}
|
||||
@@ -0,0 +1,581 @@
|
||||
package worker_test
|
||||
|
||||
// Интеграционный fault-injection (трек 5): настоящий PostgreSQL со всеми
|
||||
// миграциями, S3 — в памяти, broker — вызовы Handle с телом из outbox (так же,
|
||||
// как его опубликовал бы relay). Запуск:
|
||||
//
|
||||
// TEST_DATABASE_URL=postgres://user@host:5432/postgres go test ./internal/worker/ -run Pipeline -v
|
||||
//
|
||||
// Для каждого сценария создаётся отдельная база, после каждого шага проверяются
|
||||
// инварианты (checkInvariants).
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository/postgres"
|
||||
"github.com/charawein/mtusi2026lab5/internal/storage"
|
||||
"github.com/charawein/mtusi2026lab5/internal/worker"
|
||||
)
|
||||
|
||||
// ---------- in-memory S3 ----------
|
||||
|
||||
type memStore struct {
|
||||
mu sync.Mutex
|
||||
objs map[string][]byte
|
||||
puts map[string]int
|
||||
failPut int // сколько ближайших Put завершить временной ошибкой
|
||||
}
|
||||
|
||||
func newMemStore() *memStore { return &memStore{objs: map[string][]byte{}, puts: map[string]int{}} }
|
||||
|
||||
func (m *memStore) Put(_ context.Context, key string, r io.Reader, size int64, _ string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.failPut > 0 {
|
||||
m.failPut--
|
||||
return errors.New("s3 put: 503 slow down")
|
||||
}
|
||||
b, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.objs[key] = b
|
||||
m.puts[key]++
|
||||
return nil
|
||||
}
|
||||
func (m *memStore) Get(_ context.Context, key string) (io.ReadCloser, int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
b, ok := m.objs[key]
|
||||
if !ok {
|
||||
return nil, 0, storage.ErrNotFound
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(b)), int64(len(b)), nil
|
||||
}
|
||||
func (m *memStore) Stat(_ context.Context, key string) (storage.Object, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
b, ok := m.objs[key]
|
||||
if !ok {
|
||||
return storage.Object{}, storage.ErrNotFound
|
||||
}
|
||||
return storage.Object{Key: key, Size: int64(len(b))}, nil
|
||||
}
|
||||
func (m *memStore) Delete(_ context.Context, key string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
delete(m.objs, key)
|
||||
return nil
|
||||
}
|
||||
func (m *memStore) List(context.Context, string, func(storage.Object) error) error { return nil }
|
||||
func (m *memStore) Copy(_ context.Context, src, dst string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
b, ok := m.objs[src]
|
||||
if !ok {
|
||||
return storage.ErrNotFound
|
||||
}
|
||||
m.objs[dst] = bytes.Clone(b)
|
||||
m.puts[dst]++
|
||||
return nil
|
||||
}
|
||||
func (m *memStore) get(key string) ([]byte, bool) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
b, ok := m.objs[key]
|
||||
return b, ok
|
||||
}
|
||||
|
||||
// Детерминированный «WebP» для тестов: RIFF/WEBP-заголовок + хэш пикселей.
|
||||
func fakeWebP(_ context.Context, img image.Image, q int) ([]byte, error) {
|
||||
h := sha256.New()
|
||||
b := img.Bounds()
|
||||
fmt.Fprintf(h, "%d:%d:%d", b.Dx(), b.Dy(), q)
|
||||
for y := b.Min.Y; y < b.Max.Y; y += 7 {
|
||||
for x := b.Min.X; x < b.Max.X; x += 7 {
|
||||
r, g, bb, _ := img.At(x, y).RGBA()
|
||||
fmt.Fprintf(h, "%d%d%d", r, g, bb)
|
||||
}
|
||||
}
|
||||
return append([]byte("RIFF\x00\x00\x00\x00WEBP"), h.Sum(nil)...), nil
|
||||
}
|
||||
|
||||
// ---------- окружение ----------
|
||||
|
||||
type env struct {
|
||||
t *testing.T
|
||||
ctx context.Context
|
||||
pool *pgxpool.Pool
|
||||
repo *postgres.PipelineRepository
|
||||
att *postgres.AttachmentRepository
|
||||
store *memStore
|
||||
chat string
|
||||
owner string
|
||||
}
|
||||
|
||||
type crash struct{ code int }
|
||||
|
||||
func setup(t *testing.T) *env {
|
||||
t.Helper()
|
||||
admin := os.Getenv("TEST_DATABASE_URL")
|
||||
if admin == "" {
|
||||
t.Skip("TEST_DATABASE_URL not set (integration test needs PostgreSQL)")
|
||||
}
|
||||
ctx := context.Background()
|
||||
conn, err := pgx.Connect(ctx, admin)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db := "lab5_it_" + strings.ReplaceAll(uuid.NewString()[:8], "-", "")
|
||||
if _, err := conn.Exec(ctx, "CREATE DATABASE "+db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_, _ = conn.Exec(context.Background(), "DROP DATABASE IF EXISTS "+db+" WITH (FORCE)")
|
||||
conn.Close(context.Background())
|
||||
})
|
||||
cfg, err := pgxpool.ParseConfig(admin)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ConnConfig.Database = db
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
files, _ := filepath.Glob("../../migrations/*.up.sql")
|
||||
sort.Strings(files)
|
||||
for _, f := range files {
|
||||
sql, _ := os.ReadFile(f)
|
||||
if _, err := pool.Exec(ctx, string(sql)); err != nil {
|
||||
t.Fatalf("%s: %v", f, err)
|
||||
}
|
||||
}
|
||||
e := &env{t: t, ctx: ctx, pool: pool, repo: postgres.NewPipelineRepository(pool),
|
||||
att: postgres.NewAttachmentRepository(pool), store: newMemStore(), owner: uuid.NewString()}
|
||||
e.chat = uuid.NewString()
|
||||
e.exec(`INSERT INTO users (id, username, display_name, password_hash) VALUES ($1::uuid, $2, 'U', 'x')`, e.owner, "u_"+e.owner[:8])
|
||||
e.exec(`INSERT INTO chats (id, title) VALUES ($1, 'c')`, e.chat)
|
||||
e.exec(`INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)`, e.chat, e.owner)
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *env) exec(sql string, args ...any) {
|
||||
e.t.Helper()
|
||||
if _, err := e.pool.Exec(e.ctx, sql, args...); err != nil {
|
||||
e.t.Fatalf("%s: %v", sql, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *env) scalar(sql string, args ...any) string {
|
||||
e.t.Helper()
|
||||
var s string
|
||||
if err := e.pool.QueryRow(e.ctx, sql, args...).Scan(&s); err != nil {
|
||||
e.t.Fatalf("%s: %v", sql, err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func pngBytes(w, h int, seed uint8) []byte {
|
||||
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
img.Set(x, y, color.RGBA{uint8(x) + seed, uint8(y), seed, 255})
|
||||
}
|
||||
}
|
||||
var b bytes.Buffer
|
||||
_ = png.Encode(&b, img)
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
// upload — как API: намерение, staging-объект, MarkQueued (jobs + outbox).
|
||||
func (e *env) upload(data []byte) *domain.Attachment {
|
||||
e.t.Helper()
|
||||
id := uuid.NewString()
|
||||
sum := sha256.Sum256(data)
|
||||
a := &domain.Attachment{ID: id, ChatID: e.chat, OwnerID: e.owner, Filename: "f.png",
|
||||
ObjectKey: "s/" + e.chat + "/" + id, CreatedAt: time.Now(), UpdatedAt: time.Now(),
|
||||
ContentType: "image/png", SizeBytes: int64(len(data)), SHA256: hex.EncodeToString(sum[:])}
|
||||
if err := e.att.CreateIntent(e.ctx, a); err != nil {
|
||||
e.t.Fatal(err)
|
||||
}
|
||||
_ = e.store.Put(e.ctx, a.ObjectKey, bytes.NewReader(data), int64(len(data)), "image/png")
|
||||
if _, err := e.att.MarkQueued(e.ctx, a, 1, 3); err != nil {
|
||||
e.t.Fatal(err)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// relay — опубликовать созревшие сообщения outbox; вернуть их тела.
|
||||
func (e *env) relay(afterPublish func()) [][]byte {
|
||||
e.t.Helper()
|
||||
var out [][]byte
|
||||
_, err := e.repo.PublishDue(e.ctx, 100, func(_ context.Context, m postgres.OutboxMessage) error {
|
||||
out = append(out, m.Payload)
|
||||
return nil
|
||||
}, afterPublish)
|
||||
if err != nil {
|
||||
e.t.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (e *env) worker(lease time.Duration, faults ...string) *worker.Worker {
|
||||
pts := map[string]bool{}
|
||||
for _, f := range faults {
|
||||
pts[f] = true
|
||||
}
|
||||
w := worker.New(e.repo, e.store, fakeWebP, worker.Config{
|
||||
Owner: "w-" + uuid.NewString()[:6], Lease: lease, JobTimeout: lease,
|
||||
MaxBytes: 10 << 20, MaxPixels: 40_000_000, PixelBudget: 60_000_000, WebPQuality: 80,
|
||||
RetryBase: 200 * time.Millisecond, RetryMax: 400 * time.Millisecond,
|
||||
FaultPoints: pts, FaultDir: e.t.TempDir(), DeleteStaging: true,
|
||||
})
|
||||
worker.SetExit(w, func(code int) { panic(crash{code}) })
|
||||
return w
|
||||
}
|
||||
|
||||
// handle — доставка с перехватом «падения процесса».
|
||||
func handle(w *worker.Worker, body []byte) (d worker.Decision, crashed bool) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if _, ok := r.(crash); !ok {
|
||||
panic(r)
|
||||
}
|
||||
crashed = true
|
||||
}
|
||||
}()
|
||||
return w.Handle(context.Background(), body), false
|
||||
}
|
||||
|
||||
type state struct {
|
||||
Status, ErrorCode, JobState string
|
||||
Version, Attempt, Variants int
|
||||
}
|
||||
|
||||
func (e *env) state(a *domain.Attachment) state {
|
||||
var s state
|
||||
err := e.pool.QueryRow(e.ctx, `
|
||||
SELECT a.status, COALESCE(a.error_code, ''), a.pipeline_version,
|
||||
(SELECT count(*) FROM attachment_variants v WHERE v.attachment_id = a.id),
|
||||
COALESCE((SELECT state FROM jobs j WHERE j.attachment_id = a.id ORDER BY pipeline_version DESC LIMIT 1), ''),
|
||||
COALESCE((SELECT attempt FROM jobs j WHERE j.attachment_id = a.id ORDER BY pipeline_version DESC LIMIT 1), 0)
|
||||
FROM attachments a WHERE a.id = $1`, a.ID).
|
||||
Scan(&s.Status, &s.ErrorCode, &s.Version, &s.Variants, &s.JobState, &s.Attempt)
|
||||
if err != nil {
|
||||
e.t.Fatal(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// checkInvariants — инварианты pipeline после любого шага:
|
||||
//
|
||||
// I1 не больше одного варианта на (attachment, version, name) — PK, проверяем явно;
|
||||
// I2 ready ⇒ опубликованная версия ≥ 1, варианты этой версии полные (3) и
|
||||
// объекты в S3 совпадают с метаданными (размер и SHA-256);
|
||||
// I3 ready ⇒ оригинал в конечном prefix, байты равны принятым (SHA-256 из 202);
|
||||
// I4 нет processing с истёкшим lease после sweeper (проверяется в сценариях);
|
||||
// I5 опубликованная версия никогда не уменьшается (проверяется по истории в сценариях).
|
||||
func (e *env) checkInvariants(a *domain.Attachment) {
|
||||
e.t.Helper()
|
||||
if dup := e.scalar(`SELECT count(*) FROM (SELECT 1 FROM attachment_variants WHERE attachment_id = $1
|
||||
GROUP BY pipeline_version, name HAVING count(*) > 1) d`, a.ID); dup != "0" {
|
||||
e.t.Fatalf("I1: duplicated variants: %s", dup)
|
||||
}
|
||||
s := e.state(a)
|
||||
if s.Status != "ready" {
|
||||
return
|
||||
}
|
||||
if s.Version < 1 {
|
||||
e.t.Fatalf("I2: ready without published version")
|
||||
}
|
||||
rows, err := e.pool.Query(e.ctx, `SELECT object_key, size_bytes, sha256 FROM attachment_variants
|
||||
WHERE attachment_id = $1 AND pipeline_version = $2`, a.ID, s.Version)
|
||||
if err != nil {
|
||||
e.t.Fatal(err)
|
||||
}
|
||||
n := 0
|
||||
for rows.Next() {
|
||||
var key, sum string
|
||||
var size int64
|
||||
_ = rows.Scan(&key, &size, &sum)
|
||||
b, ok := e.store.get(key)
|
||||
got := sha256.Sum256(b)
|
||||
if !ok || int64(len(b)) != size || hex.EncodeToString(got[:]) != sum {
|
||||
e.t.Fatalf("I2: variant object %s does not match metadata", key)
|
||||
}
|
||||
n++
|
||||
}
|
||||
rows.Close()
|
||||
if n != 3 {
|
||||
e.t.Fatalf("I2: ready with %d variants of version %d", n, s.Version)
|
||||
}
|
||||
key := e.scalar(`SELECT object_key FROM attachments WHERE id = $1`, a.ID)
|
||||
b, ok := e.store.get(key)
|
||||
got := sha256.Sum256(b)
|
||||
if !strings.HasPrefix(key, "o/") || !ok || hex.EncodeToString(got[:]) != a.SHA256 {
|
||||
e.t.Fatalf("I3: published original %s missing or changed", key)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *env) events(a *domain.Attachment) string {
|
||||
rows, _ := e.pool.Query(e.ctx, `SELECT event FROM job_events e JOIN jobs j USING (job_id)
|
||||
WHERE j.attachment_id = $1 ORDER BY e.id`, a.ID)
|
||||
defer rows.Close()
|
||||
var ev []string
|
||||
for rows.Next() {
|
||||
var s string
|
||||
_ = rows.Scan(&s)
|
||||
ev = append(ev, s)
|
||||
}
|
||||
return strings.Join(ev, ",")
|
||||
}
|
||||
|
||||
func expect(t *testing.T, cond bool, format string, args ...any) {
|
||||
t.Helper()
|
||||
if !cond {
|
||||
t.Fatalf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- сценарии ----------
|
||||
|
||||
func TestPipelineHappyPathAndDuplicate(t *testing.T) {
|
||||
e := setup(t)
|
||||
a := e.upload(pngBytes(320, 200, 1))
|
||||
msgs := e.relay(nil)
|
||||
expect(t, len(msgs) == 1, "outbox messages: %d", len(msgs))
|
||||
w := e.worker(5 * time.Second)
|
||||
|
||||
d, _ := handle(w, msgs[0])
|
||||
expect(t, d == worker.Ack, "decision %v", d)
|
||||
s := e.state(a)
|
||||
expect(t, s.Status == "ready" && s.Version == 1 && s.Variants == 3 && s.JobState == "done", "state %+v", s)
|
||||
e.checkInvariants(a)
|
||||
_, staged := e.store.get("s/" + e.chat + "/" + a.ID)
|
||||
expect(t, !staged, "staging must be removed after publish")
|
||||
|
||||
puts := len(e.store.puts)
|
||||
// Дубль после done (повторная доставка того же сообщения).
|
||||
d, _ = handle(w, msgs[0])
|
||||
expect(t, d == worker.Ack && len(e.store.puts) == puts, "duplicate must be a no-op ack")
|
||||
expect(t, e.state(a).Attempt == 1, "duplicate must not start a new attempt")
|
||||
e.checkInvariants(a)
|
||||
}
|
||||
|
||||
func TestPipelineConcurrentDuplicateIsBusy(t *testing.T) {
|
||||
e := setup(t)
|
||||
a := e.upload(pngBytes(64, 64, 2))
|
||||
msg := e.relay(nil)[0]
|
||||
// Первый worker захватил lease и «работает» (имитация: claim без завершения).
|
||||
out, _, _, err := e.repo.Claim(e.ctx, e.scalar(`SELECT job_id::text FROM jobs WHERE attachment_id=$1`, a.ID), "slow-worker", time.Minute)
|
||||
expect(t, err == nil && out == postgres.Claimed, "claim %v %v", out, err)
|
||||
d, _ := handle(e.worker(time.Minute), msg)
|
||||
expect(t, d == worker.Ack, "duplicate during valid lease must be acked")
|
||||
expect(t, e.state(a).Attempt == 1 && e.state(a).Variants == 0, "duplicate must not process: %+v", e.state(a))
|
||||
}
|
||||
|
||||
func TestPipelineCrashAfterSideEffectBeforeCommit(t *testing.T) {
|
||||
e := setup(t)
|
||||
a := e.upload(pngBytes(300, 240, 3))
|
||||
msg := e.relay(nil)[0]
|
||||
_, crashed := handle(e.worker(time.Second, "worker.after_side_effect"), msg)
|
||||
expect(t, crashed, "fault must crash the worker")
|
||||
s := e.state(a)
|
||||
expect(t, s.Status == "processing" && s.Variants == 0, "after crash: %+v", s)
|
||||
puts := len(e.store.puts)
|
||||
expect(t, puts >= 4, "side effects (3 variants + original copy) written before crash: %d", puts)
|
||||
|
||||
// Перезапущенный broker доставит то же сообщение снова, но lease ещё жив → ack дубля;
|
||||
// восстановление делает sweeper после истечения lease.
|
||||
d, _ := handle(e.worker(time.Second), msg)
|
||||
expect(t, d == worker.Ack && e.state(a).Variants == 0, "redelivery during lease: %+v", e.state(a))
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
n, err := e.repo.SweepExpiredLeases(e.ctx)
|
||||
expect(t, err == nil && n == 1, "sweep %d %v", n, err)
|
||||
msgs := e.relay(nil)
|
||||
expect(t, len(msgs) == 1, "requeued message: %d", len(msgs))
|
||||
d, _ = handle(e.worker(time.Second), msgs[0])
|
||||
s = e.state(a)
|
||||
expect(t, d == worker.Ack && s.Status == "ready" && s.Attempt == 2 && s.Variants == 3, "after recovery: %+v", s)
|
||||
// Повтор перезаписал те же ключи, не создал новых объектов.
|
||||
keys := 0
|
||||
for k := range e.store.puts {
|
||||
if strings.HasPrefix(k, "v/") {
|
||||
keys++
|
||||
}
|
||||
}
|
||||
expect(t, keys == 3, "variant object keys: %d (must stay 3)", keys)
|
||||
e.checkInvariants(a)
|
||||
expect(t, strings.Contains(e.events(a), "lease_expired_requeued"), "events: %s", e.events(a))
|
||||
}
|
||||
|
||||
func TestPipelineCrashAfterCommitBeforeAck(t *testing.T) {
|
||||
e := setup(t)
|
||||
a := e.upload(pngBytes(100, 100, 4))
|
||||
msg := e.relay(nil)[0]
|
||||
_, crashed := handle(e.worker(5*time.Second, "worker.after_commit"), msg)
|
||||
expect(t, crashed && e.state(a).Status == "ready", "commit before crash: %+v", e.state(a))
|
||||
puts := len(e.store.puts)
|
||||
d, _ := handle(e.worker(5*time.Second), msg) // broker доставляет снова
|
||||
expect(t, d == worker.Ack && len(e.store.puts) == puts && e.state(a).Attempt == 1, "redelivery after commit must be no-op")
|
||||
e.checkInvariants(a)
|
||||
}
|
||||
|
||||
func TestPipelineRelayCrashAfterPublishDuplicates(t *testing.T) {
|
||||
e := setup(t)
|
||||
a := e.upload(pngBytes(80, 60, 5))
|
||||
var first [][]byte
|
||||
func() {
|
||||
defer func() { _ = recover() }()
|
||||
first = e.relay(func() { panic(crash{137}) }) // confirm получен, COMMIT нет
|
||||
}()
|
||||
again := e.relay(nil) // outbox не помечен → публикуется ещё раз
|
||||
expect(t, len(again) == 1, "message must be republished after relay crash")
|
||||
w := e.worker(5 * time.Second)
|
||||
handle(w, again[0])
|
||||
handle(w, again[0]) // и ещё дубль
|
||||
s := e.state(a)
|
||||
expect(t, s.Status == "ready" && s.Attempt == 1 && s.Variants == 3, "duplicate publish: %+v", s)
|
||||
e.checkInvariants(a)
|
||||
_ = first
|
||||
}
|
||||
|
||||
func TestPipelineVersionReorderAndStaleJob(t *testing.T) {
|
||||
e := setup(t)
|
||||
a := e.upload(pngBytes(320, 200, 6))
|
||||
v1msg := e.relay(nil)[0]
|
||||
|
||||
// Reorder: v2 поставлена и обработана РАНЬШЕ, чем дошла v1.
|
||||
_, err := e.repo.Reprocess(e.ctx, a.ID, 2, 3)
|
||||
expect(t, err == nil, "reprocess: %v", err)
|
||||
v2msg := e.relay(nil)[0]
|
||||
handle(e.worker(5*time.Second), v2msg)
|
||||
s := e.state(a)
|
||||
expect(t, s.Status == "ready" && s.Version == 2, "v2 published: %+v", s)
|
||||
thumbV2 := e.scalar(`SELECT sha256 FROM attachment_variants WHERE attachment_id=$1 AND pipeline_version=2 AND name='thumbnail'`, a.ID)
|
||||
|
||||
// Устаревшая v1 приходит позже: не должна перезаписать v2.
|
||||
d, _ := handle(e.worker(5*time.Second), v1msg)
|
||||
s = e.state(a)
|
||||
expect(t, d == worker.Ack && s.Version == 2, "stale v1 overwrote v2: %+v", s)
|
||||
expect(t, e.scalar(`SELECT count(*) FROM attachment_variants WHERE attachment_id=$1 AND pipeline_version=1`, a.ID) == "0",
|
||||
"stale v1 must be a no-op when v2 is already published")
|
||||
e.checkInvariants(a)
|
||||
|
||||
// Гонка: v1 УЖЕ обрабатывалась (lease), v3 успела опубликоваться, v1 фиксируется позже.
|
||||
b := e.upload(pngBytes(200, 100, 7))
|
||||
b1 := e.relay(nil)[0]
|
||||
jobV1 := e.scalar(`SELECT job_id::text FROM jobs WHERE attachment_id=$1 AND pipeline_version=1`, b.ID)
|
||||
out, j1, _, _ := e.repo.Claim(e.ctx, jobV1, "slow", time.Minute)
|
||||
expect(t, out == postgres.Claimed, "claim v1")
|
||||
_, _ = e.repo.Reprocess(e.ctx, b.ID, 3, 3)
|
||||
handle(e.worker(5*time.Second), e.relay(nil)[0])
|
||||
expect(t, e.state(b).Version == 3, "v3 published")
|
||||
res, err := e.repo.Complete(e.ctx, jobV1, "slow", j1.Attempt, "o/"+e.chat+"/"+b.ID, []domain.Variant{
|
||||
{Name: "thumbnail", ContentType: "image/jpeg", SizeBytes: 1, Width: 1, Height: 1, SHA256: strings.Repeat("a", 64), ObjectKey: "v/x/p1/thumbnail.jpg"}})
|
||||
expect(t, err == nil && res.Applied && !res.Published && res.NewerPublished == 3, "late v1 commit: %+v %v", res, err)
|
||||
expect(t, e.state(b).Version == 3, "late v1 commit downgraded version")
|
||||
_ = b1
|
||||
expect(t, e.scalar(`SELECT sha256 FROM attachment_variants WHERE attachment_id=$1 AND pipeline_version=2 AND name='thumbnail'`, a.ID) == thumbV2,
|
||||
"v2 result changed")
|
||||
}
|
||||
|
||||
func TestPipelineLostLeaseFencing(t *testing.T) {
|
||||
e := setup(t)
|
||||
a := e.upload(pngBytes(90, 90, 8))
|
||||
jobID := e.scalar(`SELECT job_id::text FROM jobs WHERE attachment_id=$1`, a.ID)
|
||||
e.relay(nil)
|
||||
_, ja, _, _ := e.repo.Claim(e.ctx, jobID, "A", 300*time.Millisecond) // A «завис»
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
_, _ = e.repo.SweepExpiredLeases(e.ctx)
|
||||
handle(e.worker(5*time.Second), e.relay(nil)[0]) // B завершает попытку 2
|
||||
expect(t, e.state(a).Status == "ready" && e.state(a).Attempt == 2, "B finished: %+v", e.state(a))
|
||||
res, err := e.repo.Complete(e.ctx, jobID, "A", ja.Attempt, "o/zz", nil) // A «проснулся»
|
||||
expect(t, err == nil && res.LostLease, "zombie A must be fenced: %+v %v", res, err)
|
||||
e.checkInvariants(a)
|
||||
}
|
||||
|
||||
func TestPipelineTransientRetryWithBackoff(t *testing.T) {
|
||||
e := setup(t)
|
||||
a := e.upload(pngBytes(120, 80, 9))
|
||||
msg := e.relay(nil)[0]
|
||||
e.store.failPut = 1
|
||||
d, _ := handle(e.worker(5*time.Second), msg)
|
||||
s := e.state(a)
|
||||
expect(t, d == worker.Ack && s.Status == "queued" && s.JobState == "queued", "retry scheduled: %+v", s)
|
||||
expect(t, len(e.relay(nil)) == 0, "retry must wait for backoff")
|
||||
d, _ = handle(e.worker(5*time.Second), msg) // ранняя повторная доставка
|
||||
expect(t, d == worker.Ack && e.state(a).Attempt == 1, "early delivery must not run: %+v", e.state(a))
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
msgs := e.relay(nil)
|
||||
var job domain.Job
|
||||
expect(t, len(msgs) == 1 && json.Unmarshal(msgs[0], &job) == nil && job.Attempt == 2, "retry message attempt 2: %s", msgs)
|
||||
handle(e.worker(5*time.Second), msgs[0])
|
||||
expect(t, e.state(a).Status == "ready" && e.state(a).Attempt == 2, "after retry: %+v", e.state(a))
|
||||
e.checkInvariants(a)
|
||||
}
|
||||
|
||||
func TestPipelinePermanentFailureAndReplay(t *testing.T) {
|
||||
e := setup(t)
|
||||
good := pngBytes(50, 40, 10)
|
||||
a := e.upload(good)
|
||||
// Порча staging-объекта: checksum не совпадёт → постоянная ошибка, без повторов.
|
||||
key := "s/" + e.chat + "/" + a.ID
|
||||
_ = e.store.Put(e.ctx, key, bytes.NewReader([]byte("garbage")), 7, "image/png")
|
||||
handle(e.worker(5*time.Second), e.relay(nil)[0])
|
||||
s := e.state(a)
|
||||
expect(t, s.Status == "failed" && s.ErrorCode == "checksum_mismatch" && s.Attempt == 1, "permanent: %+v", s)
|
||||
|
||||
// Replay успешной работы запрещён; failed — разрешён (с аудитом).
|
||||
_, err := e.repo.Replay(e.ctx, a.ID, 1, "tester")
|
||||
expect(t, err == nil, "replay failed job: %v", err)
|
||||
_ = e.store.Put(e.ctx, key, bytes.NewReader(good), int64(len(good)), "image/png") // исходник восстановлен
|
||||
handle(e.worker(5*time.Second), e.relay(nil)[0])
|
||||
expect(t, e.state(a).Status == "ready", "after replay: %+v", e.state(a))
|
||||
_, err = e.repo.Replay(e.ctx, a.ID, 1, "tester")
|
||||
expect(t, err != nil, "replay of done job must be refused")
|
||||
expect(t, strings.Contains(e.events(a), "replay,") && strings.Contains(e.events(a), "replay_refused"), "audit: %s", e.events(a))
|
||||
e.checkInvariants(a)
|
||||
}
|
||||
|
||||
func TestPipelineRetryExhausted(t *testing.T) {
|
||||
e := setup(t)
|
||||
a := e.upload(pngBytes(30, 30, 11))
|
||||
e.store.failPut = 100
|
||||
msg := e.relay(nil)[0]
|
||||
for i := 0; i < 3; i++ {
|
||||
handle(e.worker(5*time.Second), msg)
|
||||
time.Sleep(450 * time.Millisecond)
|
||||
if m := e.relay(nil); len(m) > 0 {
|
||||
msg = m[0]
|
||||
}
|
||||
}
|
||||
s := e.state(a)
|
||||
expect(t, s.Status == "failed" && s.ErrorCode == "retry_exhausted" && s.Attempt == 3, "exhausted: %+v", s)
|
||||
jobs, _ := e.repo.FailedJobs(e.ctx, 10)
|
||||
expect(t, len(jobs) == 1, "failed registry: %d", len(jobs))
|
||||
}
|
||||
@@ -0,0 +1,337 @@
|
||||
// Package worker обрабатывает задания attachment.process.v1.
|
||||
//
|
||||
// Порядок обработки одного сообщения (at-least-once):
|
||||
//
|
||||
// 1. Claim: lease в PostgreSQL (fencing: owner + attempt). Дубль при живом lease,
|
||||
// уже опубликованный результат, ранняя доставка до backoff → ack без работы.
|
||||
// 2. Чтение оригинала из S3 по ключу из БД (не из сообщения), сверка SHA-256.
|
||||
// 3. Варианты → PUT по детерминированным ключам v/{chat}/{id}/p{ver}/{name}
|
||||
// (повтор перезаписывает те же байты), оригинал staging → o/ (CopyObject).
|
||||
// 4. Complete: одна транзакция — варианты (ON CONFLICT по ключу эффекта),
|
||||
// attachment → ready этой версии (только если новее опубликованной), job → done.
|
||||
// 5. ack. Затем удаление staging (результат уже безопасно зафиксирован).
|
||||
//
|
||||
// Падение на любом шаге до ack приводит к повторной доставке; повтор либо
|
||||
// доделывает тот же эффект (шаги 2–4 идемпотентны), либо видит done и ack-ает.
|
||||
package worker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
"log/slog"
|
||||
"math/rand/v2"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sync/semaphore"
|
||||
|
||||
"github.com/charawein/mtusi2026lab5/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab5/internal/imaging"
|
||||
"github.com/charawein/mtusi2026lab5/internal/repository/postgres"
|
||||
"github.com/charawein/mtusi2026lab5/internal/storage"
|
||||
)
|
||||
|
||||
// Jobs — операции над заданиями (реализует postgres.PipelineRepository).
|
||||
type Jobs interface {
|
||||
Claim(ctx context.Context, jobID, owner string, lease time.Duration) (postgres.ClaimOutcome, *postgres.JobRow, *domain.Attachment, error)
|
||||
ExtendLease(ctx context.Context, jobID, owner string, attempt int, lease time.Duration) (bool, error)
|
||||
Complete(ctx context.Context, jobID, owner string, attempt int, finalKey string, variants []domain.Variant) (postgres.CompleteResult, error)
|
||||
RetryOrFail(ctx context.Context, jobID, owner string, attempt int, code, detail string, permanent bool, backoff time.Duration) (bool, error)
|
||||
ClearStaging(ctx context.Context, attachmentID, key string) error
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Owner string
|
||||
Lease time.Duration // = JOB_TIMEOUT: дольше работа не может идти без продления
|
||||
JobTimeout time.Duration
|
||||
MaxBytes int64
|
||||
MaxPixels int64
|
||||
PixelBudget int64 // суммарно декодируемых пикселей на процесс (ограничение памяти)
|
||||
WebPQuality int
|
||||
RetryBase time.Duration
|
||||
RetryMax time.Duration
|
||||
FaultPoints map[string]bool
|
||||
FaultDir string
|
||||
DeleteStaging bool
|
||||
}
|
||||
|
||||
type Worker struct {
|
||||
jobs Jobs
|
||||
store storage.Store
|
||||
webp imaging.WebPEncoder
|
||||
cfg Config
|
||||
pixels *semaphore.Weighted
|
||||
exit func(int)
|
||||
}
|
||||
|
||||
func New(jobs Jobs, store storage.Store, webp imaging.WebPEncoder, cfg Config) *Worker {
|
||||
return &Worker{jobs: jobs, store: store, webp: webp, cfg: cfg,
|
||||
pixels: semaphore.NewWeighted(cfg.PixelBudget), exit: os.Exit}
|
||||
}
|
||||
|
||||
// Decision — что сделать с доставкой.
|
||||
type Decision int
|
||||
|
||||
const (
|
||||
Ack Decision = iota
|
||||
Requeue // nack с возвратом в очередь (инфраструктура недоступна)
|
||||
Reject // nack без возврата → DLX (невалидное сообщение)
|
||||
)
|
||||
|
||||
func (d Decision) String() string { return [...]string{"ack", "requeue", "reject"}[d] }
|
||||
|
||||
// permanentError — повтор не поможет (битое изображение, нет исходника, checksum).
|
||||
type permanentError struct {
|
||||
code string
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *permanentError) Error() string { return e.code + ": " + e.err.Error() }
|
||||
|
||||
func permanent(code string, err error) error { return &permanentError{code: code, err: err} }
|
||||
|
||||
// Fault — точка внедрения сбоя (трек 5): процесс завершается как при kill -9
|
||||
// ровно один раз (маркер в FaultDir переживает перезапуск контейнера).
|
||||
func Fault(points map[string]bool, dir, point string, exit func(int)) {
|
||||
if !points[point] {
|
||||
return
|
||||
}
|
||||
marker := filepath.Join(dir, "fault-"+point)
|
||||
if _, err := os.Stat(marker); err == nil {
|
||||
return
|
||||
}
|
||||
_ = os.WriteFile(marker, []byte(time.Now().UTC().Format(time.RFC3339)), 0o644)
|
||||
slog.Warn("FAULT INJECTION: crashing", "point", point)
|
||||
exit(137)
|
||||
}
|
||||
|
||||
func (w *Worker) fault(point string) { Fault(w.cfg.FaultPoints, w.cfg.FaultDir, point, w.exit) }
|
||||
|
||||
// Backoff — экспоненциальная задержка с полным jitter в [d/2, d].
|
||||
func Backoff(attempt int, base, maxDelay time.Duration) time.Duration {
|
||||
d := base << min(attempt-1, 20)
|
||||
if d <= 0 || d > maxDelay {
|
||||
d = maxDelay
|
||||
}
|
||||
half := d / 2
|
||||
return half + time.Duration(rand.Int64N(int64(half)+1))
|
||||
}
|
||||
|
||||
// Handle обрабатывает одну доставку.
|
||||
func (w *Worker) Handle(ctx context.Context, body []byte) Decision {
|
||||
var job domain.Job
|
||||
if err := json.Unmarshal(body, &job); err != nil || job.SchemaVersion != 1 || job.Type != domain.JobType ||
|
||||
job.JobID == "" || job.AttachmentID == "" || job.PipelineVersion < 1 {
|
||||
slog.Error("invalid job message, dead-lettering", "err", err)
|
||||
return Reject
|
||||
}
|
||||
log := slog.With("job_id", job.JobID, "attachment_id", job.AttachmentID, "version", job.PipelineVersion)
|
||||
|
||||
outcome, j, a, err := w.jobs.Claim(ctx, job.JobID, w.cfg.Owner, w.cfg.Lease)
|
||||
if err != nil {
|
||||
log.Warn("claim failed, requeue", "err", err)
|
||||
return Requeue
|
||||
}
|
||||
switch outcome {
|
||||
case postgres.AlreadyDone:
|
||||
log.Info("duplicate delivery of finished job: ack without work")
|
||||
return Ack
|
||||
case postgres.Busy:
|
||||
log.Info("job leased by another worker: ack duplicate", "owner", j.LeaseOwner)
|
||||
return Ack
|
||||
case postgres.NotDue:
|
||||
log.Info("early delivery before backoff: ack, outbox will redeliver")
|
||||
return Ack
|
||||
case postgres.Terminal, postgres.ExhaustedNow:
|
||||
log.Info("job terminal: ack", "state", stateOf(j))
|
||||
return Ack
|
||||
}
|
||||
|
||||
// Heartbeat: продлеваем lease, пока работаем; потеря lease прерывает работу.
|
||||
pctx, cancel := context.WithTimeout(ctx, w.cfg.JobTimeout)
|
||||
defer cancel()
|
||||
hbDone := make(chan struct{})
|
||||
go func() {
|
||||
defer close(hbDone)
|
||||
t := time.NewTicker(w.cfg.Lease / 3)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-pctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
ok, err := w.jobs.ExtendLease(context.WithoutCancel(pctx), j.JobID, w.cfg.Owner, j.Attempt, w.cfg.Lease)
|
||||
if err == nil && !ok {
|
||||
log.Warn("lease lost, aborting attempt", "attempt", j.Attempt)
|
||||
cancel()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
finalKey, variants, perr := w.process(pctx, a, j.PipelineVersion)
|
||||
cancel()
|
||||
<-hbDone
|
||||
|
||||
dctx, dcancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second)
|
||||
defer dcancel()
|
||||
if perr != nil {
|
||||
var pe *permanentError
|
||||
code, isPermanent := "processing_failed", false
|
||||
if errors.As(perr, &pe) {
|
||||
code, isPermanent = pe.code, true
|
||||
} else if errors.Is(perr, context.DeadlineExceeded) {
|
||||
code = "timeout"
|
||||
}
|
||||
backoff := Backoff(j.Attempt, w.cfg.RetryBase, w.cfg.RetryMax)
|
||||
failed, err := w.jobs.RetryOrFail(dctx, j.JobID, w.cfg.Owner, j.Attempt, code, safeDetail(perr), isPermanent, backoff)
|
||||
if err != nil {
|
||||
// Записать исход не удалось: lease истечёт, sweeper вернёт работу в очередь.
|
||||
log.Warn("record failure failed; lease expiry will recover", "err", err)
|
||||
return Requeue
|
||||
}
|
||||
log.Warn("attempt failed", "attempt", j.Attempt, "code", code, "final", failed, "backoff", backoff)
|
||||
return Ack
|
||||
}
|
||||
|
||||
w.fault("worker.after_side_effect") // объекты записаны, результат ещё не зафиксирован
|
||||
|
||||
res, err := w.jobs.Complete(dctx, j.JobID, w.cfg.Owner, j.Attempt, finalKey, variants)
|
||||
if err != nil {
|
||||
log.Warn("complete failed; requeue", "err", err)
|
||||
return Requeue
|
||||
}
|
||||
if res.LostLease {
|
||||
log.Warn("result rejected: lease lost (another attempt owns the job)", "attempt", j.Attempt)
|
||||
return Ack
|
||||
}
|
||||
|
||||
w.fault("worker.after_commit") // результат зафиксирован, ack ещё не отправлен
|
||||
|
||||
if res.Published && w.cfg.DeleteStaging && a.StagingKey != "" && a.StagingKey != finalKey {
|
||||
// Staging больше не нужен: оригинал опубликован в o/ и зафиксирован.
|
||||
if err := w.store.Delete(dctx, a.StagingKey); err == nil {
|
||||
_ = w.jobs.ClearStaging(dctx, a.ID, a.StagingKey)
|
||||
}
|
||||
}
|
||||
log.Info("job done", "attempt", j.Attempt, "published", res.Published, "newer_version", res.NewerPublished)
|
||||
return Ack
|
||||
}
|
||||
|
||||
func stateOf(j *postgres.JobRow) string {
|
||||
if j == nil {
|
||||
return "missing"
|
||||
}
|
||||
return j.State
|
||||
}
|
||||
|
||||
func safeDetail(err error) string {
|
||||
s := err.Error()
|
||||
if len(s) > 300 {
|
||||
s = s[:300]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func ext(ctype string) string {
|
||||
switch ctype {
|
||||
case "image/webp":
|
||||
return "webp"
|
||||
case "image/png":
|
||||
return "png"
|
||||
}
|
||||
return "jpg"
|
||||
}
|
||||
|
||||
// process — шаги 2–3: чтение, проверка, варианты, публикация оригинала.
|
||||
func (w *Worker) process(ctx context.Context, a *domain.Attachment, version int) (string, []domain.Variant, error) {
|
||||
rc, size, err := w.store.Get(ctx, a.ObjectKey)
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
return "", nil, permanent("source_missing", err)
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if size > w.cfg.MaxBytes {
|
||||
rc.Close()
|
||||
return "", nil, permanent("too_large", fmt.Errorf("source %d bytes", size))
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(rc, w.cfg.MaxBytes+1))
|
||||
rc.Close()
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if sum := imaging.SHA256Hex(data); sum != a.SHA256 {
|
||||
return "", nil, permanent("checksum_mismatch", fmt.Errorf("source sha256 %s != metadata", sum[:12]))
|
||||
}
|
||||
cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return "", nil, permanent("invalid_image", err)
|
||||
}
|
||||
px := int64(cfg.Width) * int64(cfg.Height)
|
||||
if px > w.cfg.MaxPixels {
|
||||
return "", nil, permanent("too_large", fmt.Errorf("%d pixels", px))
|
||||
}
|
||||
// Ограничение памяти: суммарно декодируемых пикселей в процессе ≤ бюджета
|
||||
// (RGBA = 4 байта/пиксель; ресайз добавляет меньшие буферы).
|
||||
weight := min(px, w.cfg.PixelBudget)
|
||||
if err := w.pixels.Acquire(ctx, weight); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
defer w.pixels.Release(weight)
|
||||
|
||||
img, err := imaging.Decode(data)
|
||||
if err != nil {
|
||||
return "", nil, permanent("invalid_image", err)
|
||||
}
|
||||
p := imaging.ParamsFor(version, w.cfg.WebPQuality)
|
||||
outs := make([]imaging.Output, 0, 3)
|
||||
th, err := imaging.Thumbnail(img, p)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
cr, err := imaging.Crop(img, p)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
op, err := imaging.Optimized(ctx, img, p, w.webp)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
outs = append(outs, th, cr, op)
|
||||
img = nil
|
||||
|
||||
base := fmt.Sprintf("v/%s/%s/p%d/", a.ChatID, a.ID, version)
|
||||
variants := make([]domain.Variant, 0, len(outs))
|
||||
for _, o := range outs {
|
||||
key := base + o.Name + "." + ext(o.ContentType)
|
||||
if err := w.store.Put(ctx, key, bytes.NewReader(o.Data), int64(len(o.Data)), o.ContentType); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
variants = append(variants, domain.Variant{Name: o.Name, ContentType: o.ContentType, SizeBytes: int64(len(o.Data)),
|
||||
Width: o.Width, Height: o.Height, SHA256: o.SHA256, ObjectKey: key, PipelineVersion: version})
|
||||
}
|
||||
// Оригинал: из staging в конечный prefix (серверная копия, байты неизменны).
|
||||
finalKey := a.ObjectKey
|
||||
if strings.HasPrefix(a.ObjectKey, "s/") {
|
||||
finalKey = "o/" + strings.TrimPrefix(a.ObjectKey, "s/")
|
||||
if err := w.store.Copy(ctx, a.ObjectKey, finalKey); err != nil {
|
||||
if errors.Is(err, storage.ErrNotFound) {
|
||||
return "", nil, permanent("source_missing", err)
|
||||
}
|
||||
return "", nil, err
|
||||
}
|
||||
}
|
||||
return finalKey, variants, nil
|
||||
}
|
||||
|
||||
// SetExit подменяет завершение процесса (тесты внедрения сбоев).
|
||||
func SetExit(w *Worker, exit func(int)) { w.exit = exit }
|
||||
Reference in New Issue
Block a user