Skip to content

Commit bf03812

Browse files
authored
Merge pull request #690 from Hou-Xiaoxuan/libra_add_p2p_feature
feat: Libra add new "p2p" feature.
2 parents d8f69a5 + 5cb4ff4 commit bf03812

File tree

3 files changed

+66
-61
lines changed

3 files changed

+66
-61
lines changed

libra/Cargo.toml

+35-36
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,56 @@ name = "libra"
33
version = "0.1.0"
44
edition = "2021"
55

6-
[[bin]]
7-
name = "libra"
8-
path = "src/main.rs"
6+
[features]
7+
default = []
8+
p2p = ["gemini"]
99

1010
[dependencies]
11-
mercury = { workspace = true }
11+
anyhow = { workspace = true }
12+
byte-unit = "5.1.4"
13+
byteorder = "1.5.0"
14+
bytes = { workspace = true }
1215
ceres = { workspace = true }
13-
gemini = { workspace = true }
14-
15-
sea-orm = { workspace = true, features = [
16-
"sqlx-sqlite",
17-
"runtime-tokio-rustls",
18-
"macros",
19-
] }
20-
tokio = { workspace = true, features = ["rt-multi-thread", "rt", "macros"] }
16+
chrono = { workspace = true }
2117
clap = { workspace = true, features = ["derive"] }
18+
color-backtrace = "0.6.1"
19+
colored = { workspace = true }
2220
flate2 = { workspace = true } # add features = ["zlib"] if slow
23-
tracing = { workspace = true }
24-
tracing-subscriber ={ workspace = true }
25-
sha1 = { workspace = true }
26-
bytes = { workspace = true }
27-
chrono = { workspace = true }
2821
futures = { workspace = true }
29-
reqwest = { workspace = true, features = ["stream", "json"] }
30-
tokio-util = { version = "0.7.11", features = ["io"] }
31-
color-backtrace = "0.6.1"
32-
colored = "2.1.0"
33-
byteorder = "1.5.0"
34-
path_abs = "0.5.1"
35-
pathdiff = "0.2.1"
36-
url = "2.5.0"
37-
futures-util = "0.3.30"
38-
rpassword = "7.3.1"
22+
futures-util = { workspace = true }
23+
gemini = { workspace = true, optional = true }
24+
hex = { workspace = true }
3925
indicatif = "0.17.8"
40-
wax = "0.6.0"
4126
lazy_static = { workspace = true }
27+
lru-mem = "0.3.0"
28+
mercury = { workspace = true }
29+
once_cell = "1.19.0"
30+
path_abs = "0.5.1"
31+
pathdiff = "0.2.1"
4232
regex = { workspace = true }
43-
ring = "0.17.8"
44-
hex = { workspace = true }
33+
reqwest = { workspace = true, features = ["stream", "json"] }
34+
ring = { workspace = true }
35+
rpassword = "7.3.1"
36+
scopeguard = "1.2.0"
37+
sea-orm = { workspace = true, features = [
38+
"sqlx-sqlite",
39+
"runtime-tokio-rustls",
40+
"macros",
41+
] }
4542
serde = { workspace = true }
4643
serde_json = { workspace = true }
47-
once_cell = "1.19.0"
48-
byte-unit = "5.1.4"
49-
scopeguard = "1.2.0"
50-
lru-mem = "0.3.0"
51-
anyhow = { workspace = true }
44+
sha1 = { workspace = true }
45+
tokio = { workspace = true, features = ["rt-multi-thread", "rt", "macros"] }
46+
tokio-util = { version = "0.7.11", features = ["io"] }
47+
tracing = { workspace = true }
48+
tracing-subscriber = { workspace = true }
49+
url = "2.5.3"
50+
wax = "0.6.0"
5251

5352
[target.'cfg(unix)'.dependencies] # only on Unix
5453
pager = "0.16.0"
5554

5655
[dev-dependencies]
56+
tempfile = { workspace = true }
5757
tokio = { workspace = true, features = ["macros", "process"] }
5858
tracing-test = "0.2.4"
59-
tempfile = { workspace = true }

libra/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ achieving unified management.
8181
### Others
8282
- [ ] `.gitignore`
8383
- [x] `.gitattributes` (only for `lfs` now)
84-
- [x] `LFS` (embedded)
84+
- [x] `LFS` (embedded, with p2p feature)
8585
- [ ] `ssh`
8686

8787
## Development

libra/src/internal/protocol/lfs_client.rs

+30-24
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,41 @@ impl LFSClient {
428428
}
429429
}
430430

431-
#[allow(clippy::type_complexity)]
431+
/// Only for MonoRepo (mega)
432+
async fn fetch_chunks(&self, obj_link: &str) -> Result<Vec<ChunkRepresentation>, ()> {
433+
let mut url = Url::parse(obj_link).unwrap();
434+
let path = url.path().trim_end_matches('/');
435+
url.set_path(&(path.to_owned() + "/chunks")); // reserve query params (for GitHub link)
436+
437+
let request = self.client.get(url);
438+
let resp = request.send().await.unwrap();
439+
let code = resp.status();
440+
if code == StatusCode::NOT_FOUND || code == StatusCode::FORBIDDEN { // GitHub maybe return 403
441+
tracing::info!("Remote LFS Server not support Chunks API, or forbidden.");
442+
return Err(());
443+
} else if !code.is_success() {
444+
tracing::debug!("fatal: LFS get chunk hrefs failed. Status: {}, Message: {}", code, resp.text().await.unwrap());
445+
return Err(());
446+
}
447+
let mut res = resp.json::<FetchchunkResponse>().await.unwrap();
448+
// sort by offset
449+
res.chunks.sort_by(|a, b| a.offset.cmp(&b.offset));
450+
Ok(res.chunks)
451+
}
452+
}
453+
454+
#[cfg(feature="p2p")]
455+
type Reporter = (dyn FnMut(f64) -> anyhow::Result<()> + Send);
456+
#[cfg(feature="p2p")]
457+
impl LFSClient{
458+
432459
/// download (GET) one LFS file peer-to-peer
433460
pub async fn download_object_p2p(
434461
&self,
435462
file_uri: &str, // p2p protocol
436463
path: impl AsRef<Path>,
437464
mut reporter: Option<(
438-
&mut (dyn FnMut(f64) -> anyhow::Result<()> + Send), // progress callback
465+
&mut Reporter, // progress callback
439466
f64 // step
440467
)>) -> anyhow::Result<()>
441468
{
@@ -549,28 +576,6 @@ impl LFSClient {
549576
}
550577
Ok(buffer)
551578
}
552-
553-
/// Only for MonoRepo (mega)
554-
async fn fetch_chunks(&self, obj_link: &str) -> Result<Vec<ChunkRepresentation>, ()> {
555-
let mut url = Url::parse(obj_link).unwrap();
556-
let path = url.path().trim_end_matches('/');
557-
url.set_path(&(path.to_owned() + "/chunks")); // reserve query params (for GitHub link)
558-
559-
let request = self.client.get(url);
560-
let resp = request.send().await.unwrap();
561-
let code = resp.status();
562-
if code == StatusCode::NOT_FOUND || code == StatusCode::FORBIDDEN { // GitHub maybe return 403
563-
tracing::info!("Remote LFS Server not support Chunks API, or forbidden.");
564-
return Err(());
565-
} else if !code.is_success() {
566-
tracing::debug!("fatal: LFS get chunk hrefs failed. Status: {}, Message: {}", code, resp.text().await.unwrap());
567-
return Err(());
568-
}
569-
let mut res = resp.json::<FetchchunkResponse>().await.unwrap();
570-
// sort by offset
571-
res.chunks.sort_by(|a, b| a.offset.cmp(&b.offset));
572-
Ok(res.chunks)
573-
}
574579
}
575580

576581
// LFS locks API
@@ -715,6 +720,7 @@ mod tests {
715720
}
716721

717722
#[tokio::test]
723+
#[cfg(feature="p2p")]
718724
#[ignore] // need to start local mega server
719725
async fn test_download_chunk() {
720726
let client = LFSClient::from_url(&Url::parse("http://localhost:8000").unwrap());

0 commit comments

Comments
 (0)