Stream: ideas

Topic: basic-webserver managing connections


view this post on Zulip Luke Boswell (May 29 2024 at 11:31):

Looking for any ideas.

In basic-webserver we currently open sqlite connections and keep them all in a vec and use get_connection when roc runs a Task and use the file path as the index into the vec to access the connection we want to use.

// a global store of connections
static mut GLOBAL_SQLITE_CONNECTIONS: *mut std::sync::Mutex<Vec<SQLiteConnection>> =
    std::ptr::null_mut();

// called from our roc effect `SQLite3.execute`
fn get_connection(path: &str) -> Result<std::sync::Arc<sqlite::Connection>, sqlite::Error> {  //...

// using the path as an index to get the connection we want
for c in opened_store.iter() {
    if c.path == path {
        return Ok(Arc::clone(&c.connection));
    }
}

This is working ok. But I feel like we should improve it.

One thing I am concerned about is that we never close these connections, and that feels wrong. I assume it would be desirable for the OS to cleanup and flush things to disk etc... but I'm not sure.

I was thinking of adding some kind of timer for each connection and closing them after some period after last use.

view this post on Zulip Karl (May 29 2024 at 12:21):

From the docs it looks like it would only impact internal SQLite resources with the main one being the transactions. If the connection dies mid transaction it'll be rolled back on the next open. Otherwise the only OS level resources should be the handles for the db files themselves and I'm not aware of any downsides to a file handle remaining open forever.

view this post on Zulip Luke Boswell (May 29 2024 at 23:27):

Thank you for looking into this. That makes sense. :smiley:


Last updated: Jun 16 2026 at 16:19 UTC