-
Notifications
You must be signed in to change notification settings - Fork 30
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
dippi
wants to merge
5
commits into
benashford:master
Choose a base branch
from
dippi:pubsub-future-completion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3467a85
Resolve pubsub future only when nothing can happen anymore
dippi 36bf772
Add tests to cover new pubsub completion policy
dippi b51f99f
Add a note on PubsubStream about the assumptions made
dippi 26e1362
Reword expect in tests
dippi 23a85ec
Add comment referencing the issue #50
dippi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(), | ||
|
@@ -205,9 +205,6 @@ impl PubsubConnectionInner { | |
))); | ||
} | ||
} | ||
if self.subscriptions.is_empty() { | ||
return Ok(false); | ||
} | ||
} | ||
b"punsubscribe" => { | ||
match self.psubscriptions.entry(topic) { | ||
|
@@ -221,9 +218,6 @@ impl PubsubConnectionInner { | |
))); | ||
} | ||
} | ||
if self.psubscriptions.is_empty() { | ||
return Ok(false); | ||
} | ||
} | ||
b"message" => match self.subscriptions.get(&topic) { | ||
Some(sender) => { | ||
|
@@ -263,39 +257,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 | ||
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() { | ||
|
@@ -326,11 +313,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 | ||
} | ||
} | ||
} | ||
|
@@ -456,6 +443,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, | ||
} | ||
|
||
|
@@ -540,4 +529,40 @@ mod test { | |
assert_eq!(result[1], "test-message-2".into()); | ||
assert_eq!(result[2], "test-message-3".into()); | ||
} | ||
|
||
#[tokio::test] | ||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to do this test outside the |
||
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("Complete"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?