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

Fuse the pull consumer Stream after terminal error #751

Merged
merged 2 commits into from
Dec 2, 2022
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
15 changes: 13 additions & 2 deletions async-nats/src/jetstream/consumer/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ pub struct Stream {
heartbeat_handle: Option<JoinHandle<()>>,
last_seen: Arc<Mutex<Instant>>,
heartbeats_missing: tokio::sync::mpsc::Receiver<()>,
terminated: bool,
}

impl Drop for Stream {
Expand Down Expand Up @@ -469,7 +470,11 @@ impl Stream {
pending_reset = true;
}
}
Err(err) => request_result_tx.send(Err(err)).await.unwrap(),
Err(err) => {
if let Err(err) = request_result_tx.send(Err(err)).await {
debug!("failed to sent request result: {}", err);
}
},
}
},
_ = request_rx.changed() => debug!("task received request request"),
Expand Down Expand Up @@ -538,6 +543,7 @@ impl Stream {
pending_request: false,
last_seen,
heartbeats_missing: missed_heartbeat_rx,
terminated: false,
})
}
}
Expand All @@ -549,6 +555,9 @@ impl futures::Stream for Stream {
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
if self.terminated {
return Poll::Ready(None);
}
loop {
trace!("pending messages: {}", self.pending_messages);
if (self.pending_messages <= self.batch_config.batch / 2
Expand All @@ -564,17 +573,19 @@ impl futures::Stream for Stream {
match self.heartbeats_missing.poll_recv(cx) {
Poll::Ready(resp) => match resp {
Some(()) => {
self.terminated = true;
trace!("received missing heartbeats notification");
return Poll::Ready(Some(Err(Box::new(std::io::Error::new(
std::io::ErrorKind::TimedOut,
"did not receive idle heartbeat in time",
)))));
}
None => {
self.terminated = true;
return Poll::Ready(Some(Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"unexpected termination of heartbeat checker",
)))))
)))));
}
},
Poll::Pending => {
Expand Down
2 changes: 2 additions & 0 deletions async-nats/tests/jetstream_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,8 @@ mod jetstream {
.kind(),
std::io::ErrorKind::TimedOut
);
// after terminal error, consumer should always return none.
assert!(messages.next().await.is_none());
assert!(now.elapsed().le(&Duration::from_secs(50)));
}

Expand Down