Skip to content

Commit 30f3bd9

Browse files
committed
f always retry requests
1 parent 8b3759e commit 30f3bd9

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +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::WouldBlock |
164-
std::io::ErrorKind::ConnectionAborted |
165-
std::io::ErrorKind::UnexpectedEof => {
166-
// Reconnect if the connection was closed. This may happen if the server's
167-
// keep-alive limits are reached.
168-
*self = Self::connect(endpoint)?;
169-
self.send_request(request).await
170-
},
171-
_ => 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
172174
},
173175
}
174176
}

0 commit comments

Comments
 (0)