Skip to content

Commit 362515c

Browse files
committed
Add config option to control JSONRPC request parallelism
1 parent 0ac2dbc commit 362515c

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

src/bin/electrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fn run_server(config: Arc<Config>) -> Result<()> {
4949
&config.daemon_dir,
5050
&config.blocks_dir,
5151
config.daemon_rpc_addr,
52+
config.daemon_parallelism,
5253
config.cookie_getter(),
5354
config.network_type,
5455
signal.clone(),

src/bin/tx-fingerprint-stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn main() {
3333
&config.daemon_dir,
3434
&config.blocks_dir,
3535
config.daemon_rpc_addr,
36+
config.daemon_parallelism,
3637
config.cookie_getter(),
3738
config.network_type,
3839
signal,

src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct Config {
2525
pub daemon_dir: PathBuf,
2626
pub blocks_dir: PathBuf,
2727
pub daemon_rpc_addr: SocketAddr,
28+
pub daemon_parallelism: usize,
2829
pub cookie: Option<String>,
2930
pub electrum_rpc_addr: SocketAddr,
3031
pub http_addr: SocketAddr,
@@ -132,6 +133,12 @@ impl Config {
132133
.help("Bitcoin daemon JSONRPC 'addr:port' to connect (default: 127.0.0.1:8332 for mainnet, 127.0.0.1:18332 for testnet and 127.0.0.1:18443 for regtest)")
133134
.takes_value(true),
134135
)
136+
.arg(
137+
Arg::with_name("daemon_parallelism")
138+
.long("daemon-parallelism")
139+
.help("Number of JSONRPC requests to send in parallel")
140+
.default_value("8")
141+
)
135142
.arg(
136143
Arg::with_name("monitoring_addr")
137144
.long("monitoring-addr")
@@ -386,6 +393,7 @@ impl Config {
386393
daemon_dir,
387394
blocks_dir,
388395
daemon_rpc_addr,
396+
daemon_parallelism: value_t_or_exit!(m, "daemon_parallelism", usize),
389397
cookie,
390398
utxos_limit: value_t_or_exit!(m, "utxos_limit", usize),
391399
electrum_rpc_addr,

src/daemon.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ impl Counter {
260260

261261
pub struct Daemon {
262262
daemon_dir: PathBuf,
263+
daemon_parallelism: usize,
263264
blocks_dir: PathBuf,
264265
network: Network,
265266
conn: Mutex<Connection>,
@@ -276,13 +277,15 @@ impl Daemon {
276277
daemon_dir: &PathBuf,
277278
blocks_dir: &PathBuf,
278279
daemon_rpc_addr: SocketAddr,
280+
daemon_parallelism: usize,
279281
cookie_getter: Arc<dyn CookieGetter>,
280282
network: Network,
281283
signal: Waiter,
282284
metrics: &Metrics,
283285
) -> Result<Daemon> {
284286
let daemon = Daemon {
285287
daemon_dir: daemon_dir.clone(),
288+
daemon_parallelism,
286289
blocks_dir: blocks_dir.clone(),
287290
network,
288291
conn: Mutex::new(Connection::new(
@@ -335,6 +338,7 @@ impl Daemon {
335338
pub fn reconnect(&self) -> Result<Daemon> {
336339
Ok(Daemon {
337340
daemon_dir: self.daemon_dir.clone(),
341+
daemon_parallelism: self.daemon_parallelism,
338342
blocks_dir: self.blocks_dir.clone(),
339343
network: self.network,
340344
conn: Mutex::new(self.conn.lock().unwrap().reconnect()?),
@@ -406,10 +410,17 @@ impl Daemon {
406410
fn requests(&self, method: &str, params_list: &[Value]) -> Result<Vec<Value>> {
407411
// Send in parallel as individual JSONRPC requests, with no batching.
408412
// See https://github.com/Blockstream/electrs/pull/33
409-
params_list
410-
.par_iter()
411-
.map(|params| self.retry_request(method, params))
412-
.collect()
413+
let pool = rayon::ThreadPoolBuilder::new()
414+
.num_threads(self.daemon_parallelism)
415+
.thread_name(|i| format!("rpc-requests-{}", i))
416+
.build()
417+
.unwrap();
418+
pool.install(|| {
419+
params_list
420+
.par_iter()
421+
.map(|params| self.retry_request(method, params))
422+
.collect()
423+
})
413424
}
414425

415426
// bitcoind JSONRPC API:

tests/common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ impl TestRunner {
8989
network_type,
9090
db_path: electrsdb.path().to_path_buf(),
9191
daemon_dir: daemon_subdir.clone(),
92+
daemon_parallelism: 3,
9293
blocks_dir: daemon_subdir.join("blocks"),
9394
daemon_rpc_addr: params.rpc_socket.into(),
9495
cookie: None,
@@ -127,6 +128,7 @@ impl TestRunner {
127128
&config.daemon_dir,
128129
&config.blocks_dir,
129130
config.daemon_rpc_addr,
131+
config.daemon_parallelism,
130132
config.cookie_getter(),
131133
config.network_type,
132134
signal.clone(),

0 commit comments

Comments
 (0)