cuprate_txpool/service/
free.rs

1use std::sync::Arc;
2
3use rayon::ThreadPool;
4
5use cuprate_database::{ConcreteEnv, InitError};
6
7use crate::{
8    service::{
9        read::{init_read_service, init_read_service_with_pool},
10        types::{TxpoolReadHandle, TxpoolWriteHandle},
11        write::init_write_service,
12    },
13    Config,
14};
15
16//---------------------------------------------------------------------------------------------------- Init
17#[cold]
18#[inline(never)] // Only called once (?)
19/// Initialize a database & thread-pool, and return a read/write handle to it.
20///
21/// Once the returned handles are [`Drop::drop`]ed, the reader
22/// thread-pool and writer thread will exit automatically.
23///
24/// # Errors
25/// This will forward the error if [`crate::open`] failed.
26pub fn init(
27    config: Config,
28) -> Result<(TxpoolReadHandle, TxpoolWriteHandle, Arc<ConcreteEnv>), InitError> {
29    let reader_threads = config.reader_threads;
30
31    // Initialize the database itself.
32    let db = Arc::new(crate::open(config)?);
33
34    // Spawn the Reader thread pool and Writer.
35    let readers = init_read_service(Arc::clone(&db), reader_threads);
36    let writer = init_write_service(Arc::clone(&db));
37
38    Ok((readers, writer, db))
39}
40
41#[cold]
42#[inline(never)] // Only called once (?)
43/// Initialize a database, and return a read/write handle to it.
44///
45/// Unlike [`init`] this will not create a thread-pool, instead using
46/// the one passed in.
47///
48/// Once the returned handles are [`Drop::drop`]ed, the reader
49/// thread-pool and writer thread will exit automatically.
50///
51/// # Errors
52/// This will forward the error if [`crate::open`] failed.
53pub fn init_with_pool(
54    config: Config,
55    pool: Arc<ThreadPool>,
56) -> Result<(TxpoolReadHandle, TxpoolWriteHandle, Arc<ConcreteEnv>), InitError> {
57    // Initialize the database itself.
58    let db = Arc::new(crate::open(config)?);
59
60    // Spawn the Reader thread pool and Writer.
61    let readers = init_read_service_with_pool(Arc::clone(&db), pool);
62    let writer = init_write_service(Arc::clone(&db));
63
64    Ok((readers, writer, db))
65}