Skip to content

Commit c442e8e

Browse files
committed
Refactor do_connect_peer to always propagate result to subscribers
Extract the connection logic into `do_connect_peer_internal` and have `do_connect_peer` act as a thin wrapper that always calls `propagate_result_to_subscribers` with the result. This removes the need to manually propagate at every error site, making the code less error-prone. Co-Authored-By: HAL 9000
1 parent 3aef2b3 commit c442e8e

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

src/connection.rs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ where
5656

5757
pub(crate) async fn do_connect_peer(
5858
&self, node_id: PublicKey, addr: SocketAddress,
59+
) -> Result<(), Error> {
60+
let res = self.do_connect_peer_internal(node_id, addr).await;
61+
self.propagate_result_to_subscribers(&node_id, res);
62+
res
63+
}
64+
65+
async fn do_connect_peer_internal(
66+
&self, node_id: PublicKey, addr: SocketAddress,
5967
) -> Result<(), Error> {
6068
// First, we check if there is already an outbound connection in flight, if so, we just
6169
// await on the corresponding watch channel. The task driving the connection future will
@@ -71,15 +79,14 @@ where
7179

7280
log_info!(self.logger, "Connecting to peer: {}@{}", node_id, addr);
7381

74-
let res = match addr {
82+
match addr {
7583
SocketAddress::OnionV2(old_onion_addr) => {
7684
log_error!(
77-
self.logger,
78-
"Failed to resolve network address {:?}: Resolution of OnionV2 addresses is currently unsupported.",
79-
old_onion_addr
80-
);
81-
self.propagate_result_to_subscribers(&node_id, Err(Error::InvalidSocketAddress));
82-
return Err(Error::InvalidSocketAddress);
85+
self.logger,
86+
"Failed to resolve network address {:?}: Resolution of OnionV2 addresses is currently unsupported.",
87+
old_onion_addr
88+
);
89+
Err(Error::InvalidSocketAddress)
8390
},
8491
SocketAddress::OnionV3 { .. } => {
8592
let proxy_config = self.tor_proxy_config.as_ref().ok_or_else(|| {
@@ -88,10 +95,6 @@ where
8895
"Failed to resolve network address {:?}: Tor usage is not configured.",
8996
addr
9097
);
91-
self.propagate_result_to_subscribers(
92-
&node_id,
93-
Err(Error::InvalidSocketAddress),
94-
);
9598
Error::InvalidSocketAddress
9699
})?;
97100
let proxy_addr = proxy_config
@@ -104,10 +107,6 @@ where
104107
proxy_config.proxy_address,
105108
e
106109
);
107-
self.propagate_result_to_subscribers(
108-
&node_id,
109-
Err(Error::InvalidSocketAddress),
110-
);
111110
Error::InvalidSocketAddress
112111
})?
113112
.next()
@@ -117,10 +116,6 @@ where
117116
"Failed to resolve Tor proxy network address {}",
118117
proxy_config.proxy_address
119118
);
120-
self.propagate_result_to_subscribers(
121-
&node_id,
122-
Err(Error::InvalidSocketAddress),
123-
);
124119
Error::InvalidSocketAddress
125120
})?;
126121
let connection_future = lightning_net_tokio::tor_connect_outbound(
@@ -142,19 +137,11 @@ where
142137
addr,
143138
e
144139
);
145-
self.propagate_result_to_subscribers(
146-
&node_id,
147-
Err(Error::InvalidSocketAddress),
148-
);
149140
Error::InvalidSocketAddress
150141
})?
151142
.next()
152143
.ok_or_else(|| {
153144
log_error!(self.logger, "Failed to resolve network address {}", addr);
154-
self.propagate_result_to_subscribers(
155-
&node_id,
156-
Err(Error::InvalidSocketAddress),
157-
);
158145
Error::InvalidSocketAddress
159146
})?;
160147
let connection_future = lightning_net_tokio::connect_outbound(
@@ -164,11 +151,7 @@ where
164151
);
165152
self.await_connection(connection_future, node_id, addr).await
166153
},
167-
};
168-
169-
self.propagate_result_to_subscribers(&node_id, res);
170-
171-
res
154+
}
172155
}
173156

174157
async fn await_connection<F, CF>(

0 commit comments

Comments
 (0)