From e7a6ce214b39b95bb470b2ca644f508260fae424 Mon Sep 17 00:00:00 2001 From: Ryan Hileman Date: Fri, 25 Oct 2024 22:46:53 -0700 Subject: [PATCH] feat: make zero-copy GetObject possible via sendfile From #919, This provides the *os.File handle to io.Copy() for the case where the full file range is requested. This prevents hiding the *os.File type for io.Copy() optimizations. This still requires the change to valyala/fasthttp#1889 to expose the net.Conn similarly to enable the linux sendfile optimization. --- backend/posix/posix.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/posix/posix.go b/backend/posix/posix.go index 78fe2d4..787e8c4 100644 --- a/backend/posix/posix.go +++ b/backend/posix/posix.go @@ -2931,7 +2931,12 @@ func (p *Posix) GetObject(_ context.Context, input *s3.GetObjectInput) (*s3.GetO return nil, fmt.Errorf("open object: %w", err) } - rdr := io.NewSectionReader(f, startOffset, length) + // using an os.File allows zero-copy sendfile via io.Copy(os.File, net.Conn) + var body io.ReadCloser = f + if startOffset != 0 || length != objSize { + rdr := io.NewSectionReader(f, startOffset, length) + body = &backend.FileSectionReadCloser{R: rdr, F: f} + } return &s3.GetObjectOutput{ AcceptRanges: &acceptRange, @@ -2945,7 +2950,7 @@ func (p *Posix) GetObject(_ context.Context, input *s3.GetObjectInput) (*s3.GetO ContentRange: &contentRange, StorageClass: types.StorageClassStandard, VersionId: &versionId, - Body: &backend.FileSectionReadCloser{R: rdr, F: f}, + Body: body, }, nil }