Grokzen / redis-py-cluster

Python cluster client for the official redis cluster. Redis 3.0+.
https://redis-py-cluster.readthedocs.io/
MIT License
1.1k stars 316 forks source link

Stop leaking connections when pool is full #473

Open FranGM opened 2 years ago

FranGM commented 2 years ago

Fixes https://github.com/Grokzen/redis-py-cluster/issues/458

When our connection pool is full (there are no available connections) we can run into a case where we leak perfectly useable connections. This essentially reduces the size of our connection pool every time it happens, as it can slowly leak away our unused connections until we don't have enough connections available to cope with our workload.

This is particularly problematic when preparing connections for a pipeline, since we can potentially request (and leak) a large number of connections in one go.

This change makes sure that before raising an exception if there are no available connections we return back all connections we've checked to the pool. In cases where we can't guarantee the connections we'd return to the pool are in a safe state we just add back None objects to the pool so at least the effective size of the connection pool remains unchanged.