switchgear_pingora/
pool.rs1use crate::{PingoraLnClientPool, PingoraLnMetrics, PingoraLnMetricsCache};
2use async_trait::async_trait;
3use pingora_load_balancing::Backend;
4use switchgear_components::pool::error::LnPoolError;
5use switchgear_components::pool::LnClientPool;
6use switchgear_service_api::discovery::DiscoveryBackend;
7use switchgear_service_api::offer::Offer;
8
9#[derive(Clone)]
10pub struct DefaultPingoraLnClientPool {
11 pool: LnClientPool<Backend>,
12}
13
14impl DefaultPingoraLnClientPool {
15 pub fn new(pool: LnClientPool<Backend>) -> Self {
16 Self { pool }
17 }
18}
19
20#[async_trait]
21impl PingoraLnClientPool for DefaultPingoraLnClientPool {
22 type Error = LnPoolError;
23 type Key = Backend;
24
25 async fn get_invoice(
26 &self,
27 offer: &Offer,
28 key: &Self::Key,
29 amount_msat: Option<u64>,
30 expiry_secs: Option<u64>,
31 ) -> Result<String, Self::Error> {
32 self.pool
33 .get_invoice(offer, key, amount_msat, expiry_secs)
34 .await
35 }
36
37 async fn get_metrics(&self, key: &Self::Key) -> Result<PingoraLnMetrics, Self::Error> {
38 let metrics = self.pool.get_metrics(key).await?;
39 Ok(PingoraLnMetrics {
40 healthy: metrics.healthy,
41 node_effective_inbound_msat: metrics.node_effective_inbound_msat,
42 })
43 }
44
45 fn connect(&self, key: Self::Key, backend: &DiscoveryBackend) -> Result<(), Self::Error> {
46 self.pool.connect(key, backend)
47 }
48}
49
50impl PingoraLnMetricsCache for DefaultPingoraLnClientPool {
51 type Key = Backend;
52
53 fn get_cached_metrics(&self, key: &Self::Key) -> Option<PingoraLnMetrics> {
54 let metrics = self.pool.get_cached_metrics(key);
55 metrics.map(|m| PingoraLnMetrics {
56 healthy: m.healthy,
57 node_effective_inbound_msat: m.node_effective_inbound_msat,
58 })
59 }
60}