Skip to main content

cuprate_txpool/service/
free.rs

1use std::sync::Arc;
2
3use rayon::ThreadPool;
4
5use crate::{
6    error::TxPoolError,
7    service::{TxpoolReadHandle, TxpoolWriteHandle},
8    txpool::TxpoolDatabase,
9};
10
11//---------------------------------------------------------------------------------------------------- Init
12#[cold]
13#[inline(never)] // Only called once (?)
14/// Initialise a database and return a read/write handle to it.
15///
16/// Once the returned handles are [`Drop::drop`]ed, the reader
17/// thread-pool and writer thread will exit automatically.
18///
19/// # Errors
20/// This will forward the error if the opening failed.
21pub fn init_with_pool(
22    database: fjall::Database,
23    pool: Arc<ThreadPool>,
24) -> Result<(TxpoolReadHandle, TxpoolWriteHandle), TxPoolError> {
25    let database = Arc::new(TxpoolDatabase::open_with_database(database)?);
26
27    // Spawn the Reader thread pool and Writer.
28    let readers = TxpoolReadHandle {
29        txpool: Arc::clone(&database),
30        pool: Arc::clone(&pool),
31    };
32    let writer = TxpoolWriteHandle {
33        txpool: Arc::clone(&database),
34        pool,
35    };
36
37    Ok((readers, writer))
38}