Skip to content

Commit 7bde9d2

Browse files
Merge pull request fedimint#6127 from m1sterc001guy/fix_registration
fix: gateway registration exits early
2 parents cc2577c + 443939c commit 7bde9d2

File tree

2 files changed

+56
-46
lines changed

2 files changed

+56
-46
lines changed

gateway/ln-gateway/src/lib.rs

+46-41
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ use rpc::{
9494
};
9595
use state_machine::{GatewayClientModule, GatewayExtPayStates};
9696
use tokio::sync::RwLock;
97-
use tracing::{debug, error, info, info_span, warn, Instrument};
97+
use tracing::{debug, error, info, info_span, warn};
9898

9999
use crate::config::LightningModuleMode;
100100
use crate::db::{get_gatewayd_database_migrations, FederationConfig};
@@ -1163,14 +1163,14 @@ impl Gateway {
11631163
Self::check_lnv1_federation_network(&client, gateway_config.network).await?;
11641164
client
11651165
.get_first_module::<GatewayClientModule>()?
1166-
.register_with_federation(
1166+
.try_register_with_federation(
11671167
// Route hints will be updated in the background
11681168
Vec::new(),
11691169
GW_ANNOUNCEMENT_TTL,
11701170
federation_config.fees,
11711171
lightning_context,
11721172
)
1173-
.await?;
1173+
.await;
11741174
}
11751175

11761176
if self.is_running_lnv2() {
@@ -1361,14 +1361,23 @@ impl Gateway {
13611361

13621362
// If 'num_route_hints' is provided, all federations must be re-registered.
13631363
// Otherwise, only those affected by the new fees need to be re-registered.
1364+
let register_task_group = TaskGroup::new();
13641365
if num_route_hints.is_some() {
13651366
let all_federations_configs: Vec<_> =
13661367
dbtx.load_federation_configs().await.into_iter().collect();
1367-
self.register_federations(&new_gateway_config, &all_federations_configs)
1368-
.await?;
1368+
self.register_federations(
1369+
&new_gateway_config,
1370+
&all_federations_configs,
1371+
&register_task_group,
1372+
)
1373+
.await;
13691374
} else {
1370-
self.register_federations(&new_gateway_config, &register_federations)
1371-
.await?;
1375+
self.register_federations(
1376+
&new_gateway_config,
1377+
&register_federations,
1378+
&register_task_group,
1379+
)
1380+
.await;
13721381
}
13731382

13741383
dbtx.commit_tx().await;
@@ -1629,7 +1638,8 @@ impl Gateway {
16291638
&self,
16301639
gateway_config: &GatewayConfiguration,
16311640
federations: &[(FederationId, FederationConfig)],
1632-
) -> AdminResult<()> {
1641+
register_task_group: &TaskGroup,
1642+
) {
16331643
if let Ok(lightning_context) = self.get_lightning_context().await {
16341644
let route_hints = lightning_context
16351645
.lnrpc
@@ -1640,31 +1650,34 @@ impl Gateway {
16401650
}
16411651

16421652
for (federation_id, federation_config) in federations {
1643-
if let Some(client) = self.federation_manager.read().await.client(federation_id) {
1644-
if async {
1645-
client
1646-
.value()
1647-
.get_first_module::<GatewayClientModule>()?
1648-
.register_with_federation(
1649-
route_hints.clone(),
1650-
GW_ANNOUNCEMENT_TTL,
1651-
federation_config.fees,
1652-
lightning_context.clone(),
1653-
)
1654-
.await
1655-
}
1656-
.instrument(client.span())
1657-
.await
1658-
.is_err()
1653+
let fed_manager = self.federation_manager.read().await;
1654+
if let Some(client) = fed_manager.client(federation_id) {
1655+
let client_arc = client.clone().into_value();
1656+
let route_hints = route_hints.clone();
1657+
let lightning_context = lightning_context.clone();
1658+
let federation_config = federation_config.clone();
1659+
1660+
if let Err(e) = register_task_group
1661+
.spawn_cancellable("register_federation", async move {
1662+
let gateway_client = client_arc
1663+
.get_first_module::<GatewayClientModule>()
1664+
.expect("No GatewayClientModule exists");
1665+
gateway_client
1666+
.try_register_with_federation(
1667+
route_hints,
1668+
GW_ANNOUNCEMENT_TTL,
1669+
federation_config.fees,
1670+
lightning_context,
1671+
)
1672+
.await;
1673+
})
1674+
.await
16591675
{
1660-
Err(AdminGatewayError::RegistrationError {
1661-
federation_id: *federation_id,
1662-
})?;
1676+
warn!(?e, "Failed to shutdown register federation task");
16631677
}
16641678
}
16651679
}
16661680
}
1667-
Ok(())
16681681
}
16691682

16701683
/// This function will return a `GatewayConfiguration` one of two
@@ -1804,17 +1817,16 @@ impl Gateway {
18041817
let lightning_module_mode = self.lightning_module_mode;
18051818
info!(?lightning_module_mode, "Spawning register task...");
18061819
let gateway = self.clone();
1820+
let register_task_group = task_group.make_subgroup();
18071821
task_group.spawn_cancellable("register clients", async move {
18081822
loop {
1809-
let mut registration_result: Option<AdminResult<()>> = None;
18101823
let gateway_config = gateway.clone_gateway_config().await;
18111824
if let Some(gateway_config) = gateway_config {
18121825
let gateway_state = gateway.get_state().await;
18131826
if let GatewayState::Running { .. } = &gateway_state {
18141827
let mut dbtx = gateway.gateway_db.begin_transaction_nc().await;
18151828
let all_federations_configs: Vec<_> = dbtx.load_federation_configs().await.into_iter().collect();
1816-
let result = gateway.register_federations(&gateway_config, &all_federations_configs).await;
1817-
registration_result = Some(result);
1829+
gateway.register_federations(&gateway_config, &all_federations_configs, &register_task_group).await;
18181830
} else {
18191831
// We need to retry more often if the gateway is not in the Running state
18201832
const NOT_RUNNING_RETRY: Duration = Duration::from_secs(10);
@@ -1826,16 +1838,9 @@ impl Gateway {
18261838
warn!("Cannot register clients because gateway configuration is not set.");
18271839
}
18281840

1829-
let registration_delay: Duration = if let Some(Err(AdminGatewayError::RegistrationError { .. })) = registration_result {
1830-
// Retry to register gateway with federations in 10 seconds since it failed
1831-
Duration::from_secs(10)
1832-
} else {
1833-
// Allow a 15% buffer of the TTL before the re-registering gateway
1834-
// with the federations.
1835-
GW_ANNOUNCEMENT_TTL.mul_f32(0.85)
1836-
};
1837-
1838-
sleep(registration_delay).await;
1841+
// Allow a 15% buffer of the TTL before the re-registering gateway
1842+
// with the federations.
1843+
sleep(GW_ANNOUNCEMENT_TTL.mul_f32(0.85)).await;
18391844
}
18401845
});
18411846
}

gateway/ln-gateway/src/state_machine/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,13 @@ impl GatewayClientModule {
345345
}
346346

347347
/// Register gateway with federation
348-
pub async fn register_with_federation(
348+
pub async fn try_register_with_federation(
349349
&self,
350350
route_hints: Vec<RouteHint>,
351351
time_to_live: Duration,
352352
fees: RoutingFees,
353353
lightning_context: LightningContext,
354-
) -> anyhow::Result<()> {
354+
) {
355355
let registration_info =
356356
self.to_gateway_registration_info(route_hints, time_to_live, fees, lightning_context);
357357
let gateway_id = registration_info.info.gateway_id;
@@ -362,9 +362,14 @@ impl GatewayClientModule {
362362
.await
363363
.global
364364
.calculate_federation_id();
365-
self.module_api.register_gateway(&registration_info).await?;
366-
debug!("Successfully registered gateway {gateway_id} with federation {federation_id}");
367-
Ok(())
365+
if let Err(e) = self.module_api.register_gateway(&registration_info).await {
366+
warn!(
367+
?e,
368+
"Failed to register gateway {gateway_id} with federation {federation_id}"
369+
);
370+
} else {
371+
info!("Successfully registered gateway {gateway_id} with federation {federation_id}");
372+
}
368373
}
369374

370375
/// Attempts to remove a gateway's registration from the federation. Since

0 commit comments

Comments
 (0)