Skip to content

Commit

Permalink
Extra logs + parking_lot (#925)
Browse files Browse the repository at this point in the history
* add logging for raw_conn
* replace RwLock with parking_log
  • Loading branch information
insipx authored Jul 26, 2024
1 parent d0891e7 commit d6c0103
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions xmtp_mls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ed25519-dalek = "2.1.1"
ethers.workspace = true
ethers-core.workspace = true
futures.workspace = true
parking_lot = "0.12.3"
hex.workspace = true
libsqlite3-sys = { version = "0.28.0", optional = true }
log.workspace = true
Expand Down
20 changes: 12 additions & 8 deletions xmtp_mls/src/storage/encrypted_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ pub mod key_store_entry;
pub mod refresh_state;
pub mod schema;

use std::{
borrow::Cow,
sync::{Arc, RwLock},
};
use std::{borrow::Cow, sync::Arc};

use diesel::{
connection::{AnsiTransactionManager, SimpleConnection, TransactionManager},
Expand All @@ -35,6 +32,7 @@ use diesel::{
};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use log::{log_enabled, warn};
use parking_lot::RwLock;
use rand::RngCore;
use xmtp_cryptography::utils as crypto_utils;

Expand Down Expand Up @@ -180,12 +178,18 @@ impl EncryptedMessageStore {
fn raw_conn(
&self,
) -> Result<PooledConnection<ConnectionManager<SqliteConnection>>, StorageError> {
let pool_guard = self.pool.read()?;
let pool_guard = self.pool.read();

let pool = pool_guard
.as_ref()
.ok_or(StorageError::PoolNeedsConnection)?;

log::info!(
"Pulling connection from pool, idle_connections={}, total_connections={}",
pool.state().idle_connections,
pool.state().connections
);

let mut conn = pool.get()?;
if let Some(ref key) = self.enc_key {
conn.batch_execute(&format!("PRAGMA key = \"x'{}'\";", hex::encode(key)))?;
Expand Down Expand Up @@ -314,7 +318,7 @@ impl EncryptedMessageStore {
}

pub fn release_connection(&self) -> Result<(), StorageError> {
let mut pool_guard = self.pool.write()?;
let mut pool_guard = self.pool.write();
pool_guard.take();
Ok(())
}
Expand All @@ -330,7 +334,7 @@ impl EncryptedMessageStore {
.build(ConnectionManager::<SqliteConnection>::new(path))?,
};

let mut pool_write = self.pool.write()?;
let mut pool_write = self.pool.write();
*pool_write = Some(pool);

Ok(())
Expand Down Expand Up @@ -534,7 +538,7 @@ mod tests {
assert_eq!(fetched_identity.inbox_id, inbox_id);

store.release_connection().unwrap();
assert!(store.pool.read().unwrap().is_none());
assert!(store.pool.read().is_none());
store.reconnect().unwrap();
let fetched_identity2: StoredIdentity = conn.fetch(&()).unwrap().unwrap();

Expand Down

0 comments on commit d6c0103

Please sign in to comment.