Skip to content

Commit 806fa45

Browse files
committed
Move the ConnectionPool to util and restructure a bunch of stuff so we can
make util public. The ConnPool will probably move somewhere else in the longer term.
1 parent 24c5895 commit 806fa45

File tree

12 files changed

+93
-97
lines changed

12 files changed

+93
-97
lines changed

src/api/blobs/reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ mod tests {
221221

222222
use super::*;
223223
use crate::{
224+
protocol::ChunkRangesExt,
224225
store::{
225226
fs::{
226227
tests::{create_n0_bao, test_data, INTERESTING_SIZES},
227228
FsStore,
228229
},
229230
mem::MemStore,
230231
},
231-
util::ChunkRangesExt,
232232
};
233233

234234
async fn reader_smoke(blobs: &Blobs) -> TestResult<()> {

src/api/downloader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ async fn execute_get(
442442
request: request.clone(),
443443
})
444444
.await?;
445-
let conn = pool.connect(provider);
445+
let conn = pool.get_or_connect(provider);
446446
let local = remote.local_for_request(request.clone()).await?;
447447
if local.is_complete() {
448448
return Ok(());

src/api/remote.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ mod tests {
10671067

10681068
use crate::{
10691069
api::blobs::Blobs,
1070-
protocol::{ChunkRangesSeq, GetRequest},
1070+
protocol::{ChunkRangesExt, ChunkRangesSeq, GetRequest},
10711071
store::{
10721072
fs::{
10731073
tests::{create_n0_bao, test_data, INTERESTING_SIZES},
@@ -1076,7 +1076,6 @@ mod tests {
10761076
mem::MemStore,
10771077
},
10781078
tests::{add_test_hash_seq, add_test_hash_seq_incomplete},
1079-
util::ChunkRangesExt,
10801079
};
10811080

10821081
#[tokio::test]

src/get/request.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ use super::{fsm, GetError, GetResult, Stats};
2727
use crate::{
2828
get::error::{BadRequestSnafu, LocalFailureSnafu},
2929
hashseq::HashSeq,
30-
protocol::{ChunkRangesSeq, GetRequest},
31-
util::ChunkRangesExt,
30+
protocol::{ChunkRangesExt, ChunkRangesSeq, GetRequest},
3231
Hash, HashAndFormat,
3332
};
3433

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub mod ticket;
4343

4444
#[doc(hidden)]
4545
pub mod test;
46-
mod util;
46+
pub mod util;
4747

4848
#[cfg(test)]
4949
mod tests;

src/protocol.rs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,21 +373,25 @@
373373
//! a large existing system that has demonstrated performance issues.
374374
//!
375375
//! If in doubt, just use multiple requests and multiple connections.
376-
use std::io;
376+
use std::{
377+
io,
378+
ops::{Bound, RangeBounds},
379+
};
377380

381+
use bao_tree::{io::round_up_to_chunks, ChunkNum};
378382
use builder::GetRequestBuilder;
379383
use derive_more::From;
380384
use iroh::endpoint::VarInt;
381385
use irpc::util::AsyncReadVarintExt;
382386
use postcard::experimental::max_size::MaxSize;
387+
use range_collections::{range_set::RangeSetEntry, RangeSet2};
383388
use serde::{Deserialize, Serialize};
384389
mod range_spec;
385390
pub use bao_tree::ChunkRanges;
386391
pub use range_spec::{ChunkRangesSeq, NonEmptyRequestRangeSpecIter, RangeSpec};
387392
use snafu::{GenerateImplicitData, Snafu};
388393
use tokio::io::AsyncReadExt;
389394

390-
pub use crate::util::ChunkRangesExt;
391395
use crate::{api::blobs::Bitfield, provider::CountingReader, BlobFormat, Hash, HashAndFormat};
392396

393397
/// Maximum message size is limited to 100MiB for now.
@@ -714,6 +718,73 @@ impl TryFrom<VarInt> for Closed {
714718
}
715719
}
716720

721+
pub trait ChunkRangesExt {
722+
fn last_chunk() -> Self;
723+
fn chunk(offset: u64) -> Self;
724+
fn bytes(ranges: impl RangeBounds<u64>) -> Self;
725+
fn chunks(ranges: impl RangeBounds<u64>) -> Self;
726+
fn offset(offset: u64) -> Self;
727+
}
728+
729+
impl ChunkRangesExt for ChunkRanges {
730+
fn last_chunk() -> Self {
731+
ChunkRanges::from(ChunkNum(u64::MAX)..)
732+
}
733+
734+
/// Create a chunk range that contains a single chunk.
735+
fn chunk(offset: u64) -> Self {
736+
ChunkRanges::from(ChunkNum(offset)..ChunkNum(offset + 1))
737+
}
738+
739+
/// Create a range of chunks that contains the given byte ranges.
740+
/// The byte ranges are rounded up to the nearest chunk size.
741+
fn bytes(ranges: impl RangeBounds<u64>) -> Self {
742+
round_up_to_chunks(&bounds_from_range(ranges, |v| v))
743+
}
744+
745+
/// Create a range of chunks from u64 chunk bounds.
746+
///
747+
/// This is equivalent but more convenient than using the ChunkNum newtype.
748+
fn chunks(ranges: impl RangeBounds<u64>) -> Self {
749+
bounds_from_range(ranges, ChunkNum)
750+
}
751+
752+
/// Create a chunk range that contains a single byte offset.
753+
fn offset(offset: u64) -> Self {
754+
Self::bytes(offset..offset + 1)
755+
}
756+
}
757+
758+
// todo: move to range_collections
759+
pub(crate) fn bounds_from_range<R, T, F>(range: R, f: F) -> RangeSet2<T>
760+
where
761+
R: RangeBounds<u64>,
762+
T: RangeSetEntry,
763+
F: Fn(u64) -> T,
764+
{
765+
let from = match range.start_bound() {
766+
Bound::Included(start) => Some(*start),
767+
Bound::Excluded(start) => {
768+
let Some(start) = start.checked_add(1) else {
769+
return RangeSet2::empty();
770+
};
771+
Some(start)
772+
}
773+
Bound::Unbounded => None,
774+
};
775+
let to = match range.end_bound() {
776+
Bound::Included(end) => end.checked_add(1),
777+
Bound::Excluded(end) => Some(*end),
778+
Bound::Unbounded => None,
779+
};
780+
match (from, to) {
781+
(Some(from), Some(to)) => RangeSet2::from(f(from)..f(to)),
782+
(Some(from), None) => RangeSet2::from(f(from)..),
783+
(None, Some(to)) => RangeSet2::from(..f(to)),
784+
(None, None) => RangeSet2::all(),
785+
}
786+
}
787+
717788
pub mod builder {
718789
use std::collections::BTreeMap;
719790

@@ -863,7 +934,7 @@ pub mod builder {
863934
use bao_tree::ChunkNum;
864935

865936
use super::*;
866-
use crate::{protocol::GetManyRequest, util::ChunkRangesExt};
937+
use crate::protocol::{ChunkRangesExt, GetManyRequest};
867938

868939
#[test]
869940
fn chunk_ranges_ext() {

src/protocol/range_spec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bao_tree::{ChunkNum, ChunkRangesRef};
1212
use serde::{Deserialize, Serialize};
1313
use smallvec::{smallvec, SmallVec};
1414

15-
pub use crate::util::ChunkRangesExt;
15+
use crate::protocol::ChunkRangesExt;
1616

1717
static CHUNK_RANGES_EMPTY: OnceLock<ChunkRanges> = OnceLock::new();
1818

@@ -511,7 +511,7 @@ mod tests {
511511
use proptest::prelude::*;
512512

513513
use super::*;
514-
use crate::util::ChunkRangesExt;
514+
use crate::protocol::ChunkRangesExt;
515515

516516
fn ranges(value_range: Range<u64>) -> impl Strategy<Value = ChunkRanges> {
517517
prop::collection::vec((value_range.clone(), value_range), 0..16).prop_map(|v| {

src/store/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ use crate::{
111111
},
112112
ApiClient,
113113
},
114+
protocol::ChunkRangesExt,
114115
store::{
115116
fs::{
116117
bao_file::{
@@ -125,7 +126,6 @@ use crate::{
125126
util::{
126127
channel::oneshot,
127128
temp_tag::{TagDrop, TempTag, TempTagScope, TempTags},
128-
ChunkRangesExt,
129129
},
130130
};
131131
mod bao_file;

src/store/mem.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,12 @@ use crate::{
5656
tags::TagInfo,
5757
ApiClient,
5858
},
59+
protocol::ChunkRangesExt,
5960
store::{
6061
util::{SizeInfo, SparseMemFile, Tag},
6162
HashAndFormat, IROH_BLOCK_SIZE,
6263
},
63-
util::{
64-
temp_tag::{TagDrop, TempTagScope, TempTags},
65-
ChunkRangesExt,
66-
},
64+
util::temp_tag::{TagDrop, TempTagScope, TempTags},
6765
BlobFormat, Hash,
6866
};
6967

src/store/readonly_mem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use crate::{
4141
},
4242
ApiClient, TempTag,
4343
},
44+
protocol::ChunkRangesExt,
4445
store::{mem::CompleteStorage, IROH_BLOCK_SIZE},
45-
util::ChunkRangesExt,
4646
Hash,
4747
};
4848

0 commit comments

Comments
 (0)