1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 09:54:09 +01:00

Call Tracker::sync only in server.rs

This way, the RPC server is running (but not responding) during initial sync.

Following https://github.com/romanz/electrs/issues/534.
This commit is contained in:
Roman Zeyde 2021-10-15 11:11:41 +03:00
parent 9eb415e94d
commit 12496c44be
2 changed files with 30 additions and 30 deletions

View File

@ -124,7 +124,8 @@ pub struct Rpc {
impl Rpc {
/// Perform initial index sync (may take a while on first run).
pub fn new(config: &Config, mut tracker: Tracker) -> Result<Self> {
pub fn new(config: &Config) -> Result<Self> {
let tracker = Tracker::new(&config)?;
let rpc_duration = tracker.metrics().histogram_vec(
"rpc_duration",
"RPC duration (in seconds)",
@ -134,10 +135,6 @@ impl Rpc {
let signal = Signal::new();
let daemon = Daemon::connect(config, signal.exit_flag(), tracker.metrics())?;
tracker
.sync(&daemon, signal.exit_flag())
.context("initial sync failed")?;
let cache = Cache::new(tracker.metrics());
Ok(Self {
tracker,

View File

@ -13,7 +13,6 @@ use crate::{
electrum::{Client, Rpc},
signals::ExitError,
thread::spawn,
tracker::Tracker,
};
struct Peer {
@ -61,11 +60,7 @@ pub fn run() -> Result<()> {
fn serve() -> Result<()> {
let config = Config::from_args();
let tracker = Tracker::new(&config)?;
let mut rpc = Rpc::new(&config, tracker)?;
if config.sync_once {
return Ok(());
}
let mut rpc = Rpc::new(&config)?;
let (server_tx, server_rx) = unbounded();
if !config.disable_electrum_rpc {
@ -77,28 +72,36 @@ fn serve() -> Result<()> {
let new_block_rx = rpc.new_block_notification();
let mut peers = HashMap::<usize, Peer>::new();
loop {
select! {
recv(rpc.signal().receiver()) -> result => {
result.context("signal channel disconnected")?;
rpc.signal().exit_flag().poll().context("RPC server interrupted")?;
},
recv(new_block_rx) -> result => match result {
Ok(_) => (), // sync and update
Err(_) => break, // daemon is shutting down
},
recv(server_rx) -> event => {
let event = event.context("server disconnected")?;
handle_event(&rpc, &mut peers, event);
},
default(config.wait_duration) => (), // sync and update
};
if !server_rx.is_empty() {
continue; // continue RPC processing (if not done)
rpc.sync().context("sync failed")?; // initial sync and compaction may take a few hours
if config.sync_once {
return Ok(());
}
rpc.sync().context("rpc sync failed")?;
peers = notify_peers(&rpc, peers); // peers are disconnected on error.
loop {
select! {
// Handle signals for graceful shutdown
recv(rpc.signal().receiver()) -> result => {
result.context("signal channel disconnected")?;
rpc.signal().exit_flag().poll().context("RPC server interrupted")?;
},
// Handle new blocks' notifications
recv(new_block_rx) -> result => match result {
Ok(_) => break, // sync and update
Err(_) => return Ok(()), // daemon is shutting down
},
// Handle Electrum RPC requests
recv(server_rx) -> event => {
let event = event.context("server disconnected")?;
handle_event(&rpc, &mut peers, event);
},
default(config.wait_duration) => break, // sync and update
};
// continue RPC processing (if more requests are pending)
if server_rx.is_empty() {
break;
}
}
}
Ok(())
}
fn notify_peers(rpc: &Rpc, peers: HashMap<usize, Peer>) -> HashMap<usize, Peer> {