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

[WIP] Scoped pubsub optimizations #2

Closed

Conversation

DarrenTsung
Copy link

For @coder543 to review.

badboy and others added 30 commits February 4, 2016 18:00
Connection read/write timeout setting
Update README.md to specify latest crate
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
mitsuhiko and others added 27 commits January 12, 2017 10:54
`fail!` already includes the return
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.
@DarrenTsung DarrenTsung deleted the scoped-pubsub-optimizations branch June 13, 2019 20:30
@DarrenTsung DarrenTsung restored the scoped-pubsub-optimizations branch June 18, 2019 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet