diff --git a/crates/moonwell-deploy/src/storage.rs b/crates/moonwell-deploy/src/storage.rs index 2de929a6d..21a36697c 100644 --- a/crates/moonwell-deploy/src/storage.rs +++ b/crates/moonwell-deploy/src/storage.rs @@ -24,6 +24,8 @@ pub struct S3Store { const PART_BYTES: usize = s3::bucket::CHUNK_SIZE; const UPLOAD_WORKERS: usize = 4; +/// How many times one multipart part is sent before the upload fails. +const PART_ATTEMPTS: u32 = 6; fn read_part(reader: &mut impl Read) -> Result> { let mut bytes = Vec::with_capacity(PART_BYTES); @@ -141,14 +143,32 @@ impl S3Store { let upload_id = &upload.upload_id; handles.push(scope.spawn(move || { let expected = format!("{:x}", md5::compute(&bytes)); - let part = self - .bucket - .put_multipart_chunk(&bytes, key, number, upload_id, content_type) - .map_err(|_| { - anyhow::anyhow!( - "S3 multipart upload failed for {key}, part {number}" - ) - })?; + // A part that fails is sent again, so one dropped connection does + // not restart a gigabyte file from its first part. + let mut attempt = 0; + let part = loop { + attempt += 1; + match self.bucket.put_multipart_chunk( + &bytes, + key, + number, + upload_id, + content_type, + ) { + Ok(part) => break part, + Err(e) if attempt < PART_ATTEMPTS => { + eprintln!( + "S3 part {number} of {key}, attempt {attempt}: {e}; retrying" + ); + std::thread::sleep(std::time::Duration::from_secs( + 2u64 << attempt, + )); + } + Err(e) => anyhow::bail!( + "S3 multipart upload failed for {key}, part {number}: {e}" + ), + } + }; ensure!( part.etag.trim_matches('"') == expected, "S3 part checksum mismatch for {key}, part {number}"