1
0
Fork 0
mirror of https://github.com/romanz/electrs.git synced 2025-02-23 14:50:45 +01:00

Reuse buffer in p2p handling (#910)

This commit is contained in:
Riccardo Casatta 2023-07-23 08:57:50 +02:00 committed by GitHub
parent b041adf9bc
commit fae1e61946
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use bitcoin::blockdata::block::Header as BlockHeader;
use bitcoin::consensus::Encodable;
use bitcoin::{
consensus::{
encode::{self, ReadExt, VarInt},
@ -173,6 +174,7 @@ impl Connection {
);
let stream = Arc::clone(&conn);
let mut buffer = vec![];
crate::thread::spawn("p2p_send", move || loop {
use std::net::Shutdown;
let msg = match send_duration.observe_duration("wait", || tx_recv.recv()) {
@ -193,8 +195,12 @@ impl Connection {
magic,
payload: msg,
};
buffer.clear();
raw_msg
.consensus_encode(&mut buffer)
.expect("in-memory writers don't error");
(&*stream)
.write_all(encode::serialize(&raw_msg).as_slice())
.write_all(buffer.as_slice())
.context("p2p failed to send")
})?;
});