Keep network status & ring in a tokio::sync::watch

advantages
- reads don't prevent preparing writes
- can be followed from other parts of the system by cloning the receiver
This commit is contained in:
Alex Auvolat
2020-04-11 23:53:32 +02:00
parent 5dd59e437d
commit 9c931f5eda
9 changed files with 165 additions and 133 deletions
+14 -12
View File
@@ -1,15 +1,14 @@
use std::path::PathBuf;
use futures_util::future::*;
use tokio::fs;
use tokio::prelude::*;
use tokio::sync::Mutex;
use futures_util::future::*;
use crate::background::*;
use crate::data::*;
use crate::error::Error;
use crate::proto::*;
use crate::background::*;
pub struct BlockManager {
pub data_dir: PathBuf,
@@ -19,10 +18,11 @@ pub struct BlockManager {
impl BlockManager {
pub fn new(db: &sled::Db, data_dir: PathBuf) -> Self {
let rc = db.open_tree("block_local_rc")
let rc = db
.open_tree("block_local_rc")
.expect("Unable to open block_local_rc tree");
rc.set_merge_operator(rc_merge);
Self{
Self {
rc,
data_dir,
lock: Mutex::new(()),
@@ -81,18 +81,20 @@ impl BlockManager {
background.spawn(tokio::fs::remove_file(path).map_err(Into::into));
Ok(())
}
Some(_) => Ok(())
Some(_) => Ok(()),
}
}
}
fn rc_merge(_key: &[u8], old: Option<&[u8]>, new: &[u8]) -> Option<Vec<u8>> {
let old = old.map(|x| {
assert!(x.len() == 8);
let mut x8 = [0u8; 8];
x8.copy_from_slice(x);
u64::from_be_bytes(x8)
}).unwrap_or(0);
let old = old
.map(|x| {
assert!(x.len() == 8);
let mut x8 = [0u8; 8];
x8.copy_from_slice(x);
u64::from_be_bytes(x8)
})
.unwrap_or(0);
assert!(new.len() == 1);
let new = match new[0] {
0 => {