Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resources): add exponential backoff to call_transpose #225

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 63 additions & 53 deletions common/src/resources/transpose.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use backoff::ExponentialBackoff;
use indicatif::ProgressBar;
use reqwest::header::HeaderMap;
use serde_json::Value;
Expand All @@ -21,58 +22,67 @@ struct TransposeResponse {
}

/// executes a transpose SQL query and returns the response
async fn _call_transpose(query: &str, api_key: &str) -> Option<TransposeResponse> {
// get a new logger
let logger = Logger::default();

// build the headers
let mut headers = HeaderMap::new();
headers.insert("Content-Type", "application/json".parse().unwrap());
headers.insert("X-API-KEY", api_key.parse().unwrap());

// clone the query
let query = query.to_owned();

// make the request
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.timeout(Duration::from_secs(999999999))
.build()
.unwrap();

let response = match client
.post("https://api.transpose.io/sql")
.body(query.clone())
.headers(headers)
.send()
.await
{
Ok(res) => res,
Err(e) => {
logger.error("failed to call Transpose .");
logger.error(&format!("error: {e}"));
std::process::exit(1)
}
};

// parse body
match response.text().await {
Ok(body) => Some(match serde_json::from_str(&body) {
Ok(json) => json,
Err(e) => {
logger.error("Transpose request unsucessful.");
logger.debug(&format!("curl: curl -X GET \"https://api.transpose.io/sql\" -H \"accept: application/json\" -H \"Content-Type: application/json\" -H \"X-API-KEY: {api_key}\" -d {query}"));
logger.error(&format!("error: {e}"));
logger.debug(&format!("response body: {body:?}"));
std::process::exit(1)
async fn call_transpose(query: &str, api_key: &str) -> Option<TransposeResponse> {
backoff::future::retry(
ExponentialBackoff {
max_elapsed_time: Some(Duration::from_secs(10)),
..ExponentialBackoff::default()
},
|| async {
// get a new logger
let logger = Logger::default();

// build the headers
let mut headers = HeaderMap::new();
headers.insert("Content-Type", "application/json".parse().unwrap());
headers.insert("X-API-KEY", api_key.parse().unwrap());

// clone the query
let query = query.to_owned();

// make the request
let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.timeout(Duration::from_secs(999999999))
.build()
.unwrap();

let response = match client
.post("https://api.transpose.io/sql")
.body(query.clone())
.headers(headers)
.send()
.await
{
Ok(res) => res,
Err(e) => {
logger.error("failed to call Transpose .");
logger.error(&format!("error: {e}"));
return Err(backoff::Error::Permanent(()))
}
};

// parse body
match response.text().await {
Ok(body) => Ok(match serde_json::from_str(&body) {
Ok(json) => json,
Err(e) => {
logger.error("Transpose request unsucessful.");
logger.debug(&format!("curl: curl -X GET \"https://api.transpose.io/sql\" -H \"accept: application/json\" -H \"Content-Type: application/json\" -H \"X-API-KEY: {api_key}\" -d {query}"));
logger.error(&format!("error: {e}"));
logger.debug(&format!("response body: {body:?}"));
return Err(backoff::Error::Permanent(()))
}
}),
Err(e) => {
logger.error("failed to parse Transpose response body.");
logger.error(&format!("error: {e}"));
Err(backoff::Error::Permanent(()))
}
}
}),
Err(e) => {
logger.error("failed to parse Transpose response body.");
logger.error(&format!("error: {e}"));
std::process::exit(1)
}
}
},
).await
.ok()
}

/// Get all interactions with the given address. Includes transactions to, from, as well as internal
Expand Down Expand Up @@ -115,7 +125,7 @@ pub async fn get_transaction_list(
bounds.1
);

let response = match _call_transpose(&query, api_key).await {
let response = match call_transpose(&query, api_key).await {
Some(response) => response,
None => {
logger.error("failed to get transaction list from Transpose");
Expand Down Expand Up @@ -197,7 +207,7 @@ pub async fn get_contract_creation(
"{{\"sql\":\"SELECT block_number, transaction_hash FROM {chain}.transactions WHERE TIMESTAMP = ( SELECT created_timestamp FROM {chain}.accounts WHERE address = '{address}' ) AND contract_address = '{address}'\",\"parameters\":{{}},\"options\":{{\"timeout\": 999999999}}}}",
);

let response = match _call_transpose(&query, api_key).await {
let response = match call_transpose(&query, api_key).await {
Some(response) => response,
None => {
logger.error("failed to get creation tx from Transpose");
Expand Down
Loading