Skip to content

Commit e7cb8c5

Browse files
committed
Only count bytes added to the cache
1 parent 53fd3ea commit e7cb8c5

5 files changed

Lines changed: 32 additions & 26 deletions

File tree

‎src/any_cache.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Cache for AnyCache {
6666
nar_file_name: NarFileName,
6767
nar_size: NonZeroU64,
6868
nar_bytes: impl AsyncRead + Send + 'static,
69-
) -> Result<(), Self::Error> {
69+
) -> Result<u64, Self::Error> {
7070
match self {
7171
Self::Noop(cache) => cache
7272
.upload_nar(nar_file_name, nar_size, nar_bytes)

‎src/context.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ pub trait Cache: Clone + Send + Sync + 'static {
4545
narinfo_filename: &NarInfoFileName,
4646
) -> impl Future<Output = Result<bool, Self::Error>> + Send;
4747

48+
/// Uploads a NAR and returns the number of bytes added to the cache. Returns
49+
/// zero if an equivalent NAR was already present.
4850
fn upload_nar(
4951
&self,
5052
nar_file_name: NarFileName,
5153
nar_size: NonZeroU64,
5254
nar_bytes: impl AsyncRead + Send + 'static,
53-
) -> impl Future<Output = Result<(), Self::Error>> + Send;
55+
) -> impl Future<Output = Result<u64, Self::Error>> + Send;
5456

57+
/// Uploads a NARInfo and returns the number of bytes added to the cache.
58+
/// Returns zero if an equivalent NARInfo was already present.
5559
fn upload_narinfo(
5660
&self,
5761
narinfo_filename: NarInfoFileName,

‎src/event_loop.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ async fn handle_store_path<C: Cache, H: HttpClient, N: Nix>(
362362
// Just like `nix copy --to`, we write the NAR *before* the NARInfo to avoid
363363
// the cache server temporarily reporting false positives.
364364

365-
cache
365+
let nar_size_pushed = cache
366366
.upload_nar(nar_file_name, file_size, nar_buf)
367367
.await
368368
.map_err(HandlePathError::UploadNar)?;
@@ -389,7 +389,7 @@ async fn handle_store_path<C: Cache, H: HttpClient, N: Nix>(
389389
.await
390390
.map_err(HandlePathError::UploadNarInfo)?;
391391

392-
Ok(Some(file_size.saturating_add(narinfo_size)))
392+
Ok(NonZeroU64::new(nar_size_pushed.saturating_add(narinfo_size)))
393393
}
394394

395395
async fn should_push_store_path<C: Cache, H: HttpClient>(

‎src/noop_cache.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ impl Cache for NoopCache {
2525
_: NarFileName,
2626
_: NonZeroU64,
2727
nar_bytes: impl AsyncRead + Send + 'static,
28-
) -> Result<(), Self::Error> {
28+
) -> Result<u64, Self::Error> {
2929
let mut sink = futures::io::sink();
30-
futures::io::copy(nar_bytes, &mut sink).await.expect("never fails");
31-
Ok(())
30+
Ok(futures::io::copy(nar_bytes, &mut sink).await.expect("never fails"))
3231
}
3332

3433
async fn upload_narinfo(

‎src/sub60_cache.rs‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl Sub60Cache {
189189
nar_file_name: NarFileName,
190190
upload_id: &UploadId,
191191
etags: SmallVec<[Etag; 8]>,
192-
) -> Result<(), CacheRequestError> {
192+
) -> Result<bool, CacheRequestError> {
193193
let url = self.nar_multiparts_complete_url(upload_id);
194194

195195
let response = self
@@ -203,15 +203,15 @@ impl Sub60Cache {
203203
source,
204204
})?;
205205

206-
if response.status().is_success() {
207-
Ok(())
208-
} else {
209-
Err(CacheRequestError::unexpected_response(
206+
match response.status() {
207+
StatusCode::CREATED => Ok(true),
208+
StatusCode::OK => Ok(false),
209+
_ => Err(CacheRequestError::unexpected_response(
210210
response,
211211
Method::POST,
212212
url,
213213
)
214-
.await)
214+
.await),
215215
}
216216
}
217217

@@ -480,22 +480,25 @@ impl context::Cache for Sub60Cache {
480480
async fn upload_nar(
481481
&self,
482482
nar_file_name: NarFileName,
483-
_nar_size: NonZeroU64,
483+
nar_size: NonZeroU64,
484484
nar_bytes: impl AsyncRead + Send + 'static,
485-
) -> Result<(), Self::Error> {
485+
) -> Result<u64, Self::Error> {
486486
let initiate_body =
487487
self.initiate_multipart_nar_upload(&nar_file_name).await?;
488488

489489
let etags = self
490490
.upload_nar_multiparts(&nar_file_name, &initiate_body, nar_bytes)
491491
.await?;
492492

493-
self.complete_multipart_nar_upload(
494-
nar_file_name,
495-
&initiate_body.upload_id,
496-
etags,
497-
)
498-
.await
493+
let did_insert = self
494+
.complete_multipart_nar_upload(
495+
nar_file_name,
496+
&initiate_body.upload_id,
497+
etags,
498+
)
499+
.await?;
500+
501+
Ok(if did_insert { nar_size.get() } else { 0 })
499502
}
500503

501504
async fn upload_narinfo(
@@ -517,15 +520,15 @@ impl context::Cache for Sub60Cache {
517520
},
518521
)?;
519522

520-
if response.status().is_success() {
521-
Ok(narinfo_size)
522-
} else {
523-
Err(CacheRequestError::unexpected_response(
523+
match response.status() {
524+
StatusCode::CREATED => Ok(narinfo_size),
525+
StatusCode::OK => Ok(0),
526+
_ => Err(CacheRequestError::unexpected_response(
524527
response,
525528
Method::PUT,
526529
url,
527530
)
528-
.await)
531+
.await),
529532
}
530533
}
531534
}

0 commit comments

Comments
 (0)