From 3ac9c199db9b0a47c4c81fd68f5a4e5d89e56faf Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 22 Jun 2025 23:14:14 +0100 Subject: [PATCH] feat: create new ClnRpc for each request This removes the mutex from the ClnRpc client avoiding blocking the mint. cln-grpc handles this in the same way. https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=c-lightning --- crates/cdk-cln/src/lib.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/crates/cdk-cln/src/lib.rs b/crates/cdk-cln/src/lib.rs index ca058b77..cfb94432 100644 --- a/crates/cdk-cln/src/lib.rs +++ b/crates/cdk-cln/src/lib.rs @@ -32,7 +32,6 @@ use cln_rpc::primitives::{Amount as CLN_Amount, AmountOrAny}; use error::Error; use futures::{Stream, StreamExt}; use serde_json::Value; -use tokio::sync::Mutex; use tokio_util::sync::CancellationToken; use uuid::Uuid; @@ -42,7 +41,6 @@ pub mod error; #[derive(Clone)] pub struct Cln { rpc_socket: PathBuf, - cln_client: Arc>, fee_reserve: FeeReserve, wait_invoice_cancel_token: CancellationToken, wait_invoice_is_active: Arc, @@ -51,11 +49,8 @@ pub struct Cln { impl Cln { /// Create new [`Cln`] pub async fn new(rpc_socket: PathBuf, fee_reserve: FeeReserve) -> Result { - let cln_client = cln_rpc::ClnRpc::new(&rpc_socket).await?; - Ok(Self { rpc_socket, - cln_client: Arc::new(Mutex::new(cln_client)), fee_reserve, wait_invoice_cancel_token: CancellationToken::new(), wait_invoice_is_active: Arc::new(AtomicBool::new(false)), @@ -243,7 +238,7 @@ impl MintPayment for Cln { }) .flatten(); - let mut cln_client = self.cln_client.lock().await; + let mut cln_client = cln_rpc::ClnRpc::new(&self.rpc_socket).await?; let cln_response = cln_client .call_typed(&PayRequest { bolt11: melt_quote.request.to_string(), @@ -313,7 +308,7 @@ impl MintPayment for Cln { ) -> Result { let time_now = unix_time(); - let mut cln_client = self.cln_client.lock().await; + let mut cln_client = cln_rpc::ClnRpc::new(&self.rpc_socket).await?; let label = Uuid::new_v4().to_string(); @@ -350,7 +345,7 @@ impl MintPayment for Cln { &self, payment_hash: &str, ) -> Result { - let mut cln_client = self.cln_client.lock().await; + let mut cln_client = cln_rpc::ClnRpc::new(&self.rpc_socket).await?; let listinvoices_response = cln_client .call_typed(&ListinvoicesRequest { @@ -383,7 +378,7 @@ impl MintPayment for Cln { &self, payment_hash: &str, ) -> Result { - let mut cln_client = self.cln_client.lock().await; + let mut cln_client = cln_rpc::ClnRpc::new(&self.rpc_socket).await?; let listpays_response = cln_client .call_typed(&ListpaysRequest { @@ -425,7 +420,7 @@ impl MintPayment for Cln { impl Cln { /// Get last pay index for cln async fn get_last_pay_index(&self) -> Result, Error> { - let mut cln_client = self.cln_client.lock().await; + let mut cln_client = cln_rpc::ClnRpc::new(&self.rpc_socket).await?; let listinvoices_response = cln_client .call_typed(&ListinvoicesRequest { index: None,