From c7c873c23427f55c9e2911fbb56d956356e3c22e Mon Sep 17 00:00:00 2001 From: junderw Date: Sat, 27 Jul 2024 16:22:03 +0900 Subject: [PATCH] Feat: batching of RPC requests. --- rust-toolchain | 2 +- src/electrum/server.rs | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/rust-toolchain b/rust-toolchain index bfe79d0b..d456f745 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -1.70 +1.80 diff --git a/src/electrum/server.rs b/src/electrum/server.rs index ae427cd2..820c8961 100644 --- a/src/electrum/server.rs +++ b/src/electrum/server.rs @@ -521,18 +521,25 @@ impl Connection { trace!("RPC {:?}", msg); match msg { Message::Request(line) => { - let cmd: Value = from_str(&line).chain_err(|| "invalid JSON format")?; - let reply = match ( - cmd.get("method"), - cmd.get("params").unwrap_or(&empty_params), - cmd.get("id"), - ) { - (Some(Value::String(method)), Value::Array(params), Some(id)) => { - self.handle_command(method, params, id)? - } - _ => bail!("invalid command: {}", cmd), + let cmd: [Value; 1] = [from_str(&line).chain_err(|| "invalid JSON format")?]; + let cmds = match &cmd[..] { + [Value::Array(arr)] => arr, + x => x, }; - self.send_values(&[reply])? + let mut replies = Vec::with_capacity(cmds.len()); + for cmd in cmds { + replies.push(match ( + cmd.get("method"), + cmd.get("params").unwrap_or(&empty_params), + cmd.get("id"), + ) { + (Some(Value::String(method)), Value::Array(params), Some(id)) => { + self.handle_command(method, params, id)? + } + _ => bail!("invalid command: {}", cmd), + }); + } + self.send_values(&replies)? } Message::PeriodicUpdate => { let values = self