MCPcopy Create free account
hub / github.com/ElementsProject/lightning / refresh_currency

Method refresh_currency

plugins/currencyrate-plugin/src/oracle.rs:358–449  ·  view source on GitHub ↗
(&self, currency: &str)

Source from the content-addressed store, hash-verified

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();

Callers 2

get_all_ratesMethod · 0.80
get_source_rateMethod · 0.80

Calls 9

mapMethod · 0.80
filterMethod · 0.80
get_rateMethod · 0.80
mark_successMethod · 0.80
mark_failureMethod · 0.80
background_refreshMethod · 0.80
getMethod · 0.45
insertMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected