alternative to using 'await' with lazy_static! macro in rust?

3.2k Views Asked by At

I want to use Async MongoDB in a project.

I don't want to pass around the client because it would need to go around multiple tasks and threads. So I kept a static client using lazy_static. However, I can't use await in the initialization block.

What can I do to work around this?

Suggestions for doing it without lazy_static are also welcome.

use std::env;
use futures::stream::StreamExt;
use mongodb::{
    bson::{doc, Bson},
    options::ClientOptions,
    Client,
};

lazy_static! {
    static ref MONGO: Option<Client> = {
        if let Ok(token) = env::var("MONGO_AUTH") {
            if let Ok(client_options) = ClientOptions::parse(&token).await
                                                                     ^^^^^
            {
                if let Ok(client) = Client::with_options(client_options) {
                    return Some(client);
                }
            }
        }
        return None;
    };
}
4

There are 4 best solutions below

0
On BEST ANSWER

I went with this approach based on someone's suggestion in rust forums.

static MONGO: OnceCell<Client> = OnceCell::new();
static MONGO_INITIALIZED: OnceCell<tokio::sync::Mutex<bool>> = OnceCell::new();

pub async fn get_mongo() -> Option<&'static Client> {
    // this is racy, but that's OK: it's just a fast case
    let client_option = MONGO.get();
    if let Some(_) = client_option {
        return client_option;
    }
    // it hasn't been initialized yet, so let's grab the lock & try to
    // initialize it
    let initializing_mutex = MONGO_INITIALIZED.get_or_init(|| tokio::sync::Mutex::new(false));

    // this will wait if another task is currently initializing the client
    let mut initialized = initializing_mutex.lock().await;
    // if initialized is true, then someone else initialized it while we waited,
    // and we can just skip this part.
    if !*initialized {
        // no one else has initialized it yet, so

        if let Ok(token) = env::var("MONGO_AUTH") {
            if let Ok(client_options) = ClientOptions::parse(&token).await {
                if let Ok(client) = Client::with_options(client_options) {
                    if let Ok(_) = MONGO.set(client) {
                        *initialized = true;
                    }
                }
            }
        }
    }
    drop(initialized);
    MONGO.get()
}
1
On

However I can't use await in the initialization block.

You can skirt this with futures::executor::block_on

use once_cell::sync::Lazy;
// ...
static PGCLIENT: Lazy<Client> = Lazy::new(|| {
    let client: Client = futures::executor::block_on(async {
        let (client, connection) = tokio_postgres::connect(
            "postgres:///?user=ecarroll&port=5432&host=/run/postgresql",
            NoTls,
        )
        .await
        .unwrap();

        tokio::spawn(async move {
            if let Err(e) = connection.await {
                eprintln!("connection error: {}", e);
            }
        });

        client
    });
    client
});

What we have is a non-async closure blocking in a single thread until the resolution of the future.

1
On

Create a new runtime from tokio::runtime::Runtime and use block_on to block the current thread until completion.

// database.rs
use tokio::runtime::Runtime;
use mongodb::Client;

pub fn connect_sync() -> Client {
    Runtime::new().unwrap().block_on(async {
        Client::with_uri_str("mongodb://localhost:27017").await.unwrap()
    })
}
// main.rs
mod database;

lazy_static! {
    static ref CLIENT: mongodb::Client =  database::connect_sync();
}

#[actix_web::main]
async fn main() {
   let collection = &CLIENT.database("db_name").collection("coll_name");
   // ...
}
0
On

Use the async_once crate.

use async_once::AsyncOnce;
use lazy_static::lazy_static;
use mongodb::Client;

lazy_static! {
    static ref CLIENT: AsyncOnce<Client> = AsyncOnce::new(async {
        Client::with_uri_str(std::env::var("MONGO_URL").expect("MONGO_URL not set"))
            .await
            .unwrap()
    });
}

then

CLIENT.get().await;