• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tari-project / tari / 16123384529

07 Jul 2025 05:11PM UTC coverage: 64.327% (-7.6%) from 71.89%
16123384529

push

github

web-flow
chore: new release v4.9.0-pre.0 (#7289)

Description
---
new release esmeralda

77151 of 119935 relevant lines covered (64.33%)

227108.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.63
/common_sqlite/src/sqlite_connection_pool.rs
1
// Copyright 2020. The Tari Project
2
//
3
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4
// following conditions are met:
5
//
6
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7
// disclaimer.
8
//
9
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10
// following disclaimer in the documentation and/or other materials provided with the distribution.
11
//
12
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13
// products derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
21
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22

23
use core::time::Duration;
24
use std::{convert::TryFrom, path::PathBuf};
25

26
use diesel::{
27
    r2d2::{ConnectionManager, Pool, PooledConnection},
28
    SqliteConnection,
29
};
30
use log::*;
31

32
use crate::{connection_options::ConnectionOptions, error::SqliteStorageError};
33

34
const LOG_TARGET: &str = "common_sqlite::sqlite_connection_pool";
35

36
#[derive(Clone)]
37
pub struct SqliteConnectionPool {
38
    pool: Option<Pool<ConnectionManager<SqliteConnection>>>,
39
    db_path: String,
40
    pool_size: usize,
41
    connection_options: ConnectionOptions,
42
}
43

44
impl SqliteConnectionPool {
45
    pub fn new(
611✔
46
        db_path: String,
611✔
47
        pool_size: usize,
611✔
48
        enable_wal: bool,
611✔
49
        enable_foreign_keys: bool,
611✔
50
        busy_timeout: Duration,
611✔
51
    ) -> Self {
611✔
52
        Self {
611✔
53
            pool: None,
611✔
54
            db_path,
611✔
55
            pool_size,
611✔
56
            connection_options: ConnectionOptions::new(enable_wal, enable_foreign_keys, busy_timeout),
611✔
57
        }
611✔
58
    }
611✔
59

60
    /// Create an sqlite connection pool managed by the pool connection manager
61
    pub fn create_pool(&mut self) -> Result<(), SqliteStorageError> {
611✔
62
        if self.pool.is_none() {
611✔
63
            let pool = Pool::builder()
611✔
64
                .max_size(u32::try_from(self.pool_size)?)
611✔
65
                .connection_customizer(Box::new(self.connection_options.clone()))
611✔
66
                .build(ConnectionManager::<SqliteConnection>::new(self.db_path.as_str()))
611✔
67
                .map_err(|e| SqliteStorageError::DieselR2d2Error(e.to_string()));
611✔
68
            self.pool = Some(pool?);
611✔
69
        } else {
70
            warn!(
×
71
                target: LOG_TARGET,
×
72
                "Connection pool for {} already exists", self.db_path
×
73
            );
74
        }
75
        Ok(())
611✔
76
    }
611✔
77

78
    /// Return a pooled sqlite connection managed by the pool connection manager, waits for at most the configured
79
    /// connection timeout before returning an error.
80
    pub fn get_pooled_connection(
26,364✔
81
        &self,
26,364✔
82
    ) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>, SqliteStorageError> {
26,364✔
83
        if let Some(pool) = self.pool.as_ref() {
26,364✔
84
            pool.get().map_err(|e| {
26,364✔
85
                warn!(
×
86
                    target: LOG_TARGET,
×
87
                    "Connection pool state {:?}: {}",
×
88
                    pool.state(),
×
89
                    e
90
                );
91
                SqliteStorageError::DieselR2d2Error(e.to_string())
×
92
            })
26,364✔
93
        } else {
94
            Err(SqliteStorageError::DieselR2d2Error("Pool does not exist".to_string()))
×
95
        }
96
    }
26,364✔
97

98
    /// Return a pooled sqlite connection managed by the pool connection manager, waits for at most supplied
99
    /// connection timeout before returning an error.
100
    pub fn get_pooled_connection_timeout(
×
101
        &self,
×
102
        timeout: Duration,
×
103
    ) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>, SqliteStorageError> {
×
104
        if let Some(pool) = self.pool.clone() {
×
105
            pool.get_timeout(timeout).map_err(|e| {
×
106
                warn!(
×
107
                    target: LOG_TARGET,
×
108
                    "Connection pool state {:?}: {}",
×
109
                    pool.state(),
×
110
                    e
111
                );
112
                SqliteStorageError::DieselR2d2Error(e.to_string())
×
113
            })
×
114
        } else {
115
            Err(SqliteStorageError::DieselR2d2Error("Pool does not exist".to_string()))
×
116
        }
117
    }
×
118

119
    /// Return a pooled sqlite connection managed by the pool connection manager, returns None if there are no idle
120
    /// connections available in the pool. This method will not block waiting to establish a new connection.
121
    pub fn try_get_pooled_connection(
×
122
        &self,
×
123
    ) -> Result<Option<PooledConnection<ConnectionManager<SqliteConnection>>>, SqliteStorageError> {
×
124
        if let Some(pool) = self.pool.clone() {
×
125
            let connection = pool.try_get();
×
126
            if connection.is_none() {
×
127
                warn!(
×
128
                    target: LOG_TARGET,
×
129
                    "No connections available, pool state {:?}",
×
130
                    pool.state()
×
131
                );
132
            };
×
133
            Ok(connection)
×
134
        } else {
135
            Err(SqliteStorageError::DieselR2d2Error("Pool does not exist".to_string()))
×
136
        }
137
    }
×
138

139
    /// Return the database path
140
    pub fn db_path(&self) -> PathBuf {
2,264✔
141
        PathBuf::from(&self.db_path)
2,264✔
142
    }
2,264✔
143

144
    /// Perform cleanup on the connection pool. This will drop the pool and return the state of the pool.
145
    pub fn cleanup(&mut self) -> Option<String> {
183✔
146
        if let Some(pool) = self.pool.take() {
183✔
147
            let state = format!("{:?}", pool.state());
183✔
148
            drop(pool);
183✔
149
            return Some(state);
183✔
150
        }
×
151
        None
×
152
    }
183✔
153
}
154

155
pub trait PooledDbConnection: Send + Sync + Clone {
156
    type Error;
157

158
    fn get_pooled_connection(&self) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>, Self::Error>;
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc