Skip to content

Commit 4a12d76

Browse files
committed
Cache socket address in HttpClient for reconnect
If the HttpClient attempts to reconnect to bitcoind that is no longer running, the client fails to get the address from the stream. Cache the address when connecting to prevent this.
1 parent 8e7b590 commit 4a12d76

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lightning-block-sync/src/http.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::convert::TryFrom;
88
use std::fmt;
99
#[cfg(not(feature = "tokio"))]
1010
use std::io::Write;
11-
use std::net::ToSocketAddrs;
11+
use std::net::{SocketAddr, ToSocketAddrs};
1212
use std::time::Duration;
1313

1414
#[cfg(feature = "tokio")]
@@ -97,6 +97,7 @@ impl<'a> std::net::ToSocketAddrs for &'a HttpEndpoint {
9797

9898
/// Client for making HTTP requests.
9999
pub(crate) struct HttpClient {
100+
address: SocketAddr,
100101
stream: TcpStream,
101102
}
102103

@@ -119,7 +120,7 @@ impl HttpClient {
119120
TcpStream::from_std(stream)?
120121
};
121122

122-
Ok(Self { stream })
123+
Ok(Self { address, stream })
123124
}
124125

125126
/// Sends a `GET` request for a resource identified by `uri` at the `host`.
@@ -162,7 +163,6 @@ impl HttpClient {
162163
/// Sends an HTTP request message and reads the response, returning its body. Attempts to
163164
/// reconnect and retry if the connection has been closed.
164165
async fn send_request_with_retry(&mut self, request: &str) -> std::io::Result<Vec<u8>> {
165-
let endpoint = self.stream.peer_addr().unwrap();
166166
match self.send_request(request).await {
167167
Ok(bytes) => Ok(bytes),
168168
Err(_) => {
@@ -176,7 +176,7 @@ impl HttpClient {
176176
tokio::time::sleep(Duration::from_millis(100)).await;
177177
#[cfg(not(feature = "tokio"))]
178178
std::thread::sleep(Duration::from_millis(100));
179-
*self = Self::connect(endpoint)?;
179+
*self = Self::connect(self.address)?;
180180
self.send_request(request).await
181181
},
182182
}

0 commit comments

Comments
 (0)