Skip to content

Commit

Permalink
feat: share REQUEST_CLIENT across crate
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Nov 24, 2023
1 parent e791881 commit dcfbba1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use log::LevelFilter;
mod app;
mod config;
mod prelude;
mod request_client;
mod runner;
mod uploader;

Expand Down
19 changes: 19 additions & 0 deletions src/request_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use lazy_static::lazy_static;
use reqwest::ClientBuilder;
use reqwest_middleware::{ClientBuilder as ClientWithMiddlewareBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};

const UPLOAD_RETRY_COUNT: u32 = 3;

lazy_static! {
pub static ref REQUEST_CLIENT: ClientWithMiddleware = ClientWithMiddlewareBuilder::new(
ClientBuilder::new()
.user_agent("codspeed-runner")
.build()
.unwrap()
)
.with(RetryTransientMiddleware::new_with_policy(
ExponentialBackoff::builder().build_with_max_retries(UPLOAD_RETRY_COUNT)
))
.build();
}
6 changes: 4 additions & 2 deletions src/runner/helpers/download_file.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::prelude::*;
use crate::{prelude::*, request_client::REQUEST_CLIENT};
use std::path::Path;

use url::Url;

pub async fn download_file(url: &Url, path: &Path) -> Result<()> {
let response = reqwest::get(url.clone())
let response = REQUEST_CLIENT
.get(url.clone())
.send()
.await
.map_err(|e| anyhow!("Failed to download file: {}", e))?;
if !response.status().is_success() {
Expand Down
28 changes: 4 additions & 24 deletions src/uploader/upload.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use crate::{config::Config, prelude::*, request_client::REQUEST_CLIENT, runner::RunData};
use async_compression::tokio::write::GzipEncoder;
use base64::{engine::general_purpose, Engine as _};
use lazy_static::lazy_static;
use serde_json::json;
use tokio::io::AsyncWriteExt;
use tokio_tar::Builder;

use crate::{config::Config, prelude::*, runner::RunData};
use reqwest::ClientBuilder;
use reqwest_middleware::{ClientBuilder as ClientWithMiddlewareBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use serde_json::json;

use super::{
ci_provider::CIProvider,
github_actions_provider::GitHubActionsProvider,
Expand Down Expand Up @@ -41,26 +36,11 @@ async fn get_profile_archive_buffer(run_data: &RunData) -> Result<(Vec<u8>, Stri
Ok((archive_buffer, archive_hash))
}

const UPLOAD_RETRY_COUNT: u32 = 3;

lazy_static! {
static ref UPLOAD_REQUEST_CLIENT: ClientWithMiddleware = ClientWithMiddlewareBuilder::new(
ClientBuilder::new()
.user_agent("codspeed-runner")
.build()
.unwrap()
)
.with(RetryTransientMiddleware::new_with_policy(
ExponentialBackoff::builder().build_with_max_retries(UPLOAD_RETRY_COUNT)
))
.build();
}

async fn retrieve_upload_data(
config: &Config,
upload_metadata: &UploadMetadata,
) -> Result<UploadData> {
let mut upload_request = UPLOAD_REQUEST_CLIENT
let mut upload_request = REQUEST_CLIENT
.post(config.upload_url.clone())
.json(&upload_metadata);
if !upload_metadata.tokenless {
Expand All @@ -77,7 +57,7 @@ async fn upload_archive_buffer(
archive_buffer: Vec<u8>,
archive_hash: &String,
) -> Result<()> {
UPLOAD_REQUEST_CLIENT
REQUEST_CLIENT
.put(upload_data.upload_url.clone())
.header("Content-Type", "application/gzip")
.header("Content-Length", archive_buffer.len())
Expand Down

0 comments on commit dcfbba1

Please sign in to comment.