2019-11-06 13:15:00 -08:00
|
|
|
use crate::{
|
2020-03-12 23:20:49 -07:00
|
|
|
client_error::Result,
|
2019-11-06 13:15:00 -08:00
|
|
|
generic_rpc_client_request::GenericRpcClientRequest,
|
|
|
|
rpc_request::{RpcError, RpcRequest},
|
|
|
|
};
|
2019-03-16 21:51:41 -07:00
|
|
|
use log::*;
|
2019-09-24 13:10:59 -07:00
|
|
|
use reqwest::{self, header::CONTENT_TYPE};
|
2019-12-18 21:26:11 -08:00
|
|
|
use solana_sdk::clock::{DEFAULT_TICKS_PER_SECOND, DEFAULT_TICKS_PER_SLOT};
|
2019-11-06 13:15:00 -08:00
|
|
|
use std::{thread::sleep, time::Duration};
|
2019-03-16 21:51:41 -07:00
|
|
|
|
|
|
|
pub struct RpcClientRequest {
|
2020-01-08 12:31:43 -08:00
|
|
|
client: reqwest::blocking::Client,
|
2019-03-16 21:51:41 -07:00
|
|
|
url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RpcClientRequest {
|
|
|
|
pub fn new(url: String) -> Self {
|
2020-01-31 08:53:17 -08:00
|
|
|
Self::new_with_timeout(url, Duration::from_secs(20))
|
2019-03-16 21:51:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_timeout(url: String, timeout: Duration) -> Self {
|
2020-01-08 12:31:43 -08:00
|
|
|
let client = reqwest::blocking::Client::builder()
|
2019-09-24 13:10:59 -07:00
|
|
|
.timeout(timeout)
|
|
|
|
.build()
|
|
|
|
.expect("build rpc client");
|
|
|
|
|
|
|
|
Self { client, url }
|
2019-03-16 21:51:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericRpcClientRequest for RpcClientRequest {
|
|
|
|
fn send(
|
|
|
|
&self,
|
|
|
|
request: &RpcRequest,
|
2019-12-18 21:26:11 -08:00
|
|
|
params: serde_json::Value,
|
2019-03-16 21:51:41 -07:00
|
|
|
mut retries: usize,
|
2020-03-12 23:20:49 -07:00
|
|
|
) -> Result<serde_json::Value> {
|
2019-03-16 21:51:41 -07:00
|
|
|
// Concurrent requests are not supported so reuse the same request id for all requests
|
|
|
|
let request_id = 1;
|
|
|
|
|
2019-12-18 21:26:11 -08:00
|
|
|
let request_json = request.build_request_json(request_id, params);
|
2019-03-16 21:51:41 -07:00
|
|
|
|
|
|
|
loop {
|
2019-09-24 13:10:59 -07:00
|
|
|
match self
|
2019-03-16 21:51:41 -07:00
|
|
|
.client
|
|
|
|
.post(&self.url)
|
2019-09-24 13:10:59 -07:00
|
|
|
.header(CONTENT_TYPE, "application/json")
|
|
|
|
.body(request_json.to_string())
|
|
|
|
.send()
|
|
|
|
{
|
2020-01-08 12:31:43 -08:00
|
|
|
Ok(response) => {
|
2019-12-02 09:01:25 -08:00
|
|
|
if !response.status().is_success() {
|
|
|
|
return Err(response.error_for_status().unwrap_err().into());
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:10:59 -07:00
|
|
|
let json: serde_json::Value = serde_json::from_str(&response.text()?)?;
|
|
|
|
if json["error"].is_object() {
|
2019-10-02 18:04:18 -07:00
|
|
|
return Err(RpcError::RpcRequestError(format!(
|
2019-09-24 13:10:59 -07:00
|
|
|
"RPC Error response: {}",
|
|
|
|
serde_json::to_string(&json["error"]).unwrap()
|
2019-10-02 18:04:18 -07:00
|
|
|
))
|
|
|
|
.into());
|
2019-09-24 13:10:59 -07:00
|
|
|
}
|
|
|
|
return Ok(json["result"].clone());
|
2019-03-16 21:51:41 -07:00
|
|
|
}
|
2019-09-24 13:10:59 -07:00
|
|
|
Err(e) => {
|
2019-11-13 08:43:15 -08:00
|
|
|
info!("{:?} failed, {} retries left: {:?}", request, retries, e);
|
2019-09-24 13:10:59 -07:00
|
|
|
if retries == 0 {
|
2019-10-02 18:04:18 -07:00
|
|
|
return Err(e.into());
|
2019-09-24 13:10:59 -07:00
|
|
|
}
|
|
|
|
retries -= 1;
|
2019-09-04 12:11:44 -07:00
|
|
|
|
2019-09-24 13:10:59 -07:00
|
|
|
// Sleep for approximately half a slot
|
|
|
|
sleep(Duration::from_millis(
|
|
|
|
500 * DEFAULT_TICKS_PER_SLOT / DEFAULT_TICKS_PER_SECOND,
|
|
|
|
));
|
|
|
|
}
|
2019-03-16 21:51:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|