@@ -327,12 +327,36 @@ pub enum Event {
327
327
/// The hash for which the client wants to receive data.
328
328
hash : Hash ,
329
329
} ,
330
- /// A request was completed and the data was sent to the client .
331
- TransferCompleted {
330
+ /// A collection has been found and is being transferred .
331
+ TransferCollectionStarted {
332
332
/// An unique connection id.
333
333
connection_id : u64 ,
334
334
/// An identifier uniquely identifying this transfer request.
335
335
request_id : u64 ,
336
+ /// The number of blobs in the collection.
337
+ num_blobs : u64 ,
338
+ /// The total blob size of the data.
339
+ total_blobs_size : u64 ,
340
+ } ,
341
+ /// A collection request was completed and the data was sent to the client.
342
+ TransferCollectionCompleted {
343
+ /// An unique connection id.
344
+ connection_id : u64 ,
345
+ /// An identifier uniquely identifying this transfer request.
346
+ request_id : u64 ,
347
+ } ,
348
+ /// A blob in a collection was transferred.
349
+ TransferBlobCompleted {
350
+ /// An unique connection id.
351
+ connection_id : u64 ,
352
+ /// An identifier uniquely identifying this transfer request.
353
+ request_id : u64 ,
354
+ /// The hash of the blob
355
+ hash : Hash ,
356
+ /// The index of the blob in the collection.
357
+ index : u64 ,
358
+ /// The size of the blob transferred.
359
+ size : u64 ,
336
360
} ,
337
361
/// A request was aborted because the client disconnected.
338
362
TransferAborted {
@@ -625,6 +649,7 @@ async fn read_request(mut reader: quinn::RecvStream, buffer: &mut BytesMut) -> R
625
649
/// close the writer, and return with `Ok(SentStatus::NotFound)`.
626
650
///
627
651
/// If the transfer does _not_ end in error, the buffer will be empty and the writer is gracefully closed.
652
+ #[ allow( clippy:: too_many_arguments) ]
628
653
async fn transfer_collection (
629
654
// Database from which to fetch blobs.
630
655
db : & Database ,
@@ -636,6 +661,9 @@ async fn transfer_collection(
636
661
outboard : & Bytes ,
637
662
// The actual blob data.
638
663
data : & Bytes ,
664
+ events : broadcast:: Sender < Event > ,
665
+ connection_id : u64 ,
666
+ request_id : u64 ,
639
667
) -> Result < SentStatus > {
640
668
// We only respond to requests for collections, not individual blobs
641
669
let mut extractor = SliceExtractor :: new_outboard (
@@ -652,6 +680,13 @@ async fn transfer_collection(
652
680
653
681
let c: Collection = postcard:: from_bytes ( data) ?;
654
682
683
+ let _ = events. send ( Event :: TransferCollectionStarted {
684
+ connection_id,
685
+ request_id,
686
+ num_blobs : c. blobs . len ( ) as u64 ,
687
+ total_blobs_size : c. total_blobs_size ,
688
+ } ) ;
689
+
655
690
// TODO: we should check if the blobs referenced in this container
656
691
// actually exist in this provider before returning `FoundCollection`
657
692
write_response (
@@ -667,12 +702,21 @@ async fn transfer_collection(
667
702
writer. write_buf ( & mut data) . await ?;
668
703
for ( i, blob) in c. blobs . iter ( ) . enumerate ( ) {
669
704
debug ! ( "writing blob {}/{}" , i, c. blobs. len( ) ) ;
670
- let ( status, writer1) = send_blob ( db. clone ( ) , blob. hash , writer, buffer) . await ?;
705
+ tokio:: task:: yield_now ( ) . await ;
706
+ let ( status, writer1, size) = send_blob ( db. clone ( ) , blob. hash , writer, buffer) . await ?;
671
707
writer = writer1;
672
708
if SentStatus :: NotFound == status {
673
709
writer. finish ( ) . await ?;
674
710
return Ok ( status) ;
675
711
}
712
+
713
+ let _ = events. send ( Event :: TransferBlobCompleted {
714
+ connection_id,
715
+ request_id,
716
+ hash : blob. hash ,
717
+ index : i as u64 ,
718
+ size,
719
+ } ) ;
676
720
}
677
721
678
722
writer. finish ( ) . await ?;
@@ -740,9 +784,20 @@ async fn handle_stream(
740
784
} ;
741
785
742
786
// 5. Transfer data!
743
- match transfer_collection ( & db, writer, & mut out_buffer, & outboard, & data) . await {
787
+ match transfer_collection (
788
+ & db,
789
+ writer,
790
+ & mut out_buffer,
791
+ & outboard,
792
+ & data,
793
+ events. clone ( ) ,
794
+ connection_id,
795
+ request_id,
796
+ )
797
+ . await
798
+ {
744
799
Ok ( SentStatus :: Sent ) => {
745
- let _ = events. send ( Event :: TransferCompleted {
800
+ let _ = events. send ( Event :: TransferCollectionCompleted {
746
801
connection_id,
747
802
request_id,
748
803
} ) ;
@@ -771,7 +826,7 @@ async fn send_blob<W: AsyncWrite + Unpin + Send + 'static>(
771
826
name : Hash ,
772
827
mut writer : W ,
773
828
buffer : & mut BytesMut ,
774
- ) -> Result < ( SentStatus , W ) > {
829
+ ) -> Result < ( SentStatus , W , u64 ) > {
775
830
match db. get ( & name) {
776
831
Some ( BlobOrCollection :: Blob ( Data {
777
832
outboard,
@@ -796,11 +851,12 @@ async fn send_blob<W: AsyncWrite + Unpin + Send + 'static>(
796
851
std:: io:: Result :: Ok ( writer)
797
852
} )
798
853
. await ??;
799
- Ok ( ( SentStatus :: Sent , writer) )
854
+
855
+ Ok ( ( SentStatus :: Sent , writer, size) )
800
856
}
801
857
_ => {
802
858
write_response ( & mut writer, buffer, Res :: NotFound ) . await ?;
803
- Ok ( ( SentStatus :: NotFound , writer) )
859
+ Ok ( ( SentStatus :: NotFound , writer, 0 ) )
804
860
}
805
861
}
806
862
}
0 commit comments