fix(aws): build the pricing client inside the cost estimate's 8 s bound

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-11-16 10:56:27 -08:00
1 parent 66a9993e69
commit f3d542ba60
3 files changed
+51 -12

No files matched your search

+20 -5
View File
@@ -347,20 +347,35 @@ impl RatesCache {
api: Option<&A>,
region: &str,
now: SystemTime,
) -> Option<RegionRates> {
self.rates_with::<A, _>(async { api }, region, now).await
}
/// [`Self::rates`] with the client built by `api`, which is awaited only
/// when the cache can't answer, and within the same [`FETCH_TIMEOUT`] as
/// the fetch (building one reads credentials, which can hang too).
pub async fn rates_with<A: PricingApi, B: std::borrow::Borrow<A>>(
&self,
api: impl Future<Output = Option<B>>,
region: &str,
now: SystemTime,
) -> Option<RegionRates> {
let cached = self.read(region, now);
if let Some((rates, true)) = cached {
return Some(rates);
}
let stale = cached.map(|(rates, _)| rates);
let Some(api) = api else {
return stale;
};
if self.recently_failed(region, now) {
return stale;
}
let fetched = match tokio::time::timeout(FETCH_TIMEOUT, fetch_rates(api, region)).await {
Ok(r) => r,
let fetch = async {
let api = api.await?;
Some(fetch_rates(api.borrow(), region).await)
};
let fetched = match tokio::time::timeout(FETCH_TIMEOUT, fetch).await {
Ok(Some(r)) => r,
// No client could be built: the stale cache answers.
Ok(None) => return stale,
Err(_) => Err(format!("timed out after {}s", FETCH_TIMEOUT.as_secs())),
};
match fetched {
+24
View File
@@ -371,3 +371,27 @@ async fn a_hanging_api_times_out_to_none() {
assert_eq!(started.elapsed(), FETCH_TIMEOUT);
assert_eq!(api.calls(), 1);
}
#[tokio::test(start_paused = true)]
async fn building_the_client_counts_against_the_same_timeout() {
let (_d, c) = cache();
c.write("us-west-2", &RATES, at(T0)).unwrap();
let started = tokio::time::Instant::now();
// The client never gets built (credentials that hang): stale answers at
// the fetch's own bound, not later.
let r = c
.rates_with::<FakeApi, FakeApi>(std::future::pending(), "us-west-2", at(T0 + 2 * DAY))
.await;
assert_eq!(r, Some(RATES));
assert_eq!(started.elapsed(), FETCH_TIMEOUT);
// A fresh cache never builds a client at all.
let fresh = c
.rates_with::<FakeApi, FakeApi>(
async { panic!("no client is built for a fresh cache") },
"us-west-2",
at(T0 + 60),
)
.await;
assert_eq!(fresh, Some(RATES));
}
+7 -7
View File
@@ -350,14 +350,14 @@ async fn cost_estimate(
_ => {
// Only now touch the connection (a keys connection reads the
// keychain); without one, a stale cache still answers.
let api = match aws_conn(connection_id, settings) {
Ok(conn) => sdk_config(&conn, pricing::PRICING_REGION)
.await
.ok()
.map(|cfg| SdkPricingApi::new(&cfg)),
Err(_) => None,
// Built inside the fetch's 8 s bound: reading a profile's
// credentials can hang as well (an SSO or process provider).
let api = async {
let conn = aws_conn(connection_id, settings).ok()?;
let cfg = sdk_config(&conn, pricing::PRICING_REGION).await.ok()?;
Some(SdkPricingApi::new(&cfg))
};
cache.rates(api.as_ref(), &region, now).await
cache.rates_with::<SdkPricingApi, _>(api, &region, now).await
}
}?;
Some(pricing::estimate(config, &rates.for_arch(config.arch)?))