FIX: Stream S3 job-file downloads instead of buffering into memory#125
Merged
Conversation
GET /api/v1/job/{id}/{file} buffered the entire S3 object into a []byte
via ReadFromS3 before writing it to the response. This was the #1
allocation site in production pprof profiling (31-54% of heap alloc),
hit on every download of a job log/result stored in S3. The handler now
streams the S3 body directly to the response via io.Copy, setting
Content-Length from the object's metadata.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Wiz Scan Summary
To detect these findings earlier in the dev lifecycle, try using Wiz Code VS Code Extension. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces memory pressure in the job-file download endpoint by switching S3-backed artifact downloads from “read whole object into memory” to streaming the S3 response body directly to the HTTP client.
Changes:
- Added
aws.GetS3ObjectReaderto return an S3 objectio.ReadCloserplus content length (streaming API). - Updated
getJobFileto stream S3-backed job files viaio.Copy, settingContent-Lengthwhen available. - Kept the local-disk path (
os.ReadFile) unchanged.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/pkg/heimdall/job.go | Streams S3-backed job artifacts directly to the HTTP response instead of buffering in memory. |
| internal/pkg/aws/s3.go | Introduces a streaming S3 object reader helper returning body + content length. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Per Copilot review on #125: the 200 status is written before streaming starts, so a mid-stream S3 read failure would previously go unnoticed server-side while the client silently got a truncated body. Now logs and counts the error via telemetry. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Instead of two near-duplicate functions (buffered ReadFromS3 and streaming GetS3ObjectReader), ReadFromS3 itself now returns a streaming reader + content length. Its one remaining []byte caller, glue.GetTableMetadata, reads the body fully at the call site since it genuinely needs the bytes (a small JSON metadata file). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Yash Shrivastava (alephys26)
approved these changes
Jul 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GET /api/v1/job/{id}/{file}buffered the entire S3-backed job log/result file into a[]byte(aws.ReadFromS3) before writing it to the HTTP response. This was the Let's go #1 allocation site in production pprof profiling (31-54% of per-sample heap allocation), hit on every download of an S3-stored job artifact.aws.ReadFromS3now returns the object body as a streamingio.ReadCloserplus its content length, instead of fully buffering it into memory. Its one other caller,glue.GetTableMetadata, reads the body fully at the call site since it genuinely needs the bytes (a small JSON metadata file) -- no behavior change there.getJobFilestreams S3-backed files directly to the response viaio.Copy, settingContent-Lengthfrom the object's metadata, and records (via telemetry) if the copy fails mid-stream after the200has already been written. The local-disk (os.ReadFile) path is unchanged.This is slice 1 of a larger set of buffered-reading fixes found via pprof analysis. Remaining slices (Trino stream-decode, Avro stream read, pprof CPU-profile registration, payload size cap, Ranger client cleanup, StarRocks streaming) will follow separately.
Testing
go build ./...,go vet, and the fullgo test ./internal/... ./pkg/...suite pass on this branch.Test (against archived job):
🤖 Generated with Claude Code