Skip to content

Commit 51ffef2

Browse files
committed
refactor: remove unneeded Arc Mutex for pool
1 parent 30dc6dd commit 51ffef2

File tree

4 files changed

+11
-28
lines changed

4 files changed

+11
-28
lines changed

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ mod test;
1010

1111
use std::future::Future;
1212
use std::pin::Pin;
13-
use std::sync::Arc;
1413

1514
use bdk_chain::miniscript;
1615
use bdk_wallet::bitcoin;
1716
use bdk_wallet::chain as bdk_chain;
1817

1918
use sqlx::Database;
2019
use sqlx::Pool;
21-
use tokio::sync::Mutex;
2220

2321
/// Crate error
2422
#[derive(Debug, thiserror::Error)]
@@ -40,7 +38,7 @@ pub enum BdkSqlxError {
4038
/// Manages a pool of database connections.
4139
#[derive(Debug)]
4240
pub struct Store<DB: Database> {
43-
pub(crate) pool: Arc<Mutex<Pool<DB>>>,
41+
pub(crate) pool: Pool<DB>,
4442
wallet_name: String,
4543
migration: bool,
4644
}

src/postgres.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use sqlx::{
2626
postgres::{PgPool, Postgres},
2727
FromRow, Pool, Row, Transaction,
2828
};
29-
use tokio::sync::Mutex;
3029
use tracing::info;
3130

3231
impl AsyncWalletPersister for Store<Postgres> {
@@ -58,7 +57,7 @@ impl Store<Postgres> {
5857
/// Construct a new [`Store`] with an existing pg connection.
5958
#[tracing::instrument]
6059
pub async fn new(
61-
pool: Arc<Mutex<Pool<Postgres>>>,
60+
pool: Pool<Postgres>,
6261
wallet_name: Option<String>,
6362
migration: bool,
6463
) -> Result<Self, BdkSqlxError> {
@@ -82,7 +81,6 @@ impl Store<Postgres> {
8281
info!("new store with url");
8382

8483
let pool = PgPool::connect(url.as_str()).await?;
85-
let pool = Arc::new(Mutex::new(pool));
8684
let wallet_name = wallet_name.unwrap_or_else(|| "bdk_pg_wallet".to_string());
8785

8886
Ok(Self {
@@ -97,15 +95,14 @@ impl Store<Postgres> {
9795
#[tracing::instrument]
9896
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
9997
info!("migrate and read");
100-
let pool = self.pool.lock().await;
10198
if self.migration {
10299
let migrator = Migrator::new(std::path::Path::new("./migrations/postgres/"))
103100
.await
104101
.unwrap();
105-
migrator.run(&*pool).await.unwrap();
102+
migrator.run(&self.pool).await.unwrap();
106103
}
107104

108-
let mut tx = pool.begin().await?;
105+
let mut tx = self.pool.begin().await?;
109106

110107
let mut changeset = ChangeSet::default();
111108

@@ -180,8 +177,7 @@ impl Store<Postgres> {
180177
}
181178

182179
let wallet_name = &self.wallet_name;
183-
let pool = self.pool.lock().await;
184-
let mut tx = pool.begin().await?;
180+
let mut tx = self.pool.begin().await?;
185181

186182
if let Some(ref descriptor) = changeset.descriptor {
187183
insert_descriptor(&mut tx, wallet_name, descriptor, External).await?;

src/sqlite.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use sqlx::{
2626
sqlite::{SqlitePool, SqlitePoolOptions},
2727
};
2828
use sqlx::{sqlite::Sqlite, FromRow, Pool, Row, Transaction};
29-
use tokio::sync::Mutex;
3029
use tracing::info;
3130

3231
impl AsyncWalletPersister for Store<Sqlite> {
@@ -58,7 +57,7 @@ impl Store<Sqlite> {
5857
/// Construct a new [`Store`] with an existing sqlite connection.
5958
#[tracing::instrument]
6059
pub async fn new(
61-
pool: Arc<Mutex<Pool<Sqlite>>>,
60+
pool: Pool<Sqlite>,
6261
wallet_name: Option<String>,
6362
migration: bool,
6463
) -> Result<Self, BdkSqlxError> {
@@ -98,7 +97,6 @@ impl Store<Sqlite> {
9897
.connect(":memory:")
9998
.await?
10099
};
101-
let pool = Arc::new(Mutex::new(pool));
102100
let wallet_name = wallet_name.unwrap_or_else(|| "bdk_sqlite_wallet".to_string());
103101

104102
Ok(Self {
@@ -113,15 +111,14 @@ impl Store<Sqlite> {
113111
#[tracing::instrument]
114112
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
115113
info!("migrate and read");
116-
let pool = self.pool.lock().await;
117114
if self.migration {
118115
let migrator = Migrator::new(std::path::Path::new("./migrations/sqlite/"))
119116
.await
120117
.unwrap();
121-
migrator.run(&*pool).await.unwrap();
118+
migrator.run(&self.pool).await.unwrap();
122119
}
123120

124-
let mut tx = pool.begin().await?;
121+
let mut tx = self.pool.begin().await?;
125122

126123
let mut changeset = ChangeSet::default();
127124

@@ -196,8 +193,7 @@ impl Store<Sqlite> {
196193
}
197194

198195
let wallet_name = &self.wallet_name;
199-
let pool = self.pool.lock().await;
200-
let mut tx = pool.begin().await?;
196+
let mut tx = self.pool.begin().await?;
201197

202198
if let Some(ref descriptor) = changeset.descriptor {
203199
insert_descriptor(&mut tx, wallet_name, descriptor, External).await?;

src/test.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use std::io::Write;
2626
use std::time::Duration;
2727
use std::sync::Arc;
2828
use std::sync::Once;
29-
use tokio::sync::Mutex;
3029
use tracing_subscriber::layer::SubscriberExt;
3130
use tracing_subscriber::util::SubscriberInitExt;
3231
use tracing_subscriber::EnvFilter;
@@ -645,12 +644,7 @@ async fn wallet_is_persisted_sqlite() -> anyhow::Result<()> {
645644
)?;
646645

647646
// Create a new wallet, use sqlite in memory DB
648-
let mut store = Store::<Sqlite>::new(
649-
Arc::new(Mutex::new(pool.clone())),
650-
Some(wallet_name.clone()),
651-
true,
652-
)
653-
.await?;
647+
let mut store = Store::<Sqlite>::new(pool.clone(), Some(wallet_name.clone()), true).await?;
654648
let mut wallet = Wallet::create(external_desc, internal_desc)
655649
.network(NETWORK)
656650
.create_wallet_async(&mut store)
@@ -666,8 +660,7 @@ async fn wallet_is_persisted_sqlite() -> anyhow::Result<()> {
666660

667661
{
668662
// Recover the wallet
669-
let mut store =
670-
Store::<Sqlite>::new(Arc::new(Mutex::new(pool)), Some(wallet_name), false).await?;
663+
let mut store = Store::<Sqlite>::new(pool, Some(wallet_name), false).await?;
671664
let wallet = Wallet::load()
672665
.descriptor(KeychainKind::External, Some(external_desc))
673666
.descriptor(KeychainKind::Internal, Some(internal_desc))

0 commit comments

Comments
 (0)