Upload large layers in chunks
A registry's ingress can cut off one long request; moonbit-orbit needed a separate uploader for that. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4271a084b6
commit
c8d7f59633
3 files changed
+154
No files matched your search
@@ -49,6 +49,8 @@ text and **Documentation** link. Use a public or otherwise accessible HTTP(S)
|
||||
URL for the link. Gitea does not render a bundled README inline, but you can
|
||||
ship one under `files/share/doc/<name>/README.md`, as the `greet` example does.
|
||||
|
||||
Layers over 4 MiB are uploaded in 4 MiB pieces, so a proxy's limit on one request doesn't stop a large package.
|
||||
|
||||
Because these are ordinary OCI artifacts, you can sign them with `cosign` or `notation` and attach SBOMs using the registry's own tooling.
|
||||
|
||||
### Channel index
|
||||
|
||||
@@ -623,6 +623,92 @@ async fn upload_blob_bytes(
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Files larger than this are uploaded in pieces of this size. Registries and
|
||||
/// their proxies limit the length of one request; GHCR accepts 4 MiB per piece.
|
||||
const UPLOAD_CHUNK : Int32 = 4194304
|
||||
|
||||
///|
|
||||
/// The URL for the next step of an upload. It must stay on the registry's own
|
||||
/// origin, so that the token and the data don't go to another host.
|
||||
fn next_upload_url(
|
||||
url : String,
|
||||
location : String?,
|
||||
) -> String raise OrbitError {
|
||||
match location {
|
||||
Some(loc) => {
|
||||
let cur = parse_url(url)
|
||||
let next = resolve_location(cur, loc)
|
||||
if !same_origin(cur, next) {
|
||||
raise OrbitError("unexpected upload location on \{next.host}")
|
||||
}
|
||||
next.scheme + "://" + next.host + next.path
|
||||
}
|
||||
None => url
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Upload a file in pieces. Returns false if the registry refuses the first
|
||||
/// piece as unsupported, so the caller can send the file in one request.
|
||||
async fn upload_chunks(
|
||||
base : String,
|
||||
repo : String,
|
||||
digest : String,
|
||||
path : String,
|
||||
size : Int64,
|
||||
chunk : Int32,
|
||||
) -> Bool raise OrbitError {
|
||||
let mut url = start_blob_upload(base, repo)
|
||||
let file = @fs.open(path, mode=ReadOnly) catch {
|
||||
e => raise OrbitError("cannot open \{path}: \{e}")
|
||||
}
|
||||
defer file.close()
|
||||
let mut sent = 0L
|
||||
while sent < size {
|
||||
let left = size - sent
|
||||
let want = if left < chunk.to_int64() { left.to_int() } else { chunk }
|
||||
let data = file.read_exactly(want) catch {
|
||||
e => raise OrbitError("cannot read \{path}: \{e}")
|
||||
}
|
||||
let last = sent + want.to_int64() - 1L
|
||||
let (resp, body) = oci_request(
|
||||
Patch,
|
||||
url,
|
||||
Map([
|
||||
("Content-Type", "application/octet-stream"),
|
||||
("Content-Range", "\{sent}-\{last}"),
|
||||
]),
|
||||
data,
|
||||
repo + ":push",
|
||||
)
|
||||
if sent == 0L &&
|
||||
(resp.code == 404 || resp.code == 405 || resp.code == 501) {
|
||||
return false
|
||||
}
|
||||
if resp.code < 200 || resp.code >= 300 {
|
||||
raise OrbitError(
|
||||
"layer upload failed (\{format_oci_error(resp.code, resp.reason, body)})",
|
||||
)
|
||||
}
|
||||
url = next_upload_url(url, resp.headers.get(hdr("Location")))
|
||||
sent = last + 1L
|
||||
}
|
||||
let (resp, body) = oci_request(
|
||||
Put,
|
||||
append_digest_param(url, digest),
|
||||
Map([("Content-Type", "application/octet-stream")]),
|
||||
b"",
|
||||
repo + ":push",
|
||||
)
|
||||
if resp.code < 200 || resp.code >= 300 {
|
||||
raise OrbitError(
|
||||
"layer upload failed (\{format_oci_error(resp.code, resp.reason, body)})",
|
||||
)
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
///|
|
||||
async fn upload_blob_file(
|
||||
base : String,
|
||||
@@ -630,10 +716,15 @@ async fn upload_blob_file(
|
||||
digest : String,
|
||||
path : String,
|
||||
size : Int64,
|
||||
chunk? : Int32 = UPLOAD_CHUNK,
|
||||
) -> Unit raise OrbitError {
|
||||
if blob_exists(base, repo, digest) {
|
||||
return
|
||||
}
|
||||
if size > chunk.to_int64() &&
|
||||
upload_chunks(base, repo, digest, path, size, chunk) {
|
||||
return
|
||||
}
|
||||
let upload_url = append_digest_param(start_blob_upload(base, repo), digest)
|
||||
let (resp, body) = oci_upload_file(
|
||||
upload_url,
|
||||
|
||||
@@ -1016,3 +1016,64 @@ async test "each channel has its own index, kept by publish and used by search"
|
||||
assert_eq(names(a), ["four", "one"])
|
||||
})
|
||||
}
|
||||
|
||||
///|
|
||||
async test "large layers upload in chunks, or whole where that is unsupported" {
|
||||
token_cache.clear()
|
||||
let work = @fs.tmpdir(prefix="orbit-wb-chunk-")
|
||||
defer (@fs.rmdir(work, recursive=true, allow_missing=true) catch { _ => () })
|
||||
let path = work + "/layer"
|
||||
@fs.write_file(path, "0123456789", create_mode=CreateOrTruncate)
|
||||
let (digest, size) = sha256_file(path)
|
||||
let ranges : Array[String] = []
|
||||
let bodies : Array[String] = []
|
||||
let supported = Ref(true)
|
||||
let server = @http.Server(@socket.Addr::parse("127.0.0.1:0"))
|
||||
@async.with_task_group(group => {
|
||||
defer server.close()
|
||||
group.spawn_bg(no_wait=true, () => {
|
||||
server.run_forever((req, body, conn) => {
|
||||
let text = utf8_decode(body.read_all().binary())
|
||||
if req.meth is Post {
|
||||
conn.send_response(202, "Accepted", extra_headers={
|
||||
"Location": "/v2/p/x/blobs/uploads/1",
|
||||
})
|
||||
} else if req.meth is Patch && supported.val {
|
||||
ranges.push(req.headers.get(hdr("Content-Range")).unwrap_or(""))
|
||||
bodies.push(text)
|
||||
conn.send_response(202, "Accepted", extra_headers={
|
||||
"Location": "/v2/p/x/blobs/uploads/1?n=" +
|
||||
bodies.length().to_string(),
|
||||
})
|
||||
} else if req.meth is Put {
|
||||
bodies.push(req.path + " " + text)
|
||||
conn.send_response(201, "Created")
|
||||
} else {
|
||||
conn.send_response(404, "Not Found")
|
||||
}
|
||||
conn.end_response()
|
||||
})
|
||||
})
|
||||
let base = "http://127.0.0.1:" + server.addr.port().to_string()
|
||||
let enc = @percent.encode(digest)
|
||||
upload_blob_file(base, "p/x", digest, path, size, chunk=4)
|
||||
assert_eq(ranges, ["0-3", "4-7", "8-9"])
|
||||
assert_eq(bodies, [
|
||||
"0123",
|
||||
"4567",
|
||||
"89",
|
||||
"/v2/p/x/blobs/uploads/1?n=3&digest=" + enc + " ",
|
||||
])
|
||||
bodies.clear()
|
||||
supported.val = false
|
||||
upload_blob_file(base, "p/x", digest, path, size, chunk=4)
|
||||
assert_eq(bodies, ["/v2/p/x/blobs/uploads/1?digest=" + enc + " 0123456789"])
|
||||
})
|
||||
let other = try {
|
||||
ignore(next_upload_url("https://a/v2/x", Some("https://b/v2/x")))
|
||||
false
|
||||
} catch {
|
||||
OrbitError(_) => true
|
||||
}
|
||||
assert_true(other)
|
||||
}
|
||||
Reference in new issue
Block a user