forked from redis-rs/redis-rs
-
Notifications
You must be signed in to change notification settings - Fork 2
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
[WIP] Scoped pubsub optimizations #2
Closed
DarrenTsung
wants to merge
83
commits into
jwilm:master
from
DarrenTsung:scoped-pubsub-optimizations
Closed
[WIP] Scoped pubsub optimizations #2
DarrenTsung
wants to merge
83
commits into
jwilm:master
from
DarrenTsung:scoped-pubsub-optimizations
Conversation
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
…ad_timeout to PubSub
Connection read/write timeout setting
Remove unused import
Update README.md to specify latest crate
Faster encoding
Update rust-url to 1.1 and add test for parse_redis_url
Implement ToRedisArgs for &String to make it work without slicing
Update changelog for 0.5.4 and 0.6.0
Don't hide docs of types
`fail!` already includes the return
Rebase of redis-rs#116: BTreeMap/Set conversions
Implements LSET command.
Fixed spelling
This was causing troubles with `cargo clippy` as they seem to replace the RUSTC env variable (see https://github.com/Manishearth/rust-clippy/issues/1517)
Don't crash if the rustc version can't be determined
Originally, pubsub support as only available only by creating a client and then asking for a pubsub API. The original `PubSub` type owned a connection. This neatly avoided the issue that only certain commands are available once calling subscribe or psubscribe; however, it's very unfriendly to connection poolers such as r2d2 This commit makes it so that a borrowed connection may be used for PubSub. Available commands are limited while in pubsub mode by borring the connection mutably into a type that only exposes the allowed commands. This API may be accessed by calling `as_pubsub` on a `Connection`. To go back to being able to use *any* command, this wrapper type may be dropped, and the connection should become usable again for other purposes. Additionally, convenience methods are added to `Connection`: `subscribe` and `psubscribe`. They accept a `ToRedisArgs` and a closure which will be called every time a message is received. The subscription status can be managed via the return value of the closure which is either `ControlFlow::Continue` or `ControlFlow::Break`. This is a breaking change; the original PubSub type was removed in favor of this more flexible API and to avoid code bloat by having multiple implementations or additional abstractions to allow an implementation to be shared. Resolves redis-rs#135.
It may not be immediately obvious from the method signature that `PubSubCommands::subscribe` and `PubSubCommands::unsubscribe` block until a message is seen and `ControlFlow::Break` is returned.
When a subscribe or unsubscribe command is sent to the server, the connection enters a state where the only valid commands are pubsub commands. To return to the normal state, all subscriptions must be dropped. This is achieved by sending an unsubscribe and punsubscribe command to the server. The server then replies with all of the subscriptions that were dropped. This patch ensures that all messages are received from the server after sending those commands and that the connection object is again usable for non-pubsub activities. The PubSub API was changed to reflect the fallible nature of this operation. In the event of an error during unsubscribing, the connection is simply lost. In practical terms, this means that entering the PubSub state consumes the connection, and the connection is retrieved through calling `into_inner`. This will likely cause issues with connection pooling libraries since they don't provide ownership.
This implementation adds a flag to the `Connection` struct to indicate whether the connection is stuck in the PubSub state after PubSub is dropped. Subsequent commands check this flag and reattempt the unsubscribe routine to put the connection back into a known good state. The main drawback to this implementation is the additional state inside `Connection`.
The only code change here is PubSub::new being made private. This forces users to go through the as_pubsub method on the Connection which was the intended usage anyway.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
For @coder543 to review.