Skip to content
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
35 changes: 19 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ rust-version = "1.86"
[dependencies]
anylog = "0.6.3"
anyhow = { version = "1.0.69", features = ["backtrace"] }
backoff = "0.4.0"
bytecount = "0.6.3"
chrono = { version = "0.4.31", features = ["serde"] }
clap = { version = "4.1.6", default-features = false, features = [
Expand Down Expand Up @@ -79,6 +78,7 @@ magic_string = "0.3.4"
chrono-tz = "0.8.4"
secrecy = "0.8.0"
lru = "0.16.0"
backon = { version = "1.5.2", features = ["std", "std-blocking-sleep"] }
brotli = "8.0.2"

[dev-dependencies]
Expand Down
18 changes: 18 additions & 0 deletions src/api/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,27 @@ mod sentry_error;
pub(super) use api_error::{ApiError, ApiErrorKind};
pub(super) use sentry_error::SentryError;

use crate::api::ApiResponse;

#[derive(Clone, Debug, thiserror::Error)]
#[error("project was renamed to '{0}'\nPlease use this slug in your .sentryclirc file, sentry.properties file or in the CLI --project parameter")]
pub(super) struct ProjectRenamedError(pub(super) String);

/// Shortcut alias for results of this module.
pub(super) type ApiResult<T> = Result<T, ApiError>;

#[derive(Debug, thiserror::Error)]
#[error("request failed with retryable status code {}", .body.status)]
pub(super) struct RetryError {
body: ApiResponse,
}

impl RetryError {
pub fn new(body: ApiResponse) -> Self {
Self { body }
}

pub fn into_body(self) -> ApiResponse {
self.body
}
}
54 changes: 32 additions & 22 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::fmt;
use std::fs::File;
use std::io::{self, Read as _, Write};
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use std::{fmt, thread};

use anyhow::{Context as _, Result};
use backoff::backoff::Backoff as _;
use backon::BlockingRetryable as _;
use brotli::enc::BrotliEncoderParams;
use brotli::CompressorWriter;
#[cfg(target_os = "macos")]
Expand All @@ -45,7 +45,7 @@ use symbolic::common::DebugId;
use symbolic::debuginfo::ObjectKind;
use uuid::Uuid;

use crate::api::errors::ProjectRenamedError;
use crate::api::errors::{ProjectRenamedError, RetryError};
use crate::config::{Auth, Config};
use crate::constants::{ARCH, DEFAULT_URL, EXT, PLATFORM, RELEASE_REGISTRY_LATEST_URL, VERSION};
use crate::utils::file_upload::LegacyUploadContext;
Expand Down Expand Up @@ -1858,32 +1858,42 @@ impl ApiRequest {
pub fn send(mut self) -> ApiResult<ApiResponse> {
let max_retries = Config::current().max_retries();

let mut backoff = get_default_backoff();
let mut retry_number = 0;
let backoff = get_default_backoff().with_max_times(max_retries as usize);
let retry_number = RefCell::new(0);

loop {
let send_req = || {
let mut out = vec![];
debug!("retry number {retry_number}, max retries: {max_retries}",);
let mut retry_number = retry_number.borrow_mut();

debug!("retry number {retry_number}, max retries: {max_retries}");
*retry_number += 1;

let mut rv = self.send_into(&mut out)?;
if retry_number >= max_retries || !RETRY_STATUS_CODES.contains(&rv.status) {
rv.body = Some(out);
return Ok(rv);
}
rv.body = Some(out);

// Exponential backoff
let backoff_timeout = backoff
.next_backoff()
.expect("should not return None, as there is no max_elapsed_time");
if RETRY_STATUS_CODES.contains(&rv.status) {
anyhow::bail!(RetryError::new(rv));
}

debug!(
"retry number {retry_number}, retrying again in {} ms",
backoff_timeout.as_milliseconds()
);
std::thread::sleep(backoff_timeout);
Ok(rv)
};

retry_number += 1;
}
send_req
.retry(backoff)
.sleep(thread::sleep)
.when(|e| e.is::<RetryError>())
.notify(|e, dur| {
debug!(
"retry number {} failed due to {e:#}, retrying again in {} ms",
*retry_number.borrow() - 1,
dur.as_milliseconds()
);
})
.call()
.or_else(|err| match err.downcast::<RetryError>() {
Ok(err) => Ok(err.into_body()),
Err(err) => Err(ApiError::with_source(ApiErrorKind::RequestFailed, err)),
})
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ pub const EXT: &str = ".exe";
pub const EXT: &str = "";

/// Backoff multiplier (1.5 which is 50% increase per backoff).
pub const DEFAULT_MULTIPLIER: f64 = 1.5;
/// Backoff randomization factor (0 means no randomization).
pub const DEFAULT_RANDOMIZATION: f64 = 0.1;
pub const DEFAULT_MULTIPLIER: f32 = 1.5;
/// Initial backoff interval in milliseconds.
pub const DEFAULT_INITIAL_INTERVAL: u64 = 1000;
/// Maximum backoff interval in milliseconds.
Expand Down
30 changes: 10 additions & 20 deletions src/utils/retry.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
use std::time::{Duration, Instant};
use std::time::Duration;

use backoff::backoff::Backoff as _;
use backoff::ExponentialBackoff;
use backon::ExponentialBuilder;

use crate::constants::{
DEFAULT_INITIAL_INTERVAL, DEFAULT_MAX_INTERVAL, DEFAULT_MULTIPLIER, DEFAULT_RANDOMIZATION,
};
use crate::constants::{DEFAULT_INITIAL_INTERVAL, DEFAULT_MAX_INTERVAL, DEFAULT_MULTIPLIER};

/// Returns an ExponentialBackoff object instantianted with default values
pub fn get_default_backoff() -> ExponentialBackoff {
let mut eb = ExponentialBackoff {
current_interval: Duration::from_millis(DEFAULT_INITIAL_INTERVAL),
initial_interval: Duration::from_millis(DEFAULT_INITIAL_INTERVAL),
randomization_factor: DEFAULT_RANDOMIZATION,
multiplier: DEFAULT_MULTIPLIER,
max_interval: Duration::from_millis(DEFAULT_MAX_INTERVAL),
max_elapsed_time: None,
clock: Default::default(),
start_time: Instant::now(),
};
eb.reset();
eb
/// Returns an exponential backoff builder instantianted with default values
pub fn get_default_backoff() -> ExponentialBuilder {
ExponentialBuilder::new()
.with_min_delay(Duration::from_millis(DEFAULT_INITIAL_INTERVAL))
.with_max_delay(Duration::from_millis(DEFAULT_MAX_INTERVAL))
.with_jitter()
.with_factor(DEFAULT_MULTIPLIER)
}

/// Trait for displaying duration-like in milliseconds
Expand Down