~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

host: blobs, kept while a record references them, served and listed

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/blobs

What changes

Blobs, kept as long as a record references them. Bytes live on disk under DATA_DIR/blobs/<did>/<cid>, each written whole to a temporary name and renamed into place; the database (a third migration) keeps each blob's type, size and whether it is still temporary, and which records reference which blobs.

  • Host::upload_blob(did, bytes, type) keeps an upload as temporary, refusing one over BLOB_UPLOAD_LIMIT (BlobTooLarge, default 50 MiB, as the prototype), and answers the {"$type": "blob", …} reference a record holds it by.
  • A write's records may reference only blobs uploaded to that account. The commit links them, makes them permanent, and forgets those no record references any more, whose files then go; the #commit event names its records' blobs in blobs.
  • Uploads no record took are swept a day on: at start, then hourly.
  • com.atproto.sync.getBlob serves a referenced blob only, with X-Content-Type-Options: nosniff and a sandboxing CSP; com.atproto.sync.listBlobs lists referenced blobs by CID, a page at a time, and since a rev.

Why

A group's profile has an avatar and a banner, and its records will carry pictures; the PDS that holds the records holds their blobs. This is the prototype's lifecycle, the reference PDS's on a plain directory.

How it was checked

tests/blobs.rs: an upload not served until a record references it, then served with its headers, listed, and named in the commit's event; a blob two records share kept until both drop it, its file then gone; listBlobs' pages and since; the size limit, an unknown repo, a record referencing a blob not uploaded, a bad CID; the sweep removing only an untaken upload; blob_refs finding references at any depth and only blob-typed ones. cargo llvm-cov at 100% of lines and functions, clippy clean.

Shortcomings

com.atproto.repo.uploadBlob needs a group's OAuth session, so it comes with signing in; uploads are made from Rust until then. No image route yet (the prototype's /img/<group>/<slot>), which serves from the meta space.

Approved

This adds blob storage to the host. Bytes go on disk and their metadata and record references go in the database. Writes validate and link blob references, commit events name the blobs, and orphaned blobs are removed. getBlob and listBlobs are added, and a sweep runs hourly. I found no correctness bug that blocks the merge. I did not run the tests or read the checks.

The docs the repository asks for are updated: the design README and the config reference. The transaction logic is sound. Unlinking, relinking and then FORGET_ORPHAN give the right result when one write moves a reference between records. The temporary = 0 guard stops an orphan delete from removing an upload that is still waiting.

Two small robustness points, neither of which should block the merge:

  • A re-upload of a temporary blob keeps its old created_at, so the sweep can remove a blob the client just re-sent.
  • The .part file name is shared by concurrent uploads of identical bytes in one process. If two run at once, one rename can fail with ENOENT and return a 500.
crates/group-host/src/store.rsline 78Outdated

These lines can no longer be read.

review.knotless.dev

On conflict only mime_type is updated, so re-uploading a still-temporary blob does not extend its life. Setting created_at = excluded.created_at when temporary = 1 would fix that.

crates/group-host/src/blobs.rsline 75Outdated

These lines can no longer be read.

review.knotless.dev

The temp name is only the pid. Two simultaneous uploads of the same bytes (same cid) share it, so one rename can fail with ENOENT and surface as a 500. A per-upload counter or random suffix avoids that.

Merged as 4d45b6e