Skip to content

Connection as shared state causes error after idle without requests #2

@jregistr

Description

@jregistr

Hi. I found this repo searching for example using turso and axum. I've done some testing and found that using a Connection that is shared as State results in errors after a some successful requests if there's a pause.

called `Result::unwrap()` on an `Err` value: Hrana(Api("{\"message\":\"The stream has expired due to inactivity\",\"code\":\"STREAM_EXPIRED\"}"))

Here's what I tested.
main.rs

let url = env::var("LIBSQL_URL").expect("LIBSQL_URL must be set");
let token = env::var("LIBSQL_AUTH_TOKEN").expect("LIBSQL auth token must be set");

let db = Builder::new_remote(url, token).build().await.unwrap();
let conn = db.connect().unwrap();

let app = Router::new()
        .merge(user::show())
        .with_state(Arc::new(AppState {conn}));
let address = format!("127.0.0.1:{}", 3000).parse().unwrap()
let listener = tokio::net::TcpListener::bind(address).await.unwrap();
let svc = app.into_make_service_with_connect_info::<SocketAddr>();
axum::serve(listener, svc).await.unwrap();

file with user route

pub async fn show_user(
    Path(user_id): Path<String>,
    State(app): State<Arc<AppState>>
) -> String {
    let conn = &app.conn;
    let mut stmt = conn
        .prepare("SELECT * FROM users WHERE id = ?1")
        .await
        .unwrap();
    let row = stmt.query_row([user_id.as_str()]).await.unwrap();
    let id = row.get_value(1).unwrap().as_text().unwrap().clone();
    id
}

Worth noting Turso has an example over here: https://docs.turso.tech/sdk/rust/guides/axum
They construct both the DB and the connection in the handler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions