This ensures that the Redis connection is always properly closed.

This commit is contained in:
Jakub Miazek 2023-11-09 21:32:01 +01:00
parent 4d79561f8d
commit 32367d783d

View File

@ -18,9 +18,11 @@ logger = AppLogger().get_logger()
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
# Load the redis connection # Load the redis connection
app.state.redis = await get_redis() app.state.redis = await get_redis()
yield try:
# close redis connection and release the resources yield
app.state.redis.close() finally:
# close redis connection and release the resources
app.state.redis.close()
app = FastAPI(title="Stuff And Nonsense API", version="0.6", lifespan=lifespan) app = FastAPI(title="Stuff And Nonsense API", version="0.6", lifespan=lifespan)