Repository: libsql/libsql
File: libsql-storage/src/lib.rs (Rust)

Code before:
        trace!("DurableWalManager::log_exists()");
        Ok(true)
    }

    fn destroy(self)
    where
        Self: Sized,
    {
        trace!("DurableWalManager::destroy()");
    }
}

pub struct DurableWal {
    namespace: String,
    conn_id: String,
    client: parking_lot::Mutex<StorageClient<Channel>>,
    frames_cache: SieveCache<std::num::NonZeroU64, Vec<u8>>,
    write_cache: BTreeMap<u32, rpc::Frame>,
    lock_manager: Arc<Mutex<LockManager>>,
    _runtime: Option<tokio::runtime::Runtime>,
    rt: tokio::runtime::Handle,
}

impl DurableWal {
    fn new(namespace: String, lock_manager: Arc<Mutex<LockManager>>) -> Self {
        let (_runtime, rt) = match tokio::runtime::Handle::try_current() {
            Ok(h) => (None, h),
            Err(_) => {
                let rt = tokio::runtime::Runtime::new().unwrap();
                let handle = rt.handle().clone();
                (Some(rt), handle)
            }
        };
        // connect to external storage server
        // export LIBSQL_STORAGE_SERVER_ADDR=http://libsql-storage-server.internal:5002
        let address = std::env::var("LIBSQL_STORAGE_SERVER_ADDR")
            .unwrap_or("http://127.0.0.1:5002".to_string());
        let client = StorageClient::connect(address);
        let client = tokio::task::block_in_place(|| rt.block_on(client)).unwrap();
        let page_frames = SieveCache::new(1000).unwrap();

        Self {
            namespace,
            conn_id: uuid::Uuid::new_v4().to_string(),
            client: parking_lot::Mutex::new(client),
            frames_cache: page_frames,
            write_cache: BTreeMap::new(),
            lock_manager,
            _runtime,
            rt,
        }

Review comment (suggestion):
I'm not sure it's ok to drop the runtime when it's `Some(_)`. We should just assume that we are running in a tokio runtime, and expect a handle to be available

Code after:
        wal: &mut Self::Wal,
        _db: &mut libsql_sys::wal::Sqlite3Db,
        _sync_flags: std::ffi::c_int,
        _scratch: Option<&mut [u8]>,
    ) -> Result<()> {
        trace!("DurableWalManager::close()");
        wal.end_read_txn();
        Ok(())
    }

    fn destroy_log(&self, _vfs: &mut Vfs, _db_path: &std::ffi::CStr) -> Result<()> {
        trace!("DurableWalManager::destroy_log()");
        Ok(())
    }

    fn log_exists(&self, _vfs: &mut Vfs, _db_path: &std::ffi::CStr) -> Result<bool> {
        trace!("DurableWalManager::log_exists()");
        Ok(true)
    }

    fn destroy(self)
    where
        Self: Sized,
    {
        trace!("DurableWalManager::destroy()");
    }
}

pub struct DurableWal {
    namespace: String,
    conn_id: String,
    client: StorageClient<Channel>,
    frames_cache: SieveCache<std::num::NonZeroU64, Vec<u8>>,
    write_cache: BTreeMap<u32, rpc::Frame>,
    lock_manager: Arc<Mutex<LockManager>>,
}

impl DurableWal {
    async fn new(
        namespace: String,
        config: DurableWalConfig,
        lock_manager: Arc<Mutex<LockManager>>,
    ) -> Self {
        let client = StorageClient::connect(config.storage_server_address)
            .await
            .unwrap();
        let page_frames = SieveCache::new(1000).unwrap();
        Self {
            namespace,
            conn_id: uuid::Uuid::new_v4().to_string(),
            client,