Repository navigation
Add HTTP Range / partial-content (resume) support to streaming downloads
#1644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { | |
| ValidateParams | ||
| } from '../../httpRoutes/validateCommands.js' | ||
| import { isAddress } from 'ethers' | ||
| import { ComputeResultError } from '../../c2d/serviceOutputsZip.js' | ||
|
|
||
| export class ComputeGetResultHandler extends CommandHandler { | ||
| validate(command: ComputeGetResultCommand): ValidateParams { | ||
|
|
@@ -21,6 +22,14 @@ export class ComputeGetResultHandler extends CommandHandler { | |
| if (isNaN(command.index) || command.index < 0) { | ||
| return buildInvalidRequestMessage('Invalid result index') | ||
| } | ||
| if ( | ||
| command.offset !== undefined && | ||
| (!Number.isSafeInteger(command.offset) || command.offset < 0) | ||
| ) { | ||
| return buildInvalidRequestMessage( | ||
| 'Invalid parameter: "offset" must be a non-negative integer' | ||
| ) | ||
| } | ||
| } | ||
| return validation | ||
| } | ||
|
|
@@ -77,10 +86,27 @@ export class ComputeGetResultHandler extends CommandHandler { | |
| // need to pass the headers properly | ||
| if (respStream?.headers) { | ||
| response.status.headers = respStream?.headers | ||
| // A partial result carries a Content-Range: answer 206 here (not only in the HTTP | ||
| // route) so P2P and /directCommand callers see the partial status too. | ||
| if (respStream.headers['Content-Range']) { | ||
| response.status.httpStatus = 206 | ||
|
Comment on lines
+91
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forward the body for 206 responses in
Accept 206 as a successful status in the gate: Suggested fix
Callers that treat 206 as a failure must still consume or abort the returned stream. Otherwise the stream leaks, as the existing comment in this block describes.
|
||
| } | ||
| } | ||
| return response | ||
| } catch (error) { | ||
| CORE_LOGGER.error(`getComputeResult ${task.jobId}: ${error.message}`) | ||
| // A range past the end of the result is a client error (416), typed by the engine so | ||
| // it isn't confused with a genuine 500; it carries the size for a Content-Range header. | ||
| if (error instanceof ComputeResultError) { | ||
| const headers = | ||
| error.httpStatus === 416 && typeof error.size === 'number' | ||
| ? { 'Accept-Ranges': 'bytes', 'Content-Range': `bytes */${error.size}` } | ||
| : undefined | ||
| return { | ||
| stream: null, | ||
| status: { httpStatus: error.httpStatus, error: error.message, headers } | ||
| } | ||
| } | ||
| return { | ||
| stream: null, | ||
| status: { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,7 +45,12 @@ import { | |
| ServiceGetResultHandler | ||
| } from '../core/service/index.js' | ||
|
|
||
| import { streamToObject, streamToString, sanitizeLogValue } from '../../utils/util.js' | ||
| import { | ||
| streamToObject, | ||
| streamToString, | ||
| sanitizeLogValue, | ||
| resolveDownloadOffset | ||
| } from '../../utils/util.js' | ||
| import { PROTOCOL_COMMANDS, SERVICES_API_BASE_PATH } from '../../utils/constants.js' | ||
| import { Readable, pipeline } from 'stream' | ||
| import { HTTP_LOGGER } from '../../utils/logging/common.js' | ||
|
|
@@ -288,6 +293,16 @@ computeRoutes.get(`${SERVICES_API_BASE_PATH}/computeResult`, async (req, res) => | |
| HTTP_LOGGER.debug( | ||
| `computeResult request: job=${sanitizeLogValue(req.query.jobId)} index=${sanitizeLogValue(req.query.index ?? '-')}` | ||
| ) | ||
| // Resume support: a valid `Range: bytes=<offset>-` wins; an explicit but malformed | ||
| // `?offset=` is a 400 (not a silent full download). | ||
| const { offset, error: offsetError } = resolveDownloadOffset( | ||
| req.headers.range, | ||
| req.query.offset | ||
| ) | ||
| if (offsetError) { | ||
| res.status(400).send(offsetError) | ||
| return | ||
| } | ||
| const resultComputeTask: ComputeGetResultCommand = { | ||
| command: PROTOCOL_COMMANDS.COMPUTE_GET_RESULT, | ||
| node: (req.query.node as string) || null, | ||
|
|
@@ -296,6 +311,7 @@ computeRoutes.get(`${SERVICES_API_BASE_PATH}/computeResult`, async (req, res) => | |
| jobId: (req.query.jobId as string) || null, | ||
| signature: (req.query.signature as string) || null, | ||
| nonce: (req.query.nonce as string) || null, | ||
| offset, | ||
| authorization: req.headers?.authorization, | ||
| caller: req.caller | ||
| } | ||
|
|
@@ -304,10 +320,34 @@ computeRoutes.get(`${SERVICES_API_BASE_PATH}/computeResult`, async (req, res) => | |
| resultComputeTask | ||
| ) | ||
| if (response.stream) { | ||
| // The handler already set 206 when the result is partial (Content-Range present). | ||
| res.status(response.status.httpStatus) | ||
| res.set(response.status.headers) | ||
| response.stream.pipe(res) | ||
| if (response.status.headers) { | ||
| res.set(response.status.headers) | ||
| } | ||
| const sourceStream = response.stream as Readable | ||
| // pipe() does not forward source errors: guard a mid-flight read error (e.g. the output | ||
| // file is removed/truncated after stat) so it can't crash the process, and tear the | ||
| // source down if the client disconnects, to avoid leaking the file handle. | ||
| sourceStream.on('error', (err) => { | ||
| HTTP_LOGGER.error(`computeResult stream error: ${errorSummary(err)}`) | ||
| if (!res.headersSent) { | ||
| res.removeHeader('Content-Range') | ||
| res.status(500).send('Internal Server Error') | ||
| } else { | ||
| res.end() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the read fails mid-stream,
|
||
| } | ||
| }) | ||
| res.on('close', () => { | ||
| if (!res.writableEnded) { | ||
| sourceStream.destroy() | ||
| } | ||
| }) | ||
| sourceStream.pipe(res) | ||
| } else { | ||
| if (response.status.headers) { | ||
| res.set(response.status.headers) | ||
| } | ||
| res.status(response.status.httpStatus).send(response.status.error) | ||
| } | ||
| } catch (error) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.