feat(media): uploads in pieces — resumable, and a relay's memory no longer grows with the film
Bun.serve reads a request body as fast as the client sends it, whether the handler takes it or not. Whenever the store was slower than the client, one PUT of a film put the whole film in the relay's memory: a 512 MiB upload peaked at 762 MiB (disk) and 1011 MiB (S3). A sealed file over 16 MiB now goes up as POST /v1/media/:id/upload plus 16 MiB PATCH pieces, each signed over its offset and length, up to 4 in flight and landing in any order. Uploads under way are durable: on disk uploads/<id>.part + .json + .log (the log names a piece only after fsync), and in S3 a row in shade_media_uploads beside the multipart upload, where piece n is part n+1. A dropped connection, a relay restart or the next piece landing on another instance costs only the pieces in flight. The client asks how far the upload got and sends the rest. Uploads count against quota and are swept after 24 h without a piece. An upload still receiving pieces is never swept, however old its multipart upload is. The sweep is now part of every MediaStore and runs from createMediaService (onSweep) instead of two copies of the timer. No route takes more than 64 MiB, so Bun's 128 MiB cap stays and the container's body limit covers every route. `media put` shows progress. bench/media-store.ts: the relay's peak RSS is 330–369 MiB at both 512 MiB and 1.5 GiB (worst of 3 runs, both stores). Also found: - Bun 1.3.5 fetch never finishes sending a Bun.file().slice() body, so the client reads each piece itself. - postgres.js reads a NULL element of a TEXT[] as the string "NULL", so etags are selected as JSON. Tests: - tests/upload.test.ts (11). - S3 across two instances (+2). - PG index (+1). - media 114, storage-postgres 21, server 55, cli 15. - All 27 packages type-check. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -98,7 +98,35 @@ SHADE_MEDIA_OWNERS=<key>,<key> # who may upload / open channels (`sh
|
||||
SHADE_DISABLE_MEDIA=1 # optional: off entirely
|
||||
```
|
||||
|
||||
Routes: `/v1/media/:id`, `/v1/live/:channelId` (WebSocket), `/watch/`.
|
||||
Routes: `/v1/media/:id`, `/v1/media/:id/upload`, `/v1/live/:channelId` (WebSocket), `/watch/`.
|
||||
|
||||
### Uploads go up in pieces
|
||||
|
||||
A sealed file larger than 16 MiB is not one request. `shade media put` (and
|
||||
`MediaClient.putSealed`) starts an upload (`POST /v1/media/:id/upload`, signed over
|
||||
id, size and read-token hash) and sends it in pieces of 16 MiB (`PATCH`, each signed
|
||||
over its offset and length), up to 4 at once. The relay keeps what landed on disk or
|
||||
in the bucket, so:
|
||||
|
||||
- **A dropped connection costs the pieces in flight, not the film.** After a failure
|
||||
the client asks the relay how far the upload got and sends only what is missing,
|
||||
with backoff, for up to 8 tries in a row. A piece sent twice is the same bytes twice,
|
||||
and the second is a no-op.
|
||||
- **A relay restart, or the next piece landing on another instance, changes nothing.**
|
||||
The upload is `uploads/<id>.part` + `.json` + `.log` on disk, or a row in
|
||||
`shade_media_uploads` beside its S3 multipart upload. The log (or the row) names
|
||||
only pieces already on stable storage, so a piece cut off by a crash is sent again.
|
||||
- **The relay's memory is bounded by the window, not the film.** This is the reason
|
||||
the protocol exists. Bun.serve does not hold back a request body the handler reads
|
||||
slowly: one `PUT` of a 512 MiB film put 762 MiB (disk) and 1011 MiB (S3) into the
|
||||
relay. In pieces, it stays around 350 MiB at 512 MiB and at 1.5 GiB alike
|
||||
(`bench/media-store.ts`). Every route now takes at most 64 MiB per
|
||||
request, so Bun's own 128 MiB cap stays in place.
|
||||
- An upload counts against the owner's quota from its start. One with no new piece
|
||||
for 24 hours is dropped by the sweep.
|
||||
|
||||
A file of at most one piece still goes up as one `PUT /v1/media/:id`. The full exchange
|
||||
is described in `packages/shade-media/src/upload.ts`.
|
||||
|
||||
**There is no in-memory fallback**, for the vault's reason: a library that lives in
|
||||
RAM accepts every upload and loses all of it on the next redeploy, without anything
|
||||
@@ -122,15 +150,18 @@ SeaweedFS, Garage, B2); credentials left out of the URL come from
|
||||
or `?region=`. A bucket the credentials cannot use is reported at start; media stays
|
||||
off and the rest of the server runs.
|
||||
|
||||
- **Uploads stream.** A film goes up as an S3 multipart upload, part by part, with at
|
||||
most three 16 MiB parts in memory (512 MiB through: 11 MiB of RSS). Every part's
|
||||
payload is hashed into its signature, so a part damaged in transit is refused.
|
||||
- **Uploads stream.** Each piece of an upload is one part of an S3 multipart upload,
|
||||
and any instance may take the next one. Every part's payload is hashed into its
|
||||
signature, so a part damaged in transit is refused. A file sent in one `PUT` goes up
|
||||
part by part as it arrives, with at most three 16 MiB parts in memory.
|
||||
- **Racing uploads of one id** (on any instances) each go to their own object key;
|
||||
the primary key in Postgres admits one record, and the loser deletes its object.
|
||||
Delete removes the record first, so a crash never brings a film back.
|
||||
- **Sweeping.** Objects no record points at and multipart uploads never completed
|
||||
(a crash mid-upload) are reclaimed after 24 hours, a minute after start and every
|
||||
6 hours. Every instance may sweep at once.
|
||||
- **Sweeping.** Three kinds of leftovers are reclaimed after 24 hours: objects no
|
||||
record points at, uploads with no new piece in that time, and multipart uploads no
|
||||
upload owns (a crash mid-`PUT`). An upload still receiving pieces is spared, however
|
||||
old its multipart upload is. The sweep runs a minute after start and then hourly,
|
||||
for the disk store too, and every instance may sweep at once.
|
||||
- **Live channels** are pinned in Postgres, so a channel cannot be pinned to two keys
|
||||
by two instances. Each instance records which channels' publishers are connected to
|
||||
it (`shade_live_publishers`, refreshed every 10 s). An instance whose viewers want a
|
||||
@@ -450,6 +481,15 @@ done in v1. A deployment that needs it can pad above this layer.
|
||||
byte for byte (`MediaVectorTest`).
|
||||
- `bench/media.ts`: throughput, seek latency and live fan-out, written as
|
||||
`bench/media.chart.json`.
|
||||
- `bench/media-store.ts`: disk store against S3 (+ Postgres) through the real routes,
|
||||
each relay in its own process. Upload and read speed, seek latency, and the relay's
|
||||
peak memory at two film sizes, median or worst of 3 runs
|
||||
(`bench/media-store.chart.json`).
|
||||
- `tests/upload.test.ts`: uploads in pieces on the memory and disk stores. Covers
|
||||
dropped connections before and after the relay stored a piece, a restart with a
|
||||
piece cut off mid-write, pieces out of order and twice at once, the window, signatures
|
||||
per offset, a bad footer at the last piece, quota and sweep. `object-store.test.ts`
|
||||
covers the same over S3 across two instances.
|
||||
|
||||
## Design notes
|
||||
|
||||
@@ -477,14 +517,19 @@ done in v1. A deployment that needs it can pad above this layer.
|
||||
the encoder wrote. CENC-encrypted tracks (`encv`/`enca`) are refused; Shade already
|
||||
encrypts, and a second DRM layer would leave the viewer without keys.
|
||||
- **Bun 1.3.5.** `new Response(Bun.file(p).slice(a, b))` sent the whole file in
|
||||
answer to a Range request when the file was large. The store therefore reads the
|
||||
range itself.
|
||||
answer to a Range request when the file was large, so the store reads the range
|
||||
itself. `fetch(url, { body: Bun.file(p).slice(a, b) })` never finishes sending, so
|
||||
the client reads each piece itself. And Bun.serve buffers a request body without
|
||||
limit when the handler reads it slower than the client sends, which is why uploads
|
||||
go up in pieces.
|
||||
|
||||
## Limits (defaults)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| sealed file | 64 GiB per object, 256 GiB per owner (`MediaRoutesOptions`) |
|
||||
| sealed file | 64 GiB per object, 256 GiB per owner, uploads under way included (`MediaRoutesOptions`) |
|
||||
| one request | 64 MiB (`MAX_PIECE_BYTES`): a file larger than that goes up in pieces of 16 MiB (more for files over 156 GiB), 4 in flight |
|
||||
| abandoned upload | dropped after 24 h without a new piece |
|
||||
| metadata | 64 KiB |
|
||||
| live frame payload | 16 MiB |
|
||||
| live backlog | 3 keyframes, at most 32 MiB / 5000 frames per channel |
|
||||
|
||||
Reference in New Issue
Block a user