Skip to content

Commit dbdeba7

Browse files
ref: Only obtain max retry count once (#2521)
With the current implementation, we attempt to retrieve the max retry count from the environment or from the ini file on every access, which could lead to the user being warned multiple times if an invalid value is supplied. Instead, we should only obtain this count once and store it in the `Config`
1 parent 7ee7cad commit dbdeba7

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

src/api/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ impl Api {
396396
.request(Method::Post, url, None)?
397397
.with_form_data(form)?
398398
.with_retry(
399-
self.config.get_max_retry_count(),
399+
self.config.max_retries(),
400400
&[
401401
http::HTTP_STATUS_502_BAD_GATEWAY,
402402
http::HTTP_STATUS_503_SERVICE_UNAVAILABLE,
@@ -968,7 +968,7 @@ impl<'a> AuthenticatedApi<'a> {
968968
self.request(Method::Post, &url)?
969969
.with_json_body(request)?
970970
.with_retry(
971-
self.api.config.get_max_retry_count(),
971+
self.api.config.max_retries(),
972972
&[
973973
http::HTTP_STATUS_502_BAD_GATEWAY,
974974
http::HTTP_STATUS_503_SERVICE_UNAVAILABLE,
@@ -1001,7 +1001,7 @@ impl<'a> AuthenticatedApi<'a> {
10011001
dist: None,
10021002
})?
10031003
.with_retry(
1004-
self.api.config.get_max_retry_count(),
1004+
self.api.config.max_retries(),
10051005
&[
10061006
http::HTTP_STATUS_502_BAD_GATEWAY,
10071007
http::HTTP_STATUS_503_SERVICE_UNAVAILABLE,
@@ -1032,7 +1032,7 @@ impl<'a> AuthenticatedApi<'a> {
10321032
dist,
10331033
})?
10341034
.with_retry(
1035-
self.api.config.get_max_retry_count(),
1035+
self.api.config.max_retries(),
10361036
&[
10371037
http::HTTP_STATUS_502_BAD_GATEWAY,
10381038
http::HTTP_STATUS_503_SERVICE_UNAVAILABLE,
@@ -1408,7 +1408,7 @@ impl RegionSpecificApi<'_> {
14081408
self.request(Method::Post, &path)?
14091409
.with_form_data(form)?
14101410
.with_retry(
1411-
self.api.api.config.get_max_retry_count(),
1411+
self.api.api.config.max_retries(),
14121412
&[http::HTTP_STATUS_507_INSUFFICIENT_STORAGE],
14131413
)
14141414
.progress_bar_mode(ProgressBarMode::Request)
@@ -1467,7 +1467,7 @@ impl RegionSpecificApi<'_> {
14671467
.request(Method::Post, &path)?
14681468
.with_form_data(form)?
14691469
.with_retry(
1470-
self.api.api.config.get_max_retry_count(),
1470+
self.api.api.config.max_retries(),
14711471
&[
14721472
http::HTTP_STATUS_502_BAD_GATEWAY,
14731473
http::HTTP_STATUS_503_SERVICE_UNAVAILABLE,

src/config.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub struct Config {
5353
cached_log_level: log::LevelFilter,
5454
cached_vcs_remote: String,
5555
cached_token_data: Option<AuthTokenPayload>,
56+
max_retries: u32,
5657
}
5758

5859
impl Config {
@@ -91,6 +92,7 @@ impl Config {
9192
cached_headers: get_default_headers(&ini),
9293
cached_log_level: get_default_log_level(&ini),
9394
cached_vcs_remote: get_default_vcs_remote(&ini),
95+
max_retries: get_max_retries(&ini),
9496
ini,
9597
cached_token_data: token_embedded_data,
9698
}
@@ -468,27 +470,8 @@ impl Config {
468470
}
469471

470472
/// Returns the configured maximum number of retries for failed HTTP requests.
471-
pub fn get_max_retry_count(&self) -> u32 {
472-
match max_retries_from_env() {
473-
Ok(Some(val)) => return val,
474-
Ok(None) => (),
475-
Err(e) => {
476-
warn!(
477-
"Ignoring invalid {MAX_RETRIES_ENV_VAR} environment variable: {}",
478-
e
479-
);
480-
}
481-
};
482-
483-
match max_retries_from_ini(&self.ini) {
484-
Ok(Some(val)) => return val,
485-
Ok(None) => (),
486-
Err(e) => {
487-
warn!("Ignoring invalid {MAX_RETRIES_INI_KEY} ini key: {}", e);
488-
}
489-
};
490-
491-
DEFAULT_RETRIES
473+
pub fn max_retries(&self) -> u32 {
474+
self.max_retries
492475
}
493476

494477
/// Return the DSN
@@ -539,6 +522,32 @@ impl Config {
539522
}
540523
}
541524

525+
/// Obtains the maximum number of retries from the environment or the ini file.
526+
/// Environment variable takes precedence over the ini file. If neither is set,
527+
/// the default value is returned.
528+
fn get_max_retries(ini: &Ini) -> u32 {
529+
match max_retries_from_env() {
530+
Ok(Some(val)) => return val,
531+
Ok(None) => (),
532+
Err(e) => {
533+
warn!(
534+
"Ignoring invalid {MAX_RETRIES_ENV_VAR} environment variable: {}",
535+
e
536+
);
537+
}
538+
};
539+
540+
match max_retries_from_ini(ini) {
541+
Ok(Some(val)) => return val,
542+
Ok(None) => (),
543+
Err(e) => {
544+
warn!("Ignoring invalid {MAX_RETRIES_INI_KEY} ini key: {}", e);
545+
}
546+
};
547+
548+
DEFAULT_RETRIES
549+
}
550+
542551
/// Computes the maximum number of retries from the `SENTRY_HTTP_MAX_RETRIES` environment variable.
543552
/// Returns `Ok(None)` if the environment variable is not set, other errors are returned as is.
544553
fn max_retries_from_env() -> Result<Option<u32>> {
@@ -709,6 +718,7 @@ impl Clone for Config {
709718
cached_log_level: self.cached_log_level,
710719
cached_vcs_remote: self.cached_vcs_remote.clone(),
711720
cached_token_data: self.cached_token_data.clone(),
721+
max_retries: self.max_retries,
712722
}
713723
}
714724
}
@@ -794,6 +804,7 @@ mod tests {
794804
cached_log_level: LevelFilter::Off,
795805
cached_vcs_remote: String::new(),
796806
cached_token_data: None,
807+
max_retries: 0,
797808
};
798809

799810
assert_eq!(

0 commit comments

Comments
 (0)