Skip to content

Commit dd63bae

Browse files
authored
gossip: Write to contact-info.bin file with a BufWriter (#4115)
Non-buffered write blocks the `save_contact_info` execution to around 200ms-1s. Buffered write minimizes the execution to 6-14 ms.
1 parent 9e9c4b5 commit dd63bae

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

gossip/src/cluster_info.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ use {
8787
collections::{HashMap, HashSet, VecDeque},
8888
fmt::Debug,
8989
fs::{self, File},
90-
io::BufReader,
90+
io::{BufReader, BufWriter, Write},
9191
iter::repeat,
9292
net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener, UdpSocket},
9393
num::NonZeroUsize,
@@ -349,15 +349,19 @@ impl ClusterInfo {
349349
let tmp_filename = &filename.with_extension("tmp");
350350

351351
match File::create(tmp_filename) {
352-
Ok(mut file) => {
353-
if let Err(err) = bincode::serialize_into(&mut file, &nodes) {
352+
Ok(file) => {
353+
let mut writer = BufWriter::new(file);
354+
if let Err(err) = bincode::serialize_into(&mut writer, &nodes) {
354355
warn!(
355356
"Failed to serialize contact info info {}: {}",
356357
tmp_filename.display(),
357358
err
358359
);
359360
return;
360361
}
362+
if let Err(err) = writer.flush() {
363+
warn!("Failed to save contact info: {err}");
364+
}
361365
}
362366
Err(err) => {
363367
warn!("Failed to create {}: {}", tmp_filename.display(), err);

0 commit comments

Comments
 (0)