pr review: add better module docs etc.

This commit is contained in:
Ruediger Klaehn
2023-02-06 15:47:40 +02:00
parent 8255467c9a
commit c08fe11c4e
3 changed files with 31 additions and 19 deletions
+3 -3
View File
@@ -276,7 +276,7 @@ pub struct SliceValidator<R> {
}
impl<R> SliceValidator<R> {
/// create a new slice validator for the given hash and range
/// creates a new slice validator for the given hash and range
pub fn new(inner: R, hash: blake3::Hash, start: u64, len: u64) -> Self {
let range = start..start.saturating_add(len);
Self {
@@ -389,7 +389,7 @@ impl<R: Read> Iterator for SliceValidator<R> {
pub struct AsyncSliceValidator<R: tokio::io::AsyncRead + Unpin>(SliceValidator<R>);
impl<R: tokio::io::AsyncRead + Unpin> AsyncSliceValidator<R> {
/// create a new slice validator for the given hash and range
/// creates a new slice validator for the given hash and range
pub fn new(inner: R, hash: blake3::Hash, start: u64, len: u64) -> Self {
Self(SliceValidator::new(inner, hash, start, len))
}
@@ -511,7 +511,7 @@ pub(crate) struct AsyncSliceDecoder<R: tokio::io::AsyncRead + Unpin> {
}
impl<R: tokio::io::AsyncRead + Unpin> AsyncSliceDecoder<R> {
/// Create a new slice decoder for the given hash and range
/// Creates a new slice decoder for the given hash and range
///
/// The hash is the hash of the entire stream, and the range is the range being sent.
/// If you want to decode an entire file and do not know the length, it is OK to pass
+13 -10
View File
@@ -1,4 +1,8 @@
//! The client side api
//! The client side API
//!
//! The main entry point is [`run`]. This function takes callbacks that will
//! be invoked when blobs or collections are received. It is up to the caller
//! to store the received data.
use std::fmt::Debug;
use std::io;
use std::net::SocketAddr;
@@ -71,8 +75,14 @@ pub struct Stats {
pub data_len: u64,
/// The time it took to transfer the data
pub elapsed: Duration,
}
impl Stats {
/// Transfer rate in megabits per second
pub mbits: f64,
pub fn mbits(&self) -> f64 {
let data_len_bit = self.data_len * 8;
data_len_bit as f64 / (1000. * 1000.) / self.elapsed.as_secs_f64()
}
}
/// A verified stream of data coming from the provider
@@ -222,15 +232,8 @@ where
writer.close().await?;
let elapsed = now.elapsed();
let elapsed_s = elapsed.as_secs_f64();
let data_len_bit = data_len * 8;
let mbits = data_len_bit as f64 / (1000. * 1000.) / elapsed_s;
let stats = Stats {
data_len,
elapsed,
mbits,
};
let stats = Stats { data_len, elapsed };
Ok(stats)
}
+15 -6
View File
@@ -1,4 +1,12 @@
//! Provider api
//! Provider API
//!
//! A provider is a server that serves content-addressed data (blobs or collections).
//! To create a provider, create a database using [create_collection], then build a
//! provider using [Builder] and spawn it using [Builder::spawn].
//!
//! You can monitor what is happening in the provider using [Provider::subscribe].
//!
//! To shut down the provider, call [Provider::abort].
use std::fmt::{self, Display};
use std::io::{BufReader, Read};
use std::net::SocketAddr;
@@ -179,12 +187,12 @@ pub struct Provider {
pub enum Event {
/// A new client connected to the provider.
ClientConnected {
/// The quic connection id.
/// An unique connection id.
connection_id: u64,
},
/// A request was received from a client.
RequestReceived {
/// The quic connection id.
/// An unique connection id.
connection_id: u64,
/// The request id.
request_id: u64,
@@ -193,7 +201,7 @@ pub enum Event {
},
/// A request was completed and the data was sent to the client.
TransferCompleted {
/// The quic connection id.
/// An unique connection id.
connection_id: u64,
/// The request id.
request_id: u64,
@@ -271,6 +279,7 @@ async fn handle_stream(
) -> Result<()> {
debug!("stream opened from {:?}", stream.connection().remote_addr());
let connection_id = stream.connection().id();
stream.connection().application_protocol().unwrap();
let (mut reader, mut writer) = stream.split();
let mut out_buffer = BytesMut::with_capacity(1024);
let mut in_buffer = BytesMut::with_capacity(1024);
@@ -455,11 +464,11 @@ pub enum DataSource {
}
impl DataSource {
/// Create a new [`DataSource`] from a [`PathBuf`].
/// Creates a new [`DataSource`] from a [`PathBuf`].
pub fn new(path: PathBuf) -> Self {
DataSource::File(path)
}
/// Create a new [`DataSource`] from a [`PathBuf`] and a custom name.
/// Creates a new [`DataSource`] from a [`PathBuf`] and a custom name.
pub fn with_name(path: PathBuf, name: String) -> Self {
DataSource::NamedFile { path, name }
}