Skip to content

Commit

Permalink
Merge pull request #58 from infosiftr/lookup-by-digest
Browse files Browse the repository at this point in the history
Ensure our pre-flight HEAD request is always by-tag if we're pushing a tag
  • Loading branch information
tianon authored Jun 26, 2024
2 parents cab9251 + 4473698 commit d85d365
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions registry/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var (
// if a blob is more than this many bytes, we'll do a pre-flight HEAD request to verify whether we need to even bother pushing it before we do so (65535 is the theoretical maximum size of a single TCP packet, although MTU means it's usually closer to 1448 bytes, but this seemed like a sane place to draw a line to where a second request that might fail is worth our time)
// if a manifest or blob is more than this many bytes, we'll do a pre-flight HEAD request to verify whether we need to even bother pushing it before we do so (65535 is the theoretical maximum size of a single TCP packet, although MTU means it's usually closer to 1448 bytes, but this seemed like a sane place to draw a line to where a second request that might fail is worth our time)
BlobSizeWorthHEAD = int64(65535)
)

Expand Down Expand Up @@ -43,18 +43,25 @@ func EnsureManifest(ctx context.Context, ref Reference, manifest json.RawMessage
return desc, fmt.Errorf("%s: failed getting client: %w", ref, err)
}

// try HEAD request before pushing
// if it matches, then we can assume child objects exist as well
r, err := Lookup(ctx, ref, &LookupOptions{Head: true})
if err != nil {
return desc, fmt.Errorf("%s: failed HEAD: %w", ref, err)
}
// TODO if we had some kind of progress interface, this would be a great place for some kind of debug log of head's contents
if r != nil {
head := r.Descriptor()
r.Close()
if head.Digest == desc.Digest && head.Size == desc.Size {
return head, nil
if desc.Size > BlobSizeWorthHEAD {
// try HEAD request before pushing
// if it matches, then we can assume child objects exist as well
headRef := ref
if headRef.Tag != "" {
// if this function is called with *both* tag *and* digest, the code below works correctly and pushes by tag and then validates by digest, but this lookup specifically will prefer the digest instead and skip when it shouldn't
headRef.Digest = ""
}
r, err := Lookup(ctx, headRef, &LookupOptions{Head: true})
if err != nil {
return desc, fmt.Errorf("%s: failed HEAD: %w", ref, err)
}
// TODO if we had some kind of progress interface, this would be a great place for some kind of debug log of head's contents
if r != nil {
head := r.Descriptor()
r.Close()
if head.Digest == desc.Digest && head.Size == desc.Size {
return head, nil
}
}
}

Expand Down

0 comments on commit d85d365

Please sign in to comment.