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

Use poll_recv_many for processing commands to be written to the nats server #1189

Merged
merged 1 commit into from
Feb 2, 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
2 changes: 1 addition & 1 deletion async-nats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ regex = "1.9.1"
serde = { version = "1.0.184", features = ["derive"] }
serde_json = "1.0.104"
serde_repr = "0.1.16"
tokio = { version = "1.29.0", features = ["macros", "rt", "fs", "net", "sync", "time", "io-util"] }
tokio = { version = "1.36", features = ["macros", "rt", "fs", "net", "sync", "time", "io-util"] }
url = { version = "2"}
tokio-rustls = "0.25"
rustls-pemfile = "2"
Expand Down
24 changes: 20 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;
Jarema marked this conversation as resolved.
Show resolved Hide resolved

#[cold]
fn ping(&mut self) -> Poll<ExitReason> {
self.handler.pending_pings += 1;
Expand Down Expand Up @@ -519,13 +522,24 @@ 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());
Jarema marked this conversation as resolved.
Show resolved Hide resolved

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),
// TODO: replace `_` with `0` after bumping MSRV to 1.75
Poll::Ready(_) => return Poll::Ready(ExitReason::Closed),
}
}

Expand Down Expand Up @@ -578,10 +592,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
Loading