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

Disconnect clients from pool safely #753

Closed
wants to merge 4 commits into from

Conversation

pfreixes
Copy link
Contributor

This PR tries to solve the issues raised by #732 regarding
the danger of disconnect clients from the ConnectionPool.disconnect
method executed by a Thread different that those ones that are in charge
of the connections.

Instead of call the Connection.disconnect method it uses the syscall
shutdown to leave the socket unusable. Once the connection tries to use
the socket, even when it is already blocked such us the PubSub pattern, it
gets a socket.error exception that will be cactched by the
Connection class to then raise an ConnectionError and disconnect the
socket in a clean and safe way.

The Client.execute_command function catches the ConnectionError exception
and tries to connect again and run the command that raised the error.

Worth mentioning that in the case of the Sentinel environment, if the disconnect was because of a
change of the Redis pool servers - perhaps the master went
down and a slave was promoted, the next command will be executed using a new connection that will take into account these changes.

This PR tries to solve the issues raised by redis#732 regarding
the danger of disconnect clients from the `ConnectionPool.disconnect`
method executed by a Thread different that those ones that are in charge
of the connections.

Instead of call the `Connection.disconnect` method it uses the syscall
`shutdown` to leave the socket unusable. Once the connection tries to use
the socket, even when it is already blocked such us the `PubSub` pattern, it
gets a `socket.error` exception that will be cactched by the
`Connection` class to then raise an `ConnectionError` and disconnect the
socket in a clean and safe way.

The `Client.execute_command` function catches the `ConnectionError` exception
and tries to connect again and run the command that raised the error.
Worth mentioning that in the case of the `Sentinel` environment, if some
changes regarding the Redis pool of servers - perhaps the mater went
down and a slave was promoted - the next command will be executed using
an other server.
@pfreixes pfreixes closed this Jun 21, 2016
@pfreixes pfreixes deleted the pool_disconnect_thread_safe branch June 21, 2016 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant