X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=inline;f=lightning-block-sync%2Fsrc%2Fhttp.rs;h=1f5f046c0f87dd904820e7f97718b1a3ba16ac15;hb=b465318e12e4374d7e7cb3c00036fded340f155a;hp=0788bc989f5e3bfbdaeee747f50c7e0200e22560;hpb=6ca3b8ed496290cd9b6c49525d57a887a735a914;p=rust-lightning diff --git a/lightning-block-sync/src/http.rs b/lightning-block-sync/src/http.rs index 0788bc98..1f5f046c 100644 --- a/lightning-block-sync/src/http.rs +++ b/lightning-block-sync/src/http.rs @@ -1,3 +1,6 @@ +//! Simple HTTP implementation which supports both async and traditional execution environments +//! with minimal dependencies. This is used as the basis for REST and RPC clients. + use chunked_transfer; use serde_json; @@ -155,16 +158,19 @@ impl HttpClient { let endpoint = self.stream.peer_addr().unwrap(); match self.send_request(request).await { Ok(bytes) => Ok(bytes), - Err(e) => match e.kind() { - std::io::ErrorKind::ConnectionReset | - std::io::ErrorKind::ConnectionAborted | - std::io::ErrorKind::UnexpectedEof => { - // Reconnect if the connection was closed. This may happen if the server's - // keep-alive limits are reached. - *self = Self::connect(endpoint)?; - self.send_request(request).await - }, - _ => Err(e), + Err(_) => { + // Reconnect and retry on fail. This can happen if the connection was closed after + // the keep-alive limits are reached, or generally if the request timed out due to + // Bitcoin Core being stuck on a long-running operation or its RPC queue being + // full. + // Block 100ms before retrying the request as in many cases the source of the error + // may be persistent for some time. + #[cfg(feature = "tokio")] + tokio::time::sleep(Duration::from_millis(100)).await; + #[cfg(not(feature = "tokio"))] + std::thread::sleep(Duration::from_millis(100)); + *self = Self::connect(endpoint)?; + self.send_request(request).await }, } } @@ -399,10 +405,10 @@ enum HttpMessageLength { } /// An HTTP response body in binary format. -pub(crate) struct BinaryResponse(pub(crate) Vec); +pub struct BinaryResponse(pub Vec); /// An HTTP response body in JSON format. -pub(crate) struct JsonResponse(pub(crate) serde_json::Value); +pub struct JsonResponse(pub serde_json::Value); /// Interprets bytes from an HTTP response body as binary data. impl TryFrom> for BinaryResponse {