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

Resolve pubsub future only when nothing can happen anymore #72

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
136 changes: 79 additions & 57 deletions src/client/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl PubsubConnectionInner {
}
}

fn handle_message(&mut self, msg: resp::RespValue) -> Result<bool, error::Error> {
fn handle_message(&mut self, msg: resp::RespValue) -> Result<(), error::Error> {
let (message_type, topic, msg) = match msg {
resp::RespValue::Array(mut messages) => match (
messages.pop(),
Expand Down Expand Up @@ -193,38 +193,28 @@ impl PubsubConnectionInner {
)));
}
},
b"unsubscribe" => {
match self.subscriptions.entry(topic) {
Entry::Occupied(entry) => {
entry.remove_entry();
}
Entry::Vacant(vacant) => {
return Err(error::internal(format!(
"Unexpected unsubscribe message: {}",
vacant.key()
)));
}
b"unsubscribe" => match self.subscriptions.entry(topic) {
Entry::Occupied(entry) => {
entry.remove_entry();
}
if self.subscriptions.is_empty() {
return Ok(false);
Entry::Vacant(vacant) => {
return Err(error::internal(format!(
"Unexpected unsubscribe message: {}",
vacant.key()
)));
}
}
b"punsubscribe" => {
match self.psubscriptions.entry(topic) {
Entry::Occupied(entry) => {
entry.remove_entry();
}
Entry::Vacant(vacant) => {
return Err(error::internal(format!(
"Unexpected unsubscribe message: {}",
vacant.key()
)));
}
},
b"punsubscribe" => match self.psubscriptions.entry(topic) {
Entry::Occupied(entry) => {
entry.remove_entry();
}
if self.psubscriptions.is_empty() {
return Ok(false);
Entry::Vacant(vacant) => {
return Err(error::internal(format!(
"Unexpected unsubscribe message: {}",
vacant.key()
)));
}
}
},
b"message" => match self.subscriptions.get(&topic) {
Some(sender) => {
if let Err(error) = sender.unbounded_send(Ok(msg)) {
Expand Down Expand Up @@ -263,39 +253,32 @@ impl PubsubConnectionInner {
}
}

Ok(true)
Ok(())
}

/// Returns true, if there are still valid subscriptions at the end, or false if not, i.e. the whole thing can be dropped.
fn handle_messages(&mut self, cx: &mut Context) -> Result<bool, error::Error> {
fn handle_messages(&mut self, cx: &mut Context) -> Result<(), error::Error> {
loop {
match self.connection.poll_next_unpin(cx) {
Poll::Pending => return Ok(true),
Poll::Pending => return Ok(()),
Poll::Ready(None) => {
if self.subscriptions.is_empty() {
return Ok(false);
} else {
// This can only happen if the connection is closed server-side
for sub in self.subscriptions.values() {
sub.unbounded_send(Err(error::Error::Connection(
ConnectionReason::NotConnected,
)))
.unwrap();
}
for psub in self.psubscriptions.values() {
psub.unbounded_send(Err(error::Error::Connection(
ConnectionReason::NotConnected,
)))
.unwrap();
}
return Err(error::Error::Connection(ConnectionReason::NotConnected));
// This can only happen if the connection is closed server-side
Copy link
Author

@dippi dippi Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I removed the handling of the special case, because I couldn't think of a reason to keep it. Am I missing anything?

for sub in self.subscriptions.values() {
sub.unbounded_send(Err(error::Error::Connection(
ConnectionReason::NotConnected,
)))
.unwrap();
}
for psub in self.psubscriptions.values() {
psub.unbounded_send(Err(error::Error::Connection(
ConnectionReason::NotConnected,
)))
.unwrap();
}
return Err(error::Error::Connection(ConnectionReason::NotConnected));
}
Poll::Ready(Some(Ok(message))) => {
let message_result = self.handle_message(message)?;
if !message_result {
return Ok(false);
}
self.handle_message(message)?;
}
Poll::Ready(Some(Err(e))) => {
for sub in self.subscriptions.values() {
Expand Down Expand Up @@ -326,11 +309,11 @@ impl Future for PubsubConnectionInner {
let this_self = self.get_mut();
this_self.handle_new_subs(cx)?;
this_self.do_flush(cx)?;
let cont = this_self.handle_messages(cx)?;
if cont {
Poll::Pending
} else {
this_self.handle_messages(cx)?;
if this_self.out_rx.is_done() {
Poll::Ready(Ok(()))
} else {
Poll::Pending
}
}
}
Expand Down Expand Up @@ -456,6 +439,8 @@ impl PubsubConnection {
pub struct PubsubStream {
topic: String,
underlying: PubsubStreamInner,
// Note that, to keep the Future running, PubsubConnectionInner relies on PubsubStream to hold
// a reference to the connection. If that's ever changed remember to adapt the readiness check.
con: PubsubConnection,
}

Expand Down Expand Up @@ -540,4 +525,41 @@ mod test {
assert_eq!(result[1], "test-message-2".into());
assert_eq!(result[2], "test-message-3".into());
}

#[tokio::test]
/// Regression test for https://github.com/benashford/redis-async-rs/issues/50
async fn test_connection_remains_open_after_unsubscription() {
let addr = "127.0.0.1:6379".parse().unwrap();
let pubsub = super::pubsub_connect(addr)
.await
.expect("Cannot connect to Redis");

let topic_messages = pubsub
.subscribe("test-topic")
.await
.expect("Cannot subscribe to topic");
drop(topic_messages);

pubsub
.subscribe("test-topic")
.await
.expect("Cannot subscribe to topic");
}
}

#[tokio::test]
async fn test_connection_is_closed_after_channel_is_dropped() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do this test outside the test module, to have access to the private types. Is there a better way?

let addr = "127.0.0.1:6379".parse().unwrap();
let connection = connect_with_auth(&addr, None, None)
.await
.expect("Cannot connect to Redis");
let (out_tx, out_rx) = mpsc::unbounded();
let handle = tokio::spawn(async {
match PubsubConnectionInner::new(connection, out_rx).await {
Ok(_) => (),
Err(e) => log::error!("Pub/Sub error: {:?}", e),
}
});
drop(out_tx);
handle.await.expect("Error waiting on the JoinHandle");
}