refactor(media): upload pieces are PUT, not PATCH
A piece at an offset is idempotent, which is what PUT means. Java's HttpURLConnection, which the Kotlin port uses, refuses PATCH outright. nginx also retries PUT on the next upstream by default but never PATCH, so the docs now only ask for non_idempotent on the start/status POST. The protocol is unreleased, so nothing else depends on the old method. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -67,7 +67,7 @@ New package **`@shade/media`** — see `docs/media.md`.
|
|||||||
`@shade/storage-postgres`. A live pin can no longer change key, even between
|
`@shade/storage-postgres`. A live pin can no longer change key, even between
|
||||||
instances (`LivePinTakenError`).
|
instances (`LivePinTakenError`).
|
||||||
- **Uploads in pieces, resumable.** A sealed file over 16 MiB goes up as
|
- **Uploads in pieces, resumable.** A sealed file over 16 MiB goes up as
|
||||||
`POST /v1/media/:id/upload` + 16 MiB `PATCH` pieces (each signed over its
|
`POST /v1/media/:id/upload` + 16 MiB `PUT` pieces (each signed over its
|
||||||
offset), up to 4 in flight and landing in any order. A dropped connection,
|
offset), up to 4 in flight and landing in any order. A dropped connection,
|
||||||
a relay restart or the next piece landing on another instance costs only the
|
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.
|
pieces in flight: the client asks how far the upload got and sends the rest.
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ Routes: `/v1/media/:id`, `/v1/media/:id/upload`, `/v1/live/:channelId` (WebSocke
|
|||||||
|
|
||||||
A sealed file larger than 16 MiB is not one request. `shade media put` (and
|
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
|
`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
|
id, size and read-token hash) and sends it in pieces of 16 MiB (`PUT`, each signed
|
||||||
over its offset and length), up to 4 at once. The relay keeps what landed on disk or
|
over its offset and length), up to 4 at once. The relay keeps what landed on disk or
|
||||||
in the bucket, so:
|
in the bucket, so:
|
||||||
|
|
||||||
@@ -178,8 +178,9 @@ off and the rest of the server runs.
|
|||||||
signature, so a part damaged in transit is refused. A file sent in one `PUT` goes up
|
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.
|
part by part as it arrives, with at most three 16 MiB parts in memory.
|
||||||
- **Behind a load balancer**, let a request that cannot reach one instance go to the
|
- **Behind a load balancer**, let a request that cannot reach one instance go to the
|
||||||
next. Upload pieces are idempotent (a piece sent twice is a no-op), so this is safe
|
next. A piece is a `PUT` (idempotent: sent twice, the second is a no-op), which
|
||||||
even for their POST/PATCH, which nginx does not retry by default:
|
nginx already retries on the next upstream. The `POST` that starts an upload or asks
|
||||||
|
how far it got is just as safe to repeat, but nginx does not retry it by default:
|
||||||
`location ~ ^/v1/media/[0-9a-f]+/upload$ { proxy_next_upstream error timeout non_idempotent; … }`.
|
`location ~ ^/v1/media/[0-9a-f]+/upload$ { proxy_next_upstream error timeout non_idempotent; … }`.
|
||||||
Without it the client still retries, but a balancer that keeps sending its
|
Without it the client still retries, but a balancer that keeps sending its
|
||||||
retries to a dead instance makes the run stop; running it again resumes.
|
retries to a dead instance makes the run stop; running it again resumes.
|
||||||
|
|||||||
@@ -194,7 +194,7 @@ export class MediaClient {
|
|||||||
const length = Math.min(chunk, size - offset);
|
const length = Math.min(chunk, size - offset);
|
||||||
const piece = await cut(offset, offset + length);
|
const piece = await cut(offset, offset + length);
|
||||||
const res = await send({
|
const res = await send({
|
||||||
method: 'PATCH',
|
method: 'PUT',
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': 'application/octet-stream',
|
'content-type': 'application/octet-stream',
|
||||||
'content-length': String(length),
|
'content-length': String(length),
|
||||||
@@ -256,7 +256,7 @@ export class MediaClient {
|
|||||||
// that failed and was retried): an empty piece at the end finishes it.
|
// that failed and was retried): an empty piece at the end finishes it.
|
||||||
await retrying(async () => {
|
await retrying(async () => {
|
||||||
const res = await send({
|
const res = await send({
|
||||||
method: 'PATCH',
|
method: 'PUT',
|
||||||
headers: { 'content-length': '0', [OFFSET_HEADER]: String(size), ...this.owner.headers({ op: 'piece', id, offset: size, length: 0 }) },
|
headers: { 'content-length': '0', [OFFSET_HEADER]: String(size), ...this.owner.headers({ op: 'piece', id, offset: size, length: 0 }) },
|
||||||
});
|
});
|
||||||
if (res.status !== 201) return fail(res, 'finish');
|
if (res.status !== 201) return fail(res, 'finish');
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* PUT /v1/media/:id signed by the owner; body = a sealed SMF1 file of at most one piece
|
* PUT /v1/media/:id signed by the owner; body = a sealed SMF1 file of at most one piece
|
||||||
* POST /v1/media/:id/upload signed; start a resumable upload, or ask how far it got
|
* POST /v1/media/:id/upload signed; start a resumable upload, or ask how far it got
|
||||||
* PATCH /v1/media/:id/upload signed; one piece of it (see `upload.ts`)
|
* PUT /v1/media/:id/upload signed; one piece of it (see `upload.ts`)
|
||||||
* DELETE /v1/media/:id/upload signed; give up on it
|
* DELETE /v1/media/:id/upload signed; give up on it
|
||||||
* HEAD /v1/media/:id size (read token if the upload set one)
|
* HEAD /v1/media/:id size (read token if the upload set one)
|
||||||
* GET /v1/media/:id Range-capable read of the sealed bytes
|
* GET /v1/media/:id Range-capable read of the sealed bytes
|
||||||
@@ -210,7 +210,7 @@ export function createMediaRoutes(store: MediaStore, opts: MediaRoutesOptions =
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.patch('/v1/media/:id/upload', async (c) => {
|
app.put('/v1/media/:id/upload', async (c) => {
|
||||||
const id = c.req.param('id');
|
const id = c.req.param('id');
|
||||||
if (!ID_RE.test(id)) return c.json({ error: 'BAD_ID' }, 400);
|
if (!ID_RE.test(id)) return c.json({ error: 'BAD_ID' }, 400);
|
||||||
const length = Number(c.req.header('content-length') ?? '0');
|
const length = Number(c.req.header('content-length') ?? '0');
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
* POST /v1/media/:id/upload start, or ask how far an upload got
|
* POST /v1/media/:id/upload start, or ask how far an upload got
|
||||||
* → `{ size, chunk, window, received, landed }`, plus
|
* → `{ size, chunk, window, received, landed }`, plus
|
||||||
* `complete: true` if it was stored (its last answer was lost)
|
* `complete: true` if it was stored (its last answer was lost)
|
||||||
* PATCH /v1/media/:id/upload one piece at `x-shade-offset`
|
* PUT /v1/media/:id/upload one piece at `x-shade-offset`
|
||||||
* → 200 `{ received }`, or 201 once the file is stored
|
* → 200 `{ received }`, or 201 once the file is stored
|
||||||
* DELETE /v1/media/:id/upload give up
|
* DELETE /v1/media/:id/upload give up
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ describe('pieces', () => {
|
|||||||
let patches = 0;
|
let patches = 0;
|
||||||
let sentBytes = 0;
|
let sentBytes = 0;
|
||||||
const flaky: typeof fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
const flaky: typeof fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
if (init?.method === 'PATCH') {
|
if (init?.method === 'PUT') {
|
||||||
patches++;
|
patches++;
|
||||||
// Piece 2, first try: the connection drops before the relay gets it.
|
// Piece 2, first try: the connection drops before the relay gets it.
|
||||||
if (patches === 2) throw new Error('connection reset');
|
if (patches === 2) throw new Error('connection reset');
|
||||||
@@ -117,7 +117,7 @@ describe('pieces', () => {
|
|||||||
});
|
});
|
||||||
const piece = (base: string, offset: number, length: number) =>
|
const piece = (base: string, offset: number, length: number) =>
|
||||||
fetch(`${base}/v1/media/${id}/upload`, {
|
fetch(`${base}/v1/media/${id}/upload`, {
|
||||||
method: 'PATCH',
|
method: 'PUT',
|
||||||
headers: { [OFFSET_HEADER]: String(offset), ...owner.headers({ op: 'piece', id, offset, length }) },
|
headers: { [OFFSET_HEADER]: String(offset), ...owner.headers({ op: 'piece', id, offset, length }) },
|
||||||
body: bytes.slice(offset, offset + length),
|
body: bytes.slice(offset, offset + length),
|
||||||
});
|
});
|
||||||
@@ -156,7 +156,7 @@ describe('pieces', () => {
|
|||||||
const start = () => req('POST', { 'x-shade-size': String(size), ...owner.headers({ op: 'upload', id, size, readTokenHash: '' }) });
|
const start = () => req('POST', { 'x-shade-size': String(size), ...owner.headers({ op: 'upload', id, size, readTokenHash: '' }) });
|
||||||
const piece = (offset: number, length = Math.min(chunk, size - offset), who = owner) =>
|
const piece = (offset: number, length = Math.min(chunk, size - offset), who = owner) =>
|
||||||
req(
|
req(
|
||||||
'PATCH',
|
'PUT',
|
||||||
{ 'content-length': String(length), [OFFSET_HEADER]: String(offset), ...who.headers({ op: 'piece', id, offset, length }) },
|
{ 'content-length': String(length), [OFFSET_HEADER]: String(offset), ...who.headers({ op: 'piece', id, offset, length }) },
|
||||||
bytes.slice(offset, offset + length),
|
bytes.slice(offset, offset + length),
|
||||||
);
|
);
|
||||||
@@ -176,7 +176,7 @@ describe('pieces', () => {
|
|||||||
expect((await piece(0, chunk, OwnerKey.generate())).status).toBe(404); // someone else's upload
|
expect((await piece(0, chunk, OwnerKey.generate())).status).toBe(404); // someone else's upload
|
||||||
// A signature over one offset does not carry another.
|
// A signature over one offset does not carry another.
|
||||||
const replay = await req(
|
const replay = await req(
|
||||||
'PATCH',
|
'PUT',
|
||||||
{ 'content-length': String(chunk), [OFFSET_HEADER]: '0', ...owner.headers({ op: 'piece', id, offset: chunk, length: chunk }) },
|
{ 'content-length': String(chunk), [OFFSET_HEADER]: '0', ...owner.headers({ op: 'piece', id, offset: chunk, length: chunk }) },
|
||||||
bytes.slice(0, chunk),
|
bytes.slice(0, chunk),
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user