Grokzen/redis-py-cluster

Is need to connection pool disconect?

hjlee9182 opened this issue · 2 comments

I use redis connection pool.
When app server shutdown( ex. reboot or shutdown and start ..), need to pool disconnect ?
underline is my example.
Is pool disconnect need? or not?

fastapi

redis.py

from rediscluster import ClusterConnectionPool, RedisCluster

pool = ClusterConnectionPool(startup_nodes = [ {~~~~}], decode_responses=True )

def redis_get(key):
    rd = RedisCluster(connection_pool=pool)
    return rd.get(key)

main.py

app = FastAPI()

@app.on_event("shutdown")
def shutdown_event():
    pool.disconnect()

@hjlee9182 I do not get your question really, are you asking how to access the pool and to make a proper shutdown? or are you asking that if you really need to shutdown a pool?

So if the question is regarding that if you need to or not, it depends on how your FastAPI process and code works. If you are going to reuse the same pool object multiple times instead of just making a new instance of it then yes you Must disconnect or you migt even want to run pool.reset() if you are going to reuse it as that does some other cleanup resetting it back to 0 to be able to be used again at a later time. I do however recommend that you create a new pool instance if possible as that ensures that you get back to a good and empty state properly.
Also note that this does depends on if your process shutsdown or not. If your process manager just restarts your server then a new pool will be created each time and any old objects and old sockets/connections will automatically be reaped and deallocated and new ones will be created when your next process starts up. If you are within the same process then you must deallocate and reap these things yourself like you do in your example above, if that is how a shutdown event is processed within FastAPI, i dunno about that tho.