From fa84348ae300b703e960391a80a24a0a4b1b0447 Mon Sep 17 00:00:00 2001 From: Igor Shishkin Date: Sat, 4 Jan 2025 07:48:58 +0300 Subject: [PATCH] Wrap lazyblob Reader call into a function (#305) This would make it called only when needed by consumer but exactly always when Reader is obtained so will make possible to reduce amount of object downloads performed just to get its MIME type or calculate a checksum. Signed-off-by: Igor Shishkin --- cli/service/service.go | 7 ++++++- cli/service/source/local/local.go | 6 ++++-- cli/service/source/source.go | 2 +- cli/service/source/yum/yum.go | 13 +++++-------- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cli/service/service.go b/cli/service/service.go index 860b08a..f1056db 100644 --- a/cli/service/service.go +++ b/cli/service/service.go @@ -371,7 +371,12 @@ func (s *service) createObject(ctx context.Context, namespaceName, containerName "url": url, }).Info("uploading BLOB ...") - if err := uploadBlob(ctx, url, object.Contents, object.Size); err != nil { + rd, err := object.Contents(ctx) + if err != nil { + return err + } + + if err := uploadBlob(ctx, url, rd, object.Size); err != nil { return err } } diff --git a/cli/service/source/local/local.go b/cli/service/source/local/local.go index 7883aa8..b15ce2b 100644 --- a/cli/service/source/local/local.go +++ b/cli/service/source/local/local.go @@ -114,8 +114,10 @@ func (l *local) Process(ctx context.Context, handler func(ctx context.Context, o defer fp.Close() if err := handler(ctx, source.Object{ - Path: shortPath, - Contents: fp, + Path: shortPath, + Contents: func(ctx context.Context) (io.Reader, error) { + return fp, nil + }, SHA256: checksum, Size: uint64(size), MimeType: mimeType, diff --git a/cli/service/source/source.go b/cli/service/source/source.go index 2186a95..b199a08 100644 --- a/cli/service/source/source.go +++ b/cli/service/source/source.go @@ -7,7 +7,7 @@ import ( type Object struct { Path string - Contents io.Reader + Contents func(ctx context.Context) (io.Reader, error) SHA256 string Size uint64 MimeType string diff --git a/cli/service/source/yum/yum.go b/cli/service/source/yum/yum.go index c3e5fdd..b57042f 100644 --- a/cli/service/source/yum/yum.go +++ b/cli/service/source/yum/yum.go @@ -92,8 +92,10 @@ func (r *repository) Process(ctx context.Context, handler func(ctx context.Conte log.Tracef("handler(%s, %s, %d)", k, checksum, size) if err := handler(ctx, source.Object{ - Path: k, - Contents: bytes.NewReader(v), + Path: k, + Contents: func(ctx context.Context) (io.Reader, error) { + return bytes.NewReader(v), nil + }, SHA256: checksum, Size: uint64(size), MimeType: mimeType, @@ -160,14 +162,9 @@ func (r *repository) Process(ctx context.Context, handler func(ctx context.Conte } mimeType := mimetype.Detect(data) - fp, err = lb.Reader(ctx) - if err != nil { - return errors.Wrap(err, "error getting object reader") - } - if err := handler(ctx, source.Object{ Path: name, - Contents: fp, + Contents: lb.Reader, SHA256: checksum, Size: size, MimeType: mimeType.String(),