Skip to content

Commit

Permalink
Wrap lazyblob Reader call into a function (#305)
Browse files Browse the repository at this point in the history
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 <me@teran.ru>
  • Loading branch information
teran authored Jan 4, 2025
1 parent ea1fc89 commit fa84348
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
7 changes: 6 additions & 1 deletion cli/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
6 changes: 4 additions & 2 deletions cli/service/source/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cli/service/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions cli/service/source/yum/yum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit fa84348

Please sign in to comment.