@@ -94,7 +94,7 @@ use rpc::{
94
94
} ;
95
95
use state_machine:: { GatewayClientModule , GatewayExtPayStates } ;
96
96
use tokio:: sync:: RwLock ;
97
- use tracing:: { debug, error, info, info_span, warn, Instrument } ;
97
+ use tracing:: { debug, error, info, info_span, warn} ;
98
98
99
99
use crate :: config:: LightningModuleMode ;
100
100
use crate :: db:: { get_gatewayd_database_migrations, FederationConfig } ;
@@ -1163,14 +1163,14 @@ impl Gateway {
1163
1163
Self :: check_lnv1_federation_network ( & client, gateway_config. network ) . await ?;
1164
1164
client
1165
1165
. get_first_module :: < GatewayClientModule > ( ) ?
1166
- . register_with_federation (
1166
+ . try_register_with_federation (
1167
1167
// Route hints will be updated in the background
1168
1168
Vec :: new ( ) ,
1169
1169
GW_ANNOUNCEMENT_TTL ,
1170
1170
federation_config. fees ,
1171
1171
lightning_context,
1172
1172
)
1173
- . await ? ;
1173
+ . await ;
1174
1174
}
1175
1175
1176
1176
if self . is_running_lnv2 ( ) {
@@ -1361,14 +1361,23 @@ impl Gateway {
1361
1361
1362
1362
// If 'num_route_hints' is provided, all federations must be re-registered.
1363
1363
// Otherwise, only those affected by the new fees need to be re-registered.
1364
+ let register_task_group = TaskGroup :: new ( ) ;
1364
1365
if num_route_hints. is_some ( ) {
1365
1366
let all_federations_configs: Vec < _ > =
1366
1367
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 ;
1369
1374
} 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 ;
1372
1381
}
1373
1382
1374
1383
dbtx. commit_tx ( ) . await ;
@@ -1629,7 +1638,8 @@ impl Gateway {
1629
1638
& self ,
1630
1639
gateway_config : & GatewayConfiguration ,
1631
1640
federations : & [ ( FederationId , FederationConfig ) ] ,
1632
- ) -> AdminResult < ( ) > {
1641
+ register_task_group : & TaskGroup ,
1642
+ ) {
1633
1643
if let Ok ( lightning_context) = self . get_lightning_context ( ) . await {
1634
1644
let route_hints = lightning_context
1635
1645
. lnrpc
@@ -1640,31 +1650,34 @@ impl Gateway {
1640
1650
}
1641
1651
1642
1652
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
1659
1675
{
1660
- Err ( AdminGatewayError :: RegistrationError {
1661
- federation_id : * federation_id,
1662
- } ) ?;
1676
+ warn ! ( ?e, "Failed to shutdown register federation task" ) ;
1663
1677
}
1664
1678
}
1665
1679
}
1666
1680
}
1667
- Ok ( ( ) )
1668
1681
}
1669
1682
1670
1683
/// This function will return a `GatewayConfiguration` one of two
@@ -1804,17 +1817,16 @@ impl Gateway {
1804
1817
let lightning_module_mode = self . lightning_module_mode ;
1805
1818
info ! ( ?lightning_module_mode, "Spawning register task..." ) ;
1806
1819
let gateway = self . clone ( ) ;
1820
+ let register_task_group = task_group. make_subgroup ( ) ;
1807
1821
task_group. spawn_cancellable ( "register clients" , async move {
1808
1822
loop {
1809
- let mut registration_result: Option < AdminResult < ( ) > > = None ;
1810
1823
let gateway_config = gateway. clone_gateway_config ( ) . await ;
1811
1824
if let Some ( gateway_config) = gateway_config {
1812
1825
let gateway_state = gateway. get_state ( ) . await ;
1813
1826
if let GatewayState :: Running { .. } = & gateway_state {
1814
1827
let mut dbtx = gateway. gateway_db . begin_transaction_nc ( ) . await ;
1815
1828
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 ;
1818
1830
} else {
1819
1831
// We need to retry more often if the gateway is not in the Running state
1820
1832
const NOT_RUNNING_RETRY : Duration = Duration :: from_secs ( 10 ) ;
@@ -1826,16 +1838,9 @@ impl Gateway {
1826
1838
warn ! ( "Cannot register clients because gateway configuration is not set." ) ;
1827
1839
}
1828
1840
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 ;
1839
1844
}
1840
1845
} ) ;
1841
1846
}
0 commit comments