Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra logs + parking_lot #925

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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