Skip to content

Commit 58a4f6c

Browse files
authored
Merge pull request #931 from jkczyz/2021-05-http-client-reconnect
Cache socket address in HttpClient for reconnect
2 parents 8cc9410 + 4a12d76 commit 58a4f6c

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)