Hello,
I want to deploy a small sanic app to kubernetes.
It just has to return a database entry.
However it needs to handle a lot of requests concentrated on a short period of time.
I thought of running one worker for each pod.
app.run(host=“0.0.0.0”, port=8000, workers=1, access_log=False)
I use asyncpg connection pools to connect to the postgres database
class pg:
def __init__(self, pg_pool): self.pg_pool = pg_pool async def fetch(self, sql, *args, **kwargs): async with self.pg_pool.acquire() as connection: return await connection.fetch(sql, *args, **kwargs) async def execute(self, sql, *args, **kwargs): async with self.pg_pool.acquire() as connection: return await connection.execute(sql, *args, **kwargs)
@app.listener(‘before_server_start’)
async def register_db(app, loop):pool = await asyncpg.create_pool( user=app.config.get("DB_USER"), password=app.config.get("DB_PASSWORD"), host=app.config.get("DB_HOST"), port=app.config.get("DB_PORT"), database=app.config.get("DB_NAME") ) app.pool = pg(pool)
I believe every pod will create an own connection pool. So if I have 5 pods on one node there will be 5 connection pools. One for each pod. Isn’t there are better way to do this?
Thank you very much for your help.