Browse Source

Add metrics to blocktxids cache

tmp/parse_blk
Dagur Valberg Johannsson 5 years ago
parent
commit
6ba67cb9f6
  1. 2
      examples/index.rs
  2. 2
      src/bin/electrs.rs
  3. 15
      src/cache.rs

2
examples/index.rs

@ -17,7 +17,7 @@ fn run() -> Result<()> {
let config = Config::from_args();
let metrics = Metrics::new(config.monitoring_addr);
metrics.start();
let cache = Arc::new(BlockTxIDsCache::new(0));
let cache = Arc::new(BlockTxIDsCache::new(0, &metrics));
let daemon = Daemon::new(
&config.daemon_dir,

2
src/bin/electrs.rs

@ -28,7 +28,7 @@ fn run_server(config: &Config) -> Result<()> {
let signal = Waiter::start();
let metrics = Metrics::new(config.monitoring_addr);
metrics.start();
let blocktxids_cache = Arc::new(BlockTxIDsCache::new(config.blocktxids_cache_size));
let blocktxids_cache = Arc::new(BlockTxIDsCache::new(config.blocktxids_cache_size, &metrics));
let daemon = Daemon::new(
&config.daemon_dir,

15
src/cache.rs

@ -1,16 +1,27 @@
use crate::errors::*;
use crate::metrics::{Counter, MetricOpts, Metrics};
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use lru::LruCache;
use std::sync::Mutex;
pub struct BlockTxIDsCache {
map: Mutex<LruCache<Sha256dHash /* blockhash */, Vec<Sha256dHash /* txid */>>>,
hits: Counter,
misses: Counter,
}
impl BlockTxIDsCache {
pub fn new(capacity: usize) -> BlockTxIDsCache {
pub fn new(capacity: usize, metrics: &Metrics) -> BlockTxIDsCache {
BlockTxIDsCache {
map: Mutex::new(LruCache::new(capacity)),
hits: metrics.counter(MetricOpts::new(
"electrs_blocktxids_cache_hits",
"# of cache hits for list of transactions in a block",
)),
misses: metrics.counter(MetricOpts::new(
"electrs_blocktxids_cache_misses",
"# of cache misses for list of transactions in a block",
)),
}
}
@ -23,9 +34,11 @@ impl BlockTxIDsCache {
F: FnOnce() -> Result<Vec<Sha256dHash>>,
{
if let Some(txids) = self.map.lock().unwrap().get(blockhash) {
self.hits.inc();
return Ok(txids.clone());
}
self.misses.inc();
let txids = load_txids_func()?;
self.map.lock().unwrap().put(*blockhash, txids.clone());
Ok(txids)

Loading…
Cancel
Save