Skip to content

Commit

Permalink
Merge pull request breez#1132 from breez/configure_grpc_connection
Browse files Browse the repository at this point in the history
configure grpc connection for breez server
  • Loading branch information
roeierez authored Dec 1, 2024
2 parents 2857569 + d4c6fae commit a1dad04
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions libs/sdk-common/src/breez_server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use anyhow::Result;
use log::trace;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -30,18 +32,25 @@ pub struct BreezServer {
impl BreezServer {
pub fn new(server_url: String, api_key: Option<String>) -> Result<Self> {
Ok(Self {
grpc_channel: Mutex::new(Endpoint::from_shared(server_url.clone())?.connect_lazy()),
grpc_channel: Mutex::new(Self::create_endpoint(&server_url)?.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();
*self.grpc_channel.lock().await = Self::create_endpoint(&self.server_url)?.connect_lazy();
Ok(())
}

fn create_endpoint(server_url: &str) -> Result<Endpoint> {
Ok(Endpoint::from_shared(server_url.to_string())?
.http2_keep_alive_interval(Duration::new(5, 0))
.tcp_keepalive(Some(Duration::from_secs(5)))
.keep_alive_timeout(Duration::from_secs(5))
.keep_alive_while_idle(true))
}

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 Down

0 comments on commit a1dad04

Please sign in to comment.