tursodatabase / libsql-client-rs

libSQL Rust client library can be used to communicate with sqld natively over HTTP protocol with native Rust interface.
MIT License
75 stars 25 forks source link

v0.32.0 can no longer be used as an axum state (`!Sync`) #52

Open chesedo opened 9 months ago

chesedo commented 9 months ago

The following minimal Axum code used to work with v0.31.0, but no longer works with the last version. I believe this is because the local client is no longer Sync in v0.32.0 (while it was in v0.31.0).

/// cargo add axum libsql-client
/// cargo add tokio --features rt-multi-thread
use std::sync::Arc;

use axum::{extract::State, routing::get, Router};
use libsql_client::Client;

async fn root(State(client): State<Arc<Client>>) -> String {
    let result = client
        .execute("SELECT 'hello, world!'")
        .await
        .unwrap()
        .rows
        .first()
        .map(|row| row.values.get(0))
        .flatten()
        .map(|value| value.to_string());

    result.unwrap()
}

#[tokio::main]
async fn main() {
    let client = libsql_client::Client::from_config(libsql_client::Config {
        url: format!(
            "file:///{}/example.db",
            std::env::current_dir().unwrap().display()
        )
        .parse()
        .unwrap(),
        auth_token: None,
    })
    .await
    .unwrap();

    let state = Arc::new(client);

    // This line will have a bunch of errors for the route
    let app = Router::new().route("/", get(root)).with_state(state);

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

Error

With v0.32.0 the following snippet of the compile error is interesting:

*mut libsql_sys::ffi::sqlite3` cannot be shared between threads safely
lypanov commented 9 months ago

This blocked my getting started experience with Turso entirely alas.

Naveenravi07 commented 8 months ago

Same here

digizeph commented 7 months ago

Failed to build for 0.33. Had to revert back to 0.31. I wonder what was the underlying change introduced causing this.

LucioFranco commented 7 months ago

Hi all, sorry about the delay. We have actually been working on a new libsql client that lives in https://github.com/tursodatabase/libsql we are almost ready to publish it to cratesio which we can do next week. This client explicitly supports working in axum because it implements both Send and Sync so it should solve this problem. We plan on deprecating this library soon. In the meantime its quite stable so feel free to try it out via:

libsql = { git = "https://github.com/tursodatabase/libsql" }

if you just want to compile the the http only features (not do any C building to build the sqlite3 code) you can do that like so:

libsql = { git = "https://github.com/tursodatabase/libsql", default-features = false, features = ["remote"] }

This example shows you how you can connect either via the http/remote protocol or open a normal in memory libsql-sqlite3 database.

Feel free to ping me on discord if you run into any issues!