Skip to content

Commit

Permalink
reconnect breez server on hibernation
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Oct 29, 2024
1 parent 504c4d1 commit 696ca0e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion libs/sdk-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
strum_macros = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tonic = { workspace = true, features = [
"tls",
"transport",
Expand All @@ -35,7 +36,6 @@ urlencoding = { version = "2.1.3" }
[dev-dependencies]
bitcoin = { workspace = true, features = ["rand"] }
mockito = { workspace = true }
tokio = { workspace = true }
once_cell = { workspace = true }

[build-dependencies]
Expand Down
25 changes: 17 additions & 8 deletions libs/sdk-common/src/breez_server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use log::trace;
use tokio::sync::Mutex;
use tonic::codegen::InterceptedService;
use tonic::metadata::errors::InvalidMetadataValue;
use tonic::metadata::{Ascii, MetadataValue};
Expand All @@ -20,18 +21,26 @@ pub static PRODUCTION_BREEZSERVER_URL: &str = "https://bs1.breez.technology:443"
pub static STAGING_BREEZSERVER_URL: &str = "https://bs1-st.breez.technology:443";

pub struct BreezServer {
grpc_channel: Channel,
grpc_channel: Mutex<Channel>,
api_key: Option<String>,
server_url: String,
}

impl BreezServer {
pub fn new(server_url: String, api_key: Option<String>) -> Result<Self> {
Ok(Self {
grpc_channel: Endpoint::from_shared(server_url)?.connect_lazy(),
grpc_channel: Mutex::new(Endpoint::from_shared(server_url.clone())?.connect_lazy()),
api_key,
server_url,
})
}

pub async fn reconnect(&self) -> Result<()> {
*self.grpc_channel.lock().await =
Endpoint::from_shared(self.server_url.clone())?.connect_lazy();
Ok(())
}

fn api_key_metadata(&self) -> Result<Option<MetadataValue<Ascii>>, ServiceConnectivityError> {
match &self.api_key {
Some(key) => Ok(Some(format!("Bearer {key}").parse().map_err(
Expand All @@ -54,22 +63,22 @@ impl BreezServer {
> {
let api_key_metadata = self.api_key_metadata()?;
let with_interceptor = ChannelOpenerClient::with_interceptor(
self.grpc_channel.clone(),
self.grpc_channel.lock().await.clone(),
ApiKeyInterceptor { api_key_metadata },
);
Ok(with_interceptor)
}

pub async fn get_payment_notifier_client(&self) -> PaymentNotifierClient<Channel> {
PaymentNotifierClient::new(self.grpc_channel.clone())
PaymentNotifierClient::new(self.grpc_channel.lock().await.clone())
}

pub async fn get_information_client(&self) -> InformationClient<Channel> {
InformationClient::new(self.grpc_channel.clone())
InformationClient::new(self.grpc_channel.lock().await.clone())
}

pub async fn get_signer_client(&self) -> SignerClient<Channel> {
SignerClient::new(self.grpc_channel.clone())
SignerClient::new(self.grpc_channel.lock().await.clone())
}

pub async fn get_support_client(
Expand All @@ -80,13 +89,13 @@ impl BreezServer {
> {
let api_key_metadata = self.api_key_metadata()?;
Ok(SupportClient::with_interceptor(
self.grpc_channel.clone(),
self.grpc_channel.lock().await.clone(),
ApiKeyInterceptor { api_key_metadata },
))
}

pub async fn get_swapper_client(&self) -> SwapperClient<Channel> {
SwapperClient::new(self.grpc_channel.clone())
SwapperClient::new(self.grpc_channel.lock().await.clone())
}

pub async fn ping(&self) -> Result<String> {
Expand Down
14 changes: 14 additions & 0 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2435,6 +2435,20 @@ impl BreezServicesBuilder {
}
});

// Reconnect breez server on hibernation.
let cloned_breez_server = breez_server.clone();
let mut cloned_hibernation_receiver = hibernation_receiver.clone();
tokio::spawn(async move {
loop {
if cloned_hibernation_receiver.changed().await.is_err() {
return;
}

debug!("reconnecting breez server");
let _ = cloned_breez_server.reconnect().await;
}
});

let current_lsp_id = persister.get_lsp_id()?;
if current_lsp_id.is_none() && self.config.default_lsp_id.is_some() {
persister.set_lsp(self.config.default_lsp_id.clone().unwrap(), None)?;
Expand Down
1 change: 1 addition & 0 deletions tools/sdk-cli/Cargo.lock

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

0 comments on commit 696ca0e

Please sign in to comment.