(&self, currency: &str)
| 356 | } |
| 357 | |
| 358 | async fn refresh_currency(&self, currency: &str) -> Result<(), anyhow::Error> { |
| 359 | let mut inner = self.inner.lock().await; |
| 360 | let now = Instant::now(); |
| 361 | let mut start_background_refresh = false; |
| 362 | |
| 363 | let mut source_candidates: Vec<String> = inner |
| 364 | .sources |
| 365 | .iter() |
| 366 | .filter(|(_, s)| now >= s.backoff_until) |
| 367 | .map(|(name, _)| name.clone()) |
| 368 | .collect(); |
| 369 | |
| 370 | if source_candidates.is_empty() { |
| 371 | log::warn!("all sources have failed recently, trying them immediately again"); |
| 372 | source_candidates = inner.sources.keys().cloned().collect(); |
| 373 | } |
| 374 | |
| 375 | let currency_cache = if let Some(c) = inner.currencies.get(currency) { |
| 376 | c |
| 377 | } else { |
| 378 | inner |
| 379 | .currencies |
| 380 | .insert(currency.to_owned(), CurrencyCache::new()); |
| 381 | start_background_refresh = true; |
| 382 | inner.currencies.get(currency).unwrap() |
| 383 | }; |
| 384 | source_candidates.retain(|c| { |
| 385 | currency_cache |
| 386 | .prices |
| 387 | .get(c) |
| 388 | .is_none_or(|p| p.timestamp + SERVE_TTL <= now) |
| 389 | }); |
| 390 | |
| 391 | drop(inner); |
| 392 | |
| 393 | let futures = source_candidates.iter().map(|source_name| { |
| 394 | let inner = self.inner.clone(); |
| 395 | let client = self.client.clone(); |
| 396 | let source_name = source_name.clone(); |
| 397 | let proxy = self.proxy.clone(); |
| 398 | |
| 399 | async move { |
| 400 | let source = { |
| 401 | let inner = inner.lock().await; |
| 402 | inner.sources.get(&source_name).unwrap().source.clone() |
| 403 | }; |
| 404 | |
| 405 | let rate_result = source.get_rate(proxy, &client, currency).await; |
| 406 | |
| 407 | let mut inner = inner.lock().await; |
| 408 | |
| 409 | let source_health = inner.sources.get_mut(&source_name).unwrap(); |
| 410 | |
| 411 | match rate_result { |
| 412 | Ok(price) => { |
| 413 | source_health.mark_success(); |
| 414 | |
| 415 | let cache = inner.currencies.get_mut(currency).unwrap(); |
no test coverage detected