Skip to content

Commit b465318

Browse files
committed
Allow retrying HTTP requests if we hit a socket timeout
1 parent 7297e13 commit b465318

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

lightning-block-sync/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rpc-client = [ "serde", "serde_json", "chunked_transfer" ]
1616
[dependencies]
1717
bitcoin = "0.26"
1818
lightning = { version = "0.0.14", path = "../lightning" }
19-
tokio = { version = "1.0", features = [ "io-util", "net" ], optional = true }
19+
tokio = { version = "1.0", features = [ "io-util", "net", "time" ], optional = true }
2020
serde = { version = "1.0", features = ["derive"], optional = true }
2121
serde_json = { version = "1.0", optional = true }
2222
chunked_transfer = { version = "1.4", optional = true }

lightning-block-sync/src/http.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,19 @@ impl HttpClient {
158158
let endpoint = self.stream.peer_addr().unwrap();
159159
match self.send_request(request).await {
160160
Ok(bytes) => Ok(bytes),
161-
Err(e) => match e.kind() {
162-
std::io::ErrorKind::ConnectionReset |
163-
std::io::ErrorKind::ConnectionAborted |
164-
std::io::ErrorKind::UnexpectedEof => {
165-
// Reconnect if the connection was closed. This may happen if the server's
166-
// keep-alive limits are reached.
167-
*self = Self::connect(endpoint)?;
168-
self.send_request(request).await
169-
},
170-
_ => Err(e),
161+
Err(_) => {
162+
// Reconnect and retry on fail. This can happen if the connection was closed after
163+
// the keep-alive limits are reached, or generally if the request timed out due to
164+
// Bitcoin Core being stuck on a long-running operation or its RPC queue being
165+
// full.
166+
// Block 100ms before retrying the request as in many cases the source of the error
167+
// may be persistent for some time.
168+
#[cfg(feature = "tokio")]
169+
tokio::time::sleep(Duration::from_millis(100)).await;
170+
#[cfg(not(feature = "tokio"))]
171+
std::thread::sleep(Duration::from_millis(100));
172+
*self = Self::connect(endpoint)?;
173+
self.send_request(request).await
171174
},
172175
}
173176
}

0 commit comments

Comments
 (0)