package attachment import ( "context" "encoding/json" "fmt" "os" "strconv" "time" "gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgxpool" ) type Options struct { Concurrency, Prefetch, MaxOutstanding, MaxAttempts, PixelBudget int Timeout, Lease time.Duration } func LoadOptions() (Options, error) { o := Options{} for _, v := range []struct { name string target *int def, max int }{ {"WORKER_CONCURRENCY", &o.Concurrency, 2, 8}, {"WORKER_PREFETCH", &o.Prefetch, 4, 32}, {"MAX_OUTSTANDING_JOBS", &o.MaxOutstanding, 200, 10000}, {"WORKER_MAX_ATTEMPTS", &o.MaxAttempts, 5, 20}, {"WORKER_PIXEL_BUDGET", &o.PixelBudget, 40000000, 80000000}, } { n := v.def if raw := os.Getenv(v.name); raw != "" { var err error n, err = strconv.Atoi(raw) if err != nil { return o, fmt.Errorf("invalid %s", v.name) } } if n < 1 || n > v.max { return o, fmt.Errorf("invalid %s", v.name) } *v.target = n } for _, v := range []struct { name string target *time.Duration def int }{{"WORKER_TIMEOUT_SECONDS", &o.Timeout, 25}, {"WORKER_LEASE_SECONDS", &o.Lease, 40}} { n := v.def if raw := os.Getenv(v.name); raw != "" { var e error n, e = strconv.Atoi(raw) if e != nil { return o, fmt.Errorf("invalid %s", v.name) } } if n < 1 || n > 300 { return o, fmt.Errorf("invalid %s", v.name) } *v.target = time.Duration(n) * time.Second } if o.Lease < o.Timeout+5*time.Second { return o, fmt.Errorf("worker lease must exceed timeout by at least 5s") } if o.PixelBudget < int(MaxPixels) { return o, fmt.Errorf("WORKER_PIXEL_BUDGET must allow one 40MP image") } return o, nil } func NewAsync(pool *pgxpool.Pool, store *Store, o Options) *Service { s := New(pool, store) s.Async = true s.Options = o return s } func (s *Service) Upload(ctx context.Context, chatID, userID, name, contentType string, body interface{ Read([]byte) (int, error) }) (Attachment, error) { if !s.Async { return s.uploadSync(ctx, chatID, userID, name, contentType, body) } if err := ValidateFilename(name); err != nil { return Attachment{}, err } ctx, cancel := context.WithTimeout(ctx, UploadTimeout) defer cancel() select { case s.slots <- struct{}{}: defer func() { <-s.slots }() case <-ctx.Done(): return Attachment{}, unavailable() } original, err := Spool(ctx, body, contentType) if err != nil { return Attachment{}, err } defer original.Close() tx, err := s.Pool.Begin(ctx) if err != nil { return Attachment{}, unavailable() } defer tx.Rollback(context.Background()) // All API replicas serialize admission, including inserts before their COMMIT. if _, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(578903551)`); err != nil { return Attachment{}, unavailable() } var pending int if err = tx.QueryRow(ctx, `SELECT count(*) FROM attachment_jobs WHERE state IN ('queued','processing')`).Scan(&pending); err != nil { return Attachment{}, unavailable() } if pending >= s.Options.MaxOutstanding { return Attachment{}, appError("rate_limited", "Очередь заполнена; повторите загрузку позднее") } a := Attachment{ID: uuid.NewString(), ChatID: chatID, OwnerID: userID, Filename: name, ContentType: original.ContentType, Size: original.Size, SHA256: original.SHA256, Status: "queued", Variants: []any{}, PipelineVersion: 1} stage := "staging/" + a.ID a.Key = effectKey(a.ID, 1, "original") // The stage lock protects even a slow PUT from the orphan cleaner. if _, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,0))`, stage); err != nil { return Attachment{}, unavailable() } if err = s.put(ctx, stage, original.File, original.Size, original.ContentType); err != nil { return Attachment{}, unavailable() } err = tx.QueryRow(ctx, `INSERT INTO attachments(id,chat_id,uploader_id,filename,object_key,content_type,size_bytes,sha256,width,height,status,staging_key) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,'queued',$11) RETURNING created_at,updated_at`, a.ID, chatID, userID, name, a.Key, a.ContentType, a.Size, a.SHA256, original.Width, original.Height, stage).Scan(&a.CreatedAt, &a.UpdatedAt) if err != nil { return Attachment{}, unavailable() } job := uuid.NewString() if _, err = tx.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,state,max_attempts) VALUES($1,$2,'queued',$3)`, job, a.ID, s.Options.MaxAttempts); err != nil { return Attachment{}, unavailable() } if _, err = tx.Exec(ctx, `INSERT INTO attachment_outbox(job_id) VALUES($1)`, job); err != nil { return Attachment{}, unavailable() } if err = tx.Commit(ctx); err != nil { return Attachment{}, unavailable() } return a, nil } func effectKey(id string, version int, name string) string { return fmt.Sprintf("original/%s/v%d/%s", id, version, name) } func (s *Service) Variant(a Attachment, name string) (Attachment, error) { if name == "original" { return a, nil } for _, item := range a.Variants { raw, _ := json.Marshal(item) var v Variant if json.Unmarshal(raw, &v) == nil && v.Name == name { a.Key = effectKey(a.ID, a.PipelineVersion, name) a.ContentType = v.ContentType return a, nil } } return Attachment{}, appError(domain.ErrorNotFound, "Вариант не найден") }