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

storage: Use 1M buffers for Tar transfers #7681

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions extern/sector-storage/stores/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package stores

import (
"encoding/json"
"io"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -139,17 +138,12 @@ func (handler *FetchHandler) remoteGetSector(w http.ResponseWriter, r *http.Requ
return
}

rd, err := tarutil.TarDirectory(path)
if err != nil {
log.Errorf("%+v", err)
w.WriteHeader(500)
return
}

w.Header().Set("Content-Type", "application/x-tar")
w.WriteHeader(200)
if _, err := io.CopyBuffer(w, rd, make([]byte, CopyBuf)); err != nil {
log.Errorf("%+v", err)

err := tarutil.TarDirectory(path, w, make([]byte, CopyBuf))
if err != nil {
log.Errorf("send tar: %+v", err)
return
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion extern/sector-storage/stores/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (r *Remote) fetch(ctx context.Context, url, outname string) error {

switch mediatype {
case "application/x-tar":
return tarutil.ExtractTar(resp.Body, outname)
return tarutil.ExtractTar(resp.Body, outname, make([]byte, CopyBuf))
case "application/octet-stream":
f, err := os.Create(outname)
if err != nil {
Expand Down
18 changes: 4 additions & 14 deletions extern/sector-storage/tarutil/systar.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

var log = logging.Logger("tarutil") // nolint

func ExtractTar(body io.Reader, dir string) error {
func ExtractTar(body io.Reader, dir string, buf []byte) error {
if err := os.MkdirAll(dir, 0755); err != nil { // nolint
return xerrors.Errorf("mkdir: %w", err)
}
Expand All @@ -38,7 +38,7 @@ func ExtractTar(body io.Reader, dir string) error {

// This data is coming from a trusted source, no need to check the size.
//nolint:gosec
if _, err := io.Copy(f, tr); err != nil {
if _, err := io.CopyBuffer(f, tr, buf); err != nil {
return err
}

Expand All @@ -48,17 +48,7 @@ func ExtractTar(body io.Reader, dir string) error {
}
}

func TarDirectory(dir string) (io.ReadCloser, error) {
r, w := io.Pipe()

go func() {
_ = w.CloseWithError(writeTarDirectory(dir, w))
}()

return r, nil
}

func writeTarDirectory(dir string, w io.Writer) error {
func TarDirectory(dir string, w io.Writer, buf []byte) error {
tw := tar.NewWriter(w)

files, err := ioutil.ReadDir(dir)
Expand All @@ -81,7 +71,7 @@ func writeTarDirectory(dir string, w io.Writer) error {
return xerrors.Errorf("opening %s for reading: %w", file.Name(), err)
}

if _, err := io.Copy(tw, f); err != nil {
if _, err := io.CopyBuffer(tw, f, buf); err != nil {
return xerrors.Errorf("copy data for file %s: %w", file.Name(), err)
}

Expand Down