Skip to content

Commit

Permalink
Revert "Thread local receive buffer"
Browse files Browse the repository at this point in the history
This reverts commit 3df6660.
  • Loading branch information
mxinden committed Sep 15, 2024
1 parent 52dfa91 commit 8699209
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
30 changes: 14 additions & 16 deletions neqo-bin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ struct Runner<'a, H: Handler> {
handler: H,
timeout: Option<Pin<Box<Sleep>>>,
args: &'a Args,
recv_buf: Vec<u8>,
send_buf: Vec<u8>,
}

Expand Down Expand Up @@ -427,22 +428,17 @@ impl<'a, H: Handler> Runner<'a, H> {
async fn process(&mut self) -> Result<(), io::Error> {
let mut should_read = true;
loop {
let output = crate::RECV_BUF.with_borrow_mut(
|recv_buf| -> Result<Output<&[u8]>, io::Error> {
// TODO: Cleanup?
let dgram = should_read
.then(|| self.socket.recv(&self.local_addr, recv_buf))
.transpose()?
.flatten();
should_read = dgram.is_some();

Ok(self
.client
.process(dgram, Instant::now(), &mut self.send_buf))
},
)?;

match output {
// TODO: Cleanup?
let dgram = should_read
.then(|| self.socket.recv(&self.local_addr, &mut self.recv_buf))
.transpose()?
.flatten();
should_read = dgram.is_some();

match self
.client
.process(dgram, Instant::now(), &mut self.send_buf)
{
Output::Datagram(dgram) => {
self.socket.writable().await?;
self.socket.send(dgram)?;
Expand Down Expand Up @@ -580,6 +576,7 @@ pub async fn client(mut args: Args) -> Res<()> {
local_addr: real_local,
socket: &mut socket,
timeout: None,
recv_buf: Vec::with_capacity(neqo_udp::RECV_BUF_SIZE),
send_buf: Vec::new(),
}
.run()
Expand All @@ -597,6 +594,7 @@ pub async fn client(mut args: Args) -> Res<()> {
local_addr: real_local,
socket: &mut socket,
timeout: None,
recv_buf: Vec::with_capacity(neqo_udp::RECV_BUF_SIZE),
send_buf: Vec::new(),
}
.run()
Expand Down
5 changes: 0 additions & 5 deletions neqo-bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#![allow(clippy::missing_errors_doc)]

use std::{
cell::RefCell,
fmt::{self, Display},
net::{SocketAddr, ToSocketAddrs},
path::PathBuf,
Expand All @@ -25,10 +24,6 @@ pub mod client;
pub mod server;
pub mod udp;

std::thread_local! {
static RECV_BUF: RefCell<Vec<u8>> = RefCell::new(vec![0; neqo_udp::RECV_BUF_SIZE]);
}

/// Firefox default value
///
/// See `network.buffer.cache.size` pref <https://searchfox.org/mozilla-central/rev/f6e3b81aac49e602f06c204f9278da30993cdc8a/modules/libpref/init/all.js#3212>
Expand Down
38 changes: 18 additions & 20 deletions neqo-bin/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ pub struct ServerRunner {
server: Box<dyn HttpServer>,
timeout: Option<Pin<Box<Sleep>>>,
sockets: Vec<(SocketAddr, crate::udp::Socket)>,
recv_buf: Vec<u8>,
send_buf: Vec<u8>,
}

Expand All @@ -225,33 +226,30 @@ impl ServerRunner {
server,
timeout: None,
sockets,
recv_buf: Vec::with_capacity(neqo_udp::RECV_BUF_SIZE),
send_buf: vec![],
}
}

// TODO: Could as well call it UDP IO now, given that it does both in and output.
async fn process(&mut self, mut inx: Option<usize>) -> Result<(), io::Error> {
loop {
let output = crate::RECV_BUF.with_borrow_mut(
|recv_buf| -> Result<Output<&[u8]>, io::Error> {
let mut dgram = if let Some(i) = inx {
let (host, socket) = self.sockets.get_mut(i).unwrap();
let dgram = socket.recv(host, recv_buf)?;
if dgram.is_none() {
// TODO: Better way to not try reading again?
inx.take();
}
dgram
} else {
None
};
Ok(self
.server
.process(dgram.take(), (self.now)(), &mut self.send_buf))
},
)?;

match output {
let mut dgram = if let Some(i) = inx {
let (host, socket) = self.sockets.get_mut(i).unwrap();
let dgram = socket.recv(host, &mut self.recv_buf)?;
if dgram.is_none() {
// TODO: Better way to not try reading again?
inx.take();
}
dgram
} else {
None
};

match self
.server
.process(dgram.take(), (self.now)(), &mut self.send_buf)
{
Output::Datagram(dgram) => {
let socket = {
let addr = dgram.source();
Expand Down

0 comments on commit 8699209

Please sign in to comment.