Skip to content

Commit

Permalink
Revert "Merge pull request #1825 from tursodatabase/lucio/sync-switch…
Browse files Browse the repository at this point in the history
…-hyper"

This reverts commit 265bd83, reversing
changes made to 909b8af because it breaks the example app:

```
thread 'main' panicked at /Users/penberg/src/tursodatabase/libsql/libsql/src/local/database.rs:502:49:
called `Result::unwrap()` on an `Err` value: hyper::Error(Connect, "invalid URL, scheme is not http")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
  • Loading branch information
penberg committed Nov 19, 2024
1 parent 265bd83 commit fd44afa
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 30 deletions.
203 changes: 198 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions libsql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fallible-iterator = { version = "0.3", optional = true }

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

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports", "async", "async_futures", "async_tokio"] }
Expand Down Expand Up @@ -104,6 +105,7 @@ sync = [
"dep:bytes",
"dep:tokio",
"dep:futures",
"dep:reqwest",
"dep:serde_json",
]
hrana = [
Expand Down
31 changes: 6 additions & 25 deletions libsql/src/local/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,40 +476,21 @@ impl Database {
) -> Result<u32> {
let mut nr_retries = 0;
loop {
// TODO(lucio): add custom connector + tls support here
let client = hyper::client::Client::builder().build_http::<hyper::Body>();

let mut req = http::Request::post(uri.clone());

let client = reqwest::Client::new();
let mut builder = client.post(uri.to_owned());
match auth_token {
Some(ref auth_token) => {
let auth_header =
http::HeaderValue::try_from(format!("Bearer {}", auth_token.to_owned()))
.unwrap();

req.headers_mut()
.expect("valid http request")
.insert("Authorization", auth_header);
builder = builder
.header("Authorization", format!("Bearer {}", auth_token.to_owned()));
}
None => {}
}

// TODO(lucio): convert this to use bytes to make this clone cheap, it should be
// to possible use BytesMut when reading frames from the WAL and efficiently use Bytes
// from that.
let req = req.body(frame.clone().into()).expect("valid body");

let res = client.request(req).await.unwrap();

// TODO(lucio): only retry on server side errors
let res = builder.body(frame.to_vec()).send().await.unwrap();
if res.status().is_success() {
let res_body = hyper::body::to_bytes(res.into_body()).await.unwrap();
let resp = serde_json::from_slice::<serde_json::Value>(&res_body[..]).unwrap();

let resp = res.json::<serde_json::Value>().await.unwrap();
let max_frame_no = resp.get("max_frame_no").unwrap().as_u64().unwrap();
return Ok(max_frame_no as u32);
}

if nr_retries > max_retries {
return Err(crate::errors::Error::ConnectionFailed(format!(
"Failed to push frame: {}",
Expand Down

0 comments on commit fd44afa

Please sign in to comment.