Skip to content

Commit 265bd83

Browse files
authored
Merge pull request #1825 from tursodatabase/lucio/sync-switch-hyper
libsql: Switch to hyper for local offline
2 parents 909b8af + c8f74e4 commit 265bd83

File tree

3 files changed

+30
-206
lines changed

3 files changed

+30
-206
lines changed

Cargo.lock

Lines changed: 5 additions & 198 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libsql/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ fallible-iterator = { version = "0.3", optional = true }
4242

4343
libsql_replication = { version = "0.6", path = "../libsql-replication", optional = true }
4444
async-stream = { version = "0.3.5", optional = true }
45-
reqwest = { version = "0.12.9", default-features = false, features = [ "rustls-tls", "json" ], optional = true }
4645

4746
[dev-dependencies]
4847
criterion = { version = "0.5", features = ["html_reports", "async", "async_futures", "async_tokio"] }
@@ -105,7 +104,6 @@ sync = [
105104
"dep:bytes",
106105
"dep:tokio",
107106
"dep:futures",
108-
"dep:reqwest",
109107
"dep:serde_json",
110108
]
111109
hrana = [

libsql/src/local/database.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,21 +476,40 @@ impl Database {
476476
) -> Result<u32> {
477477
let mut nr_retries = 0;
478478
loop {
479-
let client = reqwest::Client::new();
480-
let mut builder = client.post(uri.to_owned());
479+
// TODO(lucio): add custom connector + tls support here
480+
let client = hyper::client::Client::builder().build_http::<hyper::Body>();
481+
482+
let mut req = http::Request::post(uri.clone());
483+
481484
match auth_token {
482485
Some(ref auth_token) => {
483-
builder = builder
484-
.header("Authorization", format!("Bearer {}", auth_token.to_owned()));
486+
let auth_header =
487+
http::HeaderValue::try_from(format!("Bearer {}", auth_token.to_owned()))
488+
.unwrap();
489+
490+
req.headers_mut()
491+
.expect("valid http request")
492+
.insert("Authorization", auth_header);
485493
}
486494
None => {}
487495
}
488-
let res = builder.body(frame.to_vec()).send().await.unwrap();
496+
497+
// TODO(lucio): convert this to use bytes to make this clone cheap, it should be
498+
// to possible use BytesMut when reading frames from the WAL and efficiently use Bytes
499+
// from that.
500+
let req = req.body(frame.clone().into()).expect("valid body");
501+
502+
let res = client.request(req).await.unwrap();
503+
504+
// TODO(lucio): only retry on server side errors
489505
if res.status().is_success() {
490-
let resp = res.json::<serde_json::Value>().await.unwrap();
506+
let res_body = hyper::body::to_bytes(res.into_body()).await.unwrap();
507+
let resp = serde_json::from_slice::<serde_json::Value>(&res_body[..]).unwrap();
508+
491509
let max_frame_no = resp.get("max_frame_no").unwrap().as_u64().unwrap();
492510
return Ok(max_frame_no as u32);
493511
}
512+
494513
if nr_retries > max_retries {
495514
return Err(crate::errors::Error::ConnectionFailed(format!(
496515
"Failed to push frame: {}",

0 commit comments

Comments
 (0)