pool.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. use crate::{errors::*, pragma::*};
  2. use diesel::{connection::Connection, SqliteConnection};
  3. use r2d2::{CustomizeConnection, ManageConnection, Pool};
  4. use scheduled_thread_pool::ScheduledThreadPool;
  5. use std::{
  6. sync::{
  7. atomic::{AtomicUsize, Ordering::SeqCst},
  8. Arc,
  9. },
  10. time::Duration,
  11. };
  12. lazy_static::lazy_static! {
  13. static ref DB_POOL: Arc<ScheduledThreadPool> = Arc::new(
  14. ScheduledThreadPool::with_name("db-pool-{}:", 4)
  15. );
  16. }
  17. pub struct ConnectionPool {
  18. pub(crate) inner: Pool<ConnectionManager>,
  19. }
  20. impl std::ops::Deref for ConnectionPool {
  21. type Target = Pool<ConnectionManager>;
  22. fn deref(&self) -> &Self::Target { &self.inner }
  23. }
  24. impl ConnectionPool {
  25. pub fn new<T>(config: PoolConfig, uri: T) -> Result<Self>
  26. where
  27. T: Into<String>,
  28. {
  29. let manager = ConnectionManager::new(uri);
  30. let thread_pool = DB_POOL.clone();
  31. let config = Arc::new(config);
  32. let customizer_config = DatabaseCustomizerConfig::default();
  33. let pool = r2d2::Pool::builder()
  34. .thread_pool(thread_pool)
  35. .min_idle(Some(config.min_idle))
  36. .connection_customizer(Box::new(DatabaseCustomizer::new(customizer_config)))
  37. .max_size(config.max_size)
  38. .max_lifetime(None)
  39. .connection_timeout(config.connection_timeout)
  40. .idle_timeout(Some(config.idle_timeout))
  41. .build_unchecked(manager);
  42. Ok(ConnectionPool { inner: pool })
  43. }
  44. }
  45. #[derive(Default, Debug, Clone)]
  46. pub struct ConnCounter(Arc<ConnCounterInner>);
  47. impl std::ops::Deref for ConnCounter {
  48. type Target = ConnCounterInner;
  49. fn deref(&self) -> &Self::Target { &*self.0 }
  50. }
  51. #[derive(Default, Debug)]
  52. pub struct ConnCounterInner {
  53. max_number: AtomicUsize,
  54. current_number: AtomicUsize,
  55. }
  56. impl ConnCounterInner {
  57. pub fn get_max_num(&self) -> usize { self.max_number.load(SeqCst) }
  58. pub fn reset(&self) {
  59. // reset max_number to current_number
  60. let _ = self
  61. .max_number
  62. .fetch_update(SeqCst, SeqCst, |_| Some(self.current_number.load(SeqCst)));
  63. }
  64. }
  65. pub type OnExecFunc = Box<dyn Fn() -> Box<dyn Fn(&SqliteConnection, &str)> + Send + Sync>;
  66. pub struct PoolConfig {
  67. min_idle: u32,
  68. max_size: u32,
  69. connection_timeout: Duration,
  70. idle_timeout: Duration,
  71. }
  72. impl Default for PoolConfig {
  73. fn default() -> Self {
  74. Self {
  75. min_idle: 1,
  76. max_size: 10,
  77. connection_timeout: Duration::from_secs(10),
  78. idle_timeout: Duration::from_secs(5 * 60),
  79. }
  80. }
  81. }
  82. impl PoolConfig {
  83. #[allow(dead_code)]
  84. pub fn min_idle(mut self, min_idle: u32) -> Self {
  85. self.min_idle = min_idle;
  86. self
  87. }
  88. #[allow(dead_code)]
  89. pub fn max_size(mut self, max_size: u32) -> Self {
  90. self.max_size = max_size;
  91. self
  92. }
  93. }
  94. pub struct ConnectionManager {
  95. db_uri: String,
  96. }
  97. impl ManageConnection for ConnectionManager {
  98. type Connection = SqliteConnection;
  99. type Error = crate::Error;
  100. fn connect(&self) -> Result<Self::Connection> { Ok(SqliteConnection::establish(&self.db_uri)?) }
  101. fn is_valid(&self, conn: &mut Self::Connection) -> Result<()> {
  102. Ok(conn.execute("SELECT 1").map(|_| ())?)
  103. }
  104. fn has_broken(&self, _conn: &mut Self::Connection) -> bool { false }
  105. }
  106. impl ConnectionManager {
  107. pub fn new<S: Into<String>>(uri: S) -> Self { ConnectionManager { db_uri: uri.into() } }
  108. }
  109. #[derive(Debug)]
  110. pub struct DatabaseCustomizerConfig {
  111. pub(crate) journal_mode: SQLiteJournalMode,
  112. pub(crate) synchronous: SQLiteSynchronous,
  113. pub(crate) busy_timeout: i32,
  114. pub(crate) secure_delete: bool,
  115. }
  116. impl Default for DatabaseCustomizerConfig {
  117. fn default() -> Self {
  118. Self {
  119. journal_mode: SQLiteJournalMode::WAL,
  120. synchronous: SQLiteSynchronous::NORMAL,
  121. busy_timeout: 5000,
  122. secure_delete: true,
  123. }
  124. }
  125. }
  126. #[derive(Debug)]
  127. struct DatabaseCustomizer {
  128. config: DatabaseCustomizerConfig,
  129. }
  130. impl DatabaseCustomizer {
  131. fn new(config: DatabaseCustomizerConfig) -> Self
  132. where
  133. Self: Sized,
  134. {
  135. Self { config }
  136. }
  137. }
  138. impl CustomizeConnection<SqliteConnection, crate::Error> for DatabaseCustomizer {
  139. fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<()> {
  140. conn.pragma_set_busy_timeout(self.config.busy_timeout)?;
  141. if self.config.journal_mode != SQLiteJournalMode::WAL {
  142. conn.pragma_set_journal_mode(self.config.journal_mode, None)?;
  143. }
  144. conn.pragma_set_synchronous(self.config.synchronous, None)?;
  145. Ok(())
  146. }
  147. }