Skip to content

Commit

Permalink
Move the tripping to a separate impl on the atomicbool
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamc committed Jun 13, 2024
1 parent 25359d9 commit 51bb9f9
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions gha-cache/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ pub enum Error {
#[error("Failed to initialize the client: {0}")]
InitError(Box<dyn std::error::Error + Send + Sync>),

#[error("GitHub Actions Cache throttled Magic Nix Cache. Not trying to use it again on this run.")]
#[error(
"GitHub Actions Cache throttled Magic Nix Cache. Not trying to use it again on this run."
)]
CircuitBreakerTripped,

#[error("Request error: {0}")]
Expand Down Expand Up @@ -280,18 +282,6 @@ impl Api {
self.circuit_breaker_429_tripped.load(Ordering::Relaxed)
}

fn circuit_breaker_trip_on_429(&self, e: &Error) {
if let Error::ApiError {
status: reqwest::StatusCode::TOO_MANY_REQUESTS,
info: ref _info,
} = e
{
tracing::info!("Disabling GitHub Actions Cache due to 429: Too Many Requests");
self.circuit_breaker_429_tripped
.store(true, Ordering::Relaxed);
}
}

/// Mutates the cache version/namespace.
pub fn mutate_version(&mut self, data: &[u8]) {
self.version_hasher.update(data);
Expand Down Expand Up @@ -408,13 +398,7 @@ impl Api {

drop(permit);

if let Err(Error::ApiError {
status: reqwest::StatusCode::TOO_MANY_REQUESTS,
info: ref _info,
}) = r
{
circuit_breaker_429_tripped.store(true, Ordering::Relaxed);
}
circuit_breaker_429_tripped.check_result(&r);

r
})
Expand Down Expand Up @@ -475,9 +459,7 @@ impl Api {
.check_json()
.await;

if let Err(ref e) = res {
self.circuit_breaker_trip_on_429(e);
}
self.circuit_breaker_429_tripped.check_result(&res);

match res {
Ok(entry) => Ok(Some(entry)),
Expand Down Expand Up @@ -520,9 +502,7 @@ impl Api {
.check_json()
.await;

if let Err(ref e) = res {
self.circuit_breaker_trip_on_429(e);
}
self.circuit_breaker_429_tripped.check_result(&res);

res
}
Expand All @@ -549,7 +529,7 @@ impl Api {
.check()
.await
{
self.circuit_breaker_trip_on_429(&e);
self.circuit_breaker_429_tripped.check_err(&e);
return Err(e);
}

Expand Down Expand Up @@ -619,3 +599,27 @@ async fn handle_error(res: reqwest::Response) -> Error {

Error::ApiError { status, info }
}

trait AtomicCircuitBreaker {
fn check_err(&self, e: &Error);
fn check_result<T>(&self, r: &std::result::Result<T, Error>);
}

impl AtomicCircuitBreaker for AtomicBool {
fn check_result<T>(&self, r: &std::result::Result<T, Error>) {
if let Err(ref e) = r {
self.check_err(e)
}
}

fn check_err(&self, e: &Error) {
if let Error::ApiError {
status: reqwest::StatusCode::TOO_MANY_REQUESTS,
info: ref _info,
} = e
{
tracing::info!("Disabling GitHub Actions Cache due to 429: Too Many Requests");
self.store(true, Ordering::Relaxed);
}
}
}

0 comments on commit 51bb9f9

Please sign in to comment.