fix: set HTTP client timeout and remove pagination double-fetch

- Set a 30-second default timeout on the shared reqwest client so
  future call sites can't hang indefinitely.
- Use ensure_page_within() to clamp the page before querying instead
  of fetching, detecting an empty result, and re-fetching.
This commit is contained in:
Jon Seager 2026-02-06 18:19:37 +00:00
parent 42b971658d
commit ca43ad1d00
No known key found for this signature in database
2 changed files with 11 additions and 19 deletions

View file

@ -138,7 +138,11 @@ impl AppState {
ai_usage_repo, ai_usage_repo,
webauthn: config.webauthn, webauthn: config.webauthn,
challenge_store: Arc::new(ChallengeStore::new()), challenge_store: Arc::new(ChallengeStore::new()),
http_client: reqwest::Client::new(), #[allow(clippy::expect_used)]
http_client: reqwest::ClientBuilder::new()
.timeout(std::time::Duration::from_secs(30))
.build()
.expect("failed to build HTTP client"),
foursquare_url: config.foursquare_url, foursquare_url: config.foursquare_url,
foursquare_api_key: config.foursquare_api_key, foursquare_api_key: config.foursquare_api_key,
openrouter_url: config.openrouter_url, openrouter_url: config.openrouter_url,

View file

@ -54,25 +54,14 @@ where
} }
PageSize::Limited(page_size) => { PageSize::Limited(page_size) => {
let limit = i64::from(page_size); let limit = i64::from(page_size);
let mut page = request.page();
let offset = i64::from(page - 1).saturating_mul(limit);
let total = fetch_count(pool, count_query, search).await?; let total = fetch_count(pool, count_query, search).await?;
let mut records = fetch_records::<R>( // Clamp page to valid range before fetching
pool, let adjusted = (*request).ensure_page_within(total as u64);
base_query, let page = adjusted.page();
order_clause,
search,
Some((limit, offset)),
)
.await?;
if page > 1 && records.is_empty() && total > 0 {
let last_page = ((total + limit - 1) / limit) as u32;
page = last_page.max(1);
let offset = i64::from(page - 1).saturating_mul(limit); let offset = i64::from(page - 1).saturating_mul(limit);
records = fetch_records::<R>(
let records = fetch_records::<R>(
pool, pool,
base_query, base_query,
order_clause, order_clause,
@ -80,7 +69,6 @@ where
Some((limit, offset)), Some((limit, offset)),
) )
.await?; .await?;
}
let mut items = Vec::with_capacity(records.len()); let mut items = Vec::with_capacity(records.len());
for record in records { for record in records {