Skip to content

Commit

Permalink
Use poll_recv_many for receiving messages to publish to the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini committed Jan 4, 2024
1 parent 0424068 commit 88bda9c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ members = [
"nats-server"
]

[patch.crates-io]
tokio = { git = "https://github.com/tokio-rs/tokio.git" }
23 changes: 19 additions & 4 deletions async-nats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ impl ConnectionHandler {
struct ProcessFut<'a> {
handler: &'a mut ConnectionHandler,
receiver: &'a mut mpsc::Receiver<Command>,
recv_buf: &'a mut Vec<Command>,
}

enum ExitReason {
Expand All @@ -451,6 +452,8 @@ impl ConnectionHandler {
}

impl<'a> ProcessFut<'a> {
const RECV_CHUNK_SIZE: usize = 16;

#[cold]
fn ping(&mut self) -> Poll<ExitReason> {
self.handler.pending_pings += 1;
Expand Down Expand Up @@ -516,13 +519,23 @@ impl ConnectionHandler {
let mut made_progress = true;
loop {
while !self.handler.connection.is_write_buf_full() {
match self.receiver.poll_recv(cx) {
debug_assert!(self.recv_buf.is_empty());

let Self {
recv_buf,
handler,
receiver,
} = &mut *self;
match receiver.poll_recv_many(cx, recv_buf, Self::RECV_CHUNK_SIZE) {
Poll::Pending => break,
Poll::Ready(Some(cmd)) => {
Poll::Ready(1..) => {
made_progress = true;
self.handler.handle_command(cmd);

for cmd in recv_buf.drain(..) {
handler.handle_command(cmd);
}
}
Poll::Ready(None) => return Poll::Ready(ExitReason::Closed),
Poll::Ready(0) => return Poll::Ready(ExitReason::Closed),
}
}

Expand Down Expand Up @@ -575,10 +588,12 @@ impl ConnectionHandler {
}
}

let mut recv_buf = Vec::with_capacity(ProcessFut::RECV_CHUNK_SIZE);
loop {
let process = ProcessFut {
handler: self,
receiver,
recv_buf: &mut recv_buf,
};
match process.await {
ExitReason::Disconnected(err) => {
Expand Down

0 comments on commit 88bda9c

Please sign in to comment.