Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bin/client): use BufWriter when writing to file #1756

Merged
merged 8 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions neqo-bin/src/bin/client/http09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
cell::RefCell,
collections::{HashMap, VecDeque},
fs::File,
io::Write,
io::{BufWriter, Write},
net::SocketAddr,
path::PathBuf,
rc::Rc,
Expand All @@ -29,7 +29,7 @@ use super::{get_output_file, Args, KeyUpdateState, Res};
use crate::qlog_new;

pub struct Handler<'a> {
streams: HashMap<StreamId, Option<File>>,
streams: HashMap<StreamId, Option<BufWriter<File>>>,
url_queue: VecDeque<Url>,
all_paths: Vec<PathBuf>,
args: &'a Args,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<'b> Handler<'b> {
client: &mut Connection,
stream_id: StreamId,
output_read_data: bool,
maybe_out_file: &mut Option<File>,
maybe_out_file: &mut Option<BufWriter<File>>,
) -> Res<bool> {
let mut data = vec![0; 4096];
loop {
Expand All @@ -246,8 +246,7 @@ impl<'b> Handler<'b> {
}

fn read(&mut self, client: &mut Connection, stream_id: StreamId) -> Res<()> {
let mut maybe_maybe_out_file = self.streams.get_mut(&stream_id);
match &mut maybe_maybe_out_file {
match self.streams.get_mut(&stream_id) {
None => {
println!("Data on unexpected stream: {stream_id}");
return Ok(());
Expand All @@ -261,7 +260,9 @@ impl<'b> Handler<'b> {
)?;

if fin_recvd {
if maybe_out_file.is_none() {
if let Some(mut out_file) = maybe_out_file.take() {
out_file.flush()?;
} else {
println!("<FIN[{stream_id}]>");
}
self.streams.remove(&stream_id);
Expand Down
12 changes: 8 additions & 4 deletions neqo-bin/src/bin/client/http3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
collections::{HashMap, VecDeque},
fmt::Display,
fs::File,
io::Write,
io::{BufWriter, Write},
net::SocketAddr,
path::PathBuf,
rc::Rc,
Expand Down Expand Up @@ -269,7 +269,7 @@ impl StreamHandlerType {
}

struct DownloadStreamHandler {
out_file: Option<File>,
out_file: Option<BufWriter<File>>,
}

impl StreamHandler for DownloadStreamHandler {
Expand Down Expand Up @@ -300,8 +300,12 @@ impl StreamHandler for DownloadStreamHandler {
println!("READ[{}]: 0x{}", stream_id, hex(&data));
}

if fin && self.out_file.is_none() {
println!("<FIN[{stream_id}]>");
if fin {
if let Some(mut out_file) = self.out_file.take() {
out_file.flush()?;
} else {
println!("<FIN[{stream_id}]>");
}
}

Ok(true)
Expand Down
8 changes: 5 additions & 3 deletions neqo-bin/src/bin/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
collections::{HashMap, VecDeque},
fmt::{self, Display},
fs::{create_dir_all, File, OpenOptions},
io,
io::{self, BufWriter},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs},
path::PathBuf,
pin::Pin,
Expand Down Expand Up @@ -36,6 +36,8 @@ use url::{Origin, Url};
mod http09;
mod http3;

const BUFWRITER_BUFFER_SIZE: usize = 64 * 1024;

#[derive(Debug)]
pub enum ClientError {
ArgumentError(&'static str),
Expand Down Expand Up @@ -252,7 +254,7 @@ fn get_output_file(
url: &Url,
output_dir: &Option<PathBuf>,
all_paths: &mut Vec<PathBuf>,
) -> Option<File> {
) -> Option<BufWriter<File>> {
if let Some(ref dir) = output_dir {
let mut out_path = dir.clone();

Expand Down Expand Up @@ -284,7 +286,7 @@ fn get_output_file(
.ok()?;

all_paths.push(out_path);
Some(f)
Some(BufWriter::with_capacity(BUFWRITER_BUFFER_SIZE, f))
} else {
None
}
Expand Down