Skip to content
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

Switch reading from S3 to io.Copy from io.ReadFull #4225

Merged
merged 2 commits into from
Mar 30, 2018
Merged

Conversation

jefferai
Copy link
Member

If the Content-Length header wasn't being sent back, the current
behavior could panic. It's unclear when it will not be sent; it appears
to be CORS dependent. But this works around it by not trying to
preallocate a buffer of a specific size and instead just read until EOF.

In addition I noticed that Close wasn't being called.
https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#GetObjectOutput
specifies that Body is an io.ReadCloser so I added a call to Close.

Fixes #4222

If the Content-Length header wasn't being sent back, the current
behavior could panic. It's unclear when it will not be sent; it appears
to be CORS dependent. But this works around it by not trying to
preallocate a buffer of a specific size and instead just read until EOF.

In addition I noticed that Close wasn't being called.
https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#GetObjectOutput
specifies that Body is an io.ReadCloser so I added a call to Close.

Fixes #4222
@jefferai jefferai added this to the 0.10 milestone Mar 30, 2018
data := make([]byte, *resp.ContentLength)
_, err = io.ReadFull(resp.Body, data)
data := bytes.NewBuffer(nil)
_, err = io.Copy(data, bytes.NewBuffer(resp.Body))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should just be resp.Body w/o the bytes.NewBuffer()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah good point, I did that before looking up whether it was a byte slice or reader and forgot to go back and change it.


data := make([]byte, *resp.ContentLength)
_, err = io.ReadFull(resp.Body, data)
data := bytes.NewBuffer(nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To retain the more efficient pre-allocation when ContentLength is known (presumably the normal case), consider:

	var cap int64
	if resp.ContentLength != nil {
		cap = *resp.ContentLength
	}

	data := bytes.NewBuffer(make([]byte, 0, cap))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at that as a "efficiency doesn't really matter because network aspects will dominate the total time" but I guess if it's a 20mb object it actually could, so I'll do that.

@jefferai
Copy link
Member Author

@kalafut Fixed, can you ✔️ ?

Copy link
Contributor

@kalafut kalafut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jefferai jefferai merged commit 82b493a into master Mar 30, 2018
@jefferai jefferai deleted the issue-4222 branch March 30, 2018 16:42
@@ -188,16 +188,20 @@ func (s *S3Backend) Get(ctx context.Context, key string) (*physical.Entry, error
if resp == nil {
return nil, fmt.Errorf("got nil response from S3 but no error")
}
defer resp.Body.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might it be more defensive to do this right before line 178? Something like, if resp != nil { defer resp.Body.Close() }? Then there'd be absolutely no way there could be a memory leak if we hit 404's a lot, and if for some reason the AWS client still returned a body. I know for the http.Client that doesn't happen, but I don't know if the AWS client maintains the same contract. I can't find anything saying either way in their docs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, considering we're thinking of returning data with 404s. I'll update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants