11/// Example how to limit blob requests by hash and node id, and to add
2- /// restrictions on limited content.
2+ /// throttling or limiting the maximum number of connections.
3+ ///
4+ /// Limiting is done via a fn that returns an EventSender and internally
5+ /// makes liberal use of spawn to spawn background tasks.
6+ ///
7+ /// This is fine, since the tasks will terminate as soon as the [BlobsProtocol]
8+ /// instance holding the [EventSender] will be dropped. But for production
9+ /// grade code you might nevertheless put the tasks into a [tokio::task::JoinSet] or
10+ /// [n0_future::FuturesUnordered].
311mod common;
412use std:: {
513 collections:: { HashMap , HashSet } ,
@@ -31,33 +39,37 @@ use crate::common::get_or_generate_secret_key;
3139pub enum Args {
3240 /// Limit requests by node id
3341 ByNodeId {
34- /// Path for files to add
42+ /// Path for files to add.
3543 paths : Vec < PathBuf > ,
3644 #[ clap( long( "allow" ) ) ]
3745 /// Nodes that are allowed to download content.
3846 allowed_nodes : Vec < NodeId > ,
47+ /// Number of secrets to generate for allowed node ids.
3948 #[ clap( long, default_value_t = 1 ) ]
4049 secrets : usize ,
4150 } ,
4251 /// Limit requests by hash, only first hash is allowed
4352 ByHash {
44- /// Path for files to add
53+ /// Path for files to add.
4554 paths : Vec < PathBuf > ,
4655 } ,
4756 /// Throttle requests
4857 Throttle {
49- /// Path for files to add
58+ /// Path for files to add.
5059 paths : Vec < PathBuf > ,
60+ /// Delay in milliseconds after sending a chunk group of 16 KiB.
5161 #[ clap( long, default_value = "100" ) ]
5262 delay_ms : u64 ,
5363 } ,
5464 /// Limit maximum number of connections.
5565 MaxConnections {
56- /// Path for files to add
66+ /// Path for files to add.
5767 paths : Vec < PathBuf > ,
68+ /// Maximum number of concurrent get requests.
5869 #[ clap( long, default_value = "1" ) ]
5970 max_connections : usize ,
6071 } ,
72+ /// Get a blob. Just for completeness sake.
6173 Get {
6274 /// Ticket for the blob to download
6375 ticket : BlobTicket ,
@@ -84,6 +96,8 @@ fn limit_by_node_id(allowed_nodes: HashSet<NodeId>) -> EventSender {
8496 EventSender :: new (
8597 tx,
8698 EventMask {
99+ // We want a request for each incoming connection so we can accept
100+ // or reject them. We don't need any other events.
87101 connected : ConnectMode :: Request ,
88102 ..EventMask :: DEFAULT
89103 } ,
@@ -112,6 +126,9 @@ fn limit_by_hash(allowed_hashes: HashSet<Hash>) -> EventSender {
112126 EventSender :: new (
113127 tx,
114128 EventMask {
129+ // We want to get a request for each get request that we can answer
130+ // with OK or not OK depending on the hash. We do not want detailed
131+ // events once it has been decided to handle a request.
115132 get : RequestMode :: Request ,
116133 ..EventMask :: DEFAULT
117134 } ,
@@ -128,6 +145,8 @@ fn throttle(delay_ms: u64) -> EventSender {
128145 "Throttling {} {}, {}ms" ,
129146 msg. connection_id, msg. request_id, delay_ms
130147 ) ;
148+ // we could compute the delay from the size of the data to have a fixed rate.
149+ // but the size is almost always 16 KiB (16 chunks).
131150 tokio:: time:: sleep ( std:: time:: Duration :: from_millis ( delay_ms) ) . await ;
132151 msg. tx . send ( Ok ( ( ) ) ) . await . ok ( ) ;
133152 } ) ;
@@ -137,6 +156,8 @@ fn throttle(delay_ms: u64) -> EventSender {
137156 EventSender :: new (
138157 tx,
139158 EventMask {
159+ // We want to get requests for each sent user data blob, so we can add a delay.
160+ // Other than that, we don't need any events.
140161 throttle : ThrottleMode :: Throttle ,
141162 ..EventMask :: DEFAULT
142163 } ,
@@ -206,6 +227,10 @@ fn limit_max_connections(max_connections: usize) -> EventSender {
206227 EventSender :: new (
207228 tx,
208229 EventMask {
230+ // For each get request, we want to get a request so we can decide
231+ // based on the current connection count if we want to accept or reject.
232+ // We also want detailed logging of events for the get request, so we can
233+ // detect when the request is finished one way or another.
209234 get : RequestMode :: RequestLog ,
210235 ..EventMask :: DEFAULT
211236 } ,
0 commit comments