node: ability to clean up peers at code
This commit is contained in:
parent
4e532fb6ad
commit
8244f2a2fb
1 changed files with 39 additions and 4 deletions
|
@ -55,6 +55,8 @@ pub struct Node {
|
||||||
stop_needed: AtomicBool,
|
stop_needed: AtomicBool,
|
||||||
/// Flag to check if app exit is needed after [`Server`] stop.
|
/// Flag to check if app exit is needed after [`Server`] stop.
|
||||||
exit_after_stop: AtomicBool,
|
exit_after_stop: AtomicBool,
|
||||||
|
/// Flag to clean-up peers and restart the [`Server`].
|
||||||
|
clean_up_peers: AtomicBool,
|
||||||
|
|
||||||
/// An error occurred on [`Server`] start.
|
/// An error occurred on [`Server`] start.
|
||||||
error: Arc<RwLock<Option<Error>>>
|
error: Arc<RwLock<Option<Error>>>
|
||||||
|
@ -71,7 +73,8 @@ impl Default for Node {
|
||||||
stop_needed: AtomicBool::new(false),
|
stop_needed: AtomicBool::new(false),
|
||||||
exit_after_stop: AtomicBool::new(false),
|
exit_after_stop: AtomicBool::new(false),
|
||||||
start_stratum_needed: AtomicBool::new(false),
|
start_stratum_needed: AtomicBool::new(false),
|
||||||
error: Arc::new(RwLock::new(None))
|
error: Arc::new(RwLock::new(None)),
|
||||||
|
clean_up_peers: AtomicBool::new(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -147,6 +150,11 @@ impl Node {
|
||||||
NODE_STATE.restart_needed.load(Ordering::Relaxed)
|
NODE_STATE.restart_needed.load(Ordering::Relaxed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check if clean-up of [`Server`] peers is needed.
|
||||||
|
fn clean_up_peers_needed() -> bool {
|
||||||
|
NODE_STATE.clean_up_peers.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get node [`Server`] statistics.
|
/// Get node [`Server`] statistics.
|
||||||
pub fn get_stats() -> Option<ServerStats> {
|
pub fn get_stats() -> Option<ServerStats> {
|
||||||
NODE_STATE.stats.read().clone()
|
NODE_STATE.stats.read().clone()
|
||||||
|
@ -229,10 +237,17 @@ impl Node {
|
||||||
Ok(mut server) => {
|
Ok(mut server) => {
|
||||||
let mut first_start = true;
|
let mut first_start = true;
|
||||||
loop {
|
loop {
|
||||||
if Self::is_restarting() {
|
// Restart server if request or peers clean up is needed
|
||||||
|
let clean_up_peers = Self::clean_up_peers_needed();
|
||||||
|
if Self::is_restarting() || clean_up_peers {
|
||||||
server.stop();
|
server.stop();
|
||||||
|
// Wait server after stop.
|
||||||
thread::sleep(Duration::from_millis(5000));
|
thread::sleep(Duration::from_millis(5000));
|
||||||
// Reset stratum stats
|
// Clean-up peers of requested.
|
||||||
|
if clean_up_peers {
|
||||||
|
Node::clean_up_peers(true);
|
||||||
|
}
|
||||||
|
// Reset stratum stats.
|
||||||
{
|
{
|
||||||
let mut w_stratum_stats = NODE_STATE.stratum_stats.write();
|
let mut w_stratum_stats = NODE_STATE.stratum_stats.write();
|
||||||
*w_stratum_stats = StratumStats::default();
|
*w_stratum_stats = StratumStats::default();
|
||||||
|
@ -365,6 +380,27 @@ impl Node {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Clean-up [`Server`] peers from storage.
|
||||||
|
pub fn clean_up_peers(force: bool) {
|
||||||
|
if force || !Node::is_running() {
|
||||||
|
NODE_STATE.clean_up_peers.store(false, Ordering::Relaxed);
|
||||||
|
// Get saved server config.
|
||||||
|
let config = NodeConfig::node_server_config();
|
||||||
|
let mut server_config = config.server.clone();
|
||||||
|
// Remove peers folder.
|
||||||
|
let mut tmp_dir = PathBuf::from(&server_config.db_root);
|
||||||
|
tmp_dir.push("peers");
|
||||||
|
if tmp_dir.exists() {
|
||||||
|
match fs::remove_dir_all(tmp_dir) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_) => { println!("Cannot remove peers dir") }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
NODE_STATE.clean_up_peers.store(true, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get synchronization status i18n text.
|
/// Get synchronization status i18n text.
|
||||||
pub fn get_sync_status_text() -> String {
|
pub fn get_sync_status_text() -> String {
|
||||||
if Node::is_stopping() {
|
if Node::is_stopping() {
|
||||||
|
@ -493,7 +529,6 @@ fn start_node_server() -> Result<Server, Error> {
|
||||||
// Fix to avoid too many opened files.
|
// Fix to avoid too many opened files.
|
||||||
server_config.p2p_config.peer_min_preferred_outbound_count =
|
server_config.p2p_config.peer_min_preferred_outbound_count =
|
||||||
server_config.p2p_config.peer_max_outbound_count;
|
server_config.p2p_config.peer_max_outbound_count;
|
||||||
|
|
||||||
// Remove temporary file dir.
|
// Remove temporary file dir.
|
||||||
{
|
{
|
||||||
let mut tmp_dir = PathBuf::from(&server_config.db_root);
|
let mut tmp_dir = PathBuf::from(&server_config.db_root);
|
||||||
|
|
Loading…
Reference in a new issue