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

Method background_refresh

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

Source from the content-addressed store, hash-verified

449 }
450
451 fn background_refresh(&self, currency: &str) {
452 let inner = self.inner.clone();
453 let client = self.client.clone();
454 let proxy = self.proxy.clone();
455 let currency = currency.to_owned();
456 tokio::spawn(async move {
457 tokio::time::sleep(SOURCE_TIMEOUT_SECS * 5).await;
458 loop {
459 let mut inner = inner.lock().await;
460 let now = Instant::now();
461 inner.reset_backoff_if_needed(now);
462 let available_sources: Vec<String> = inner
463 .sources
464 .iter()
465 .filter(|(_, source_health)| source_health.backoff_until < now)
466 .map(|(name, _)| name)
467 .cloned()
468 .collect();
469
470 let prices = inner
471 .currencies
472 .get(&currency)
473 .map(|currency_cache| &currency_cache.prices);
474
475 let mut sources_by_staleness: Vec<(String, Option<Instant>)> = available_sources
476 .into_iter()
477 .map(|name| {
478 let last_fetch = prices
479 .and_then(|prices| prices.get(&name))
480 .map(|price_cache| price_cache.timestamp);
481 (name, last_fetch)
482 })
483 .collect();
484
485 sources_by_staleness.sort_by_key(|(_, s)| *s);
486
487 let stale_cutoff = now - SERVE_TTL + 2 * SOURCE_TIMEOUT_SECS;
488 if sources_by_staleness
489 .last()
490 .and_then(|(_, timestamp)| *timestamp)
491 .is_some_and(|timestamp| timestamp > stale_cutoff)
492 {
493 sources_by_staleness.clear();
494 }
495
496 log::trace!(
497 "sources_by_staleness: {}",
498 sources_by_staleness
499 .iter()
500 .map(|(n, p)| format!(
501 "{n}:{}",
502 p.map(|f| f.elapsed().as_secs()).unwrap_or(0)
503 ))
504 .collect::<Vec<String>>()
505 .join(", ")
506 );
507
508 for (name, _) in sources_by_staleness {

Callers 1

refresh_currencyMethod · 0.80

Calls 11

mapMethod · 0.80
filterMethod · 0.80
get_rateMethod · 0.80
mark_successMethod · 0.80
is_drift_okMethod · 0.80
mark_failureMethod · 0.80
getMethod · 0.45
unwrapMethod · 0.45
insertMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected