Sfoglia il codice sorgente

correct pg connection pool size to avoid exceeding limit (#2679)

Samuel Colvin 8 anni fa
parent
commit
0951875353
1 ha cambiato i file con 8 aggiunte e 1 eliminazioni
  1. 8 1
      frameworks/Python/aiohttp/app/main.py

+ 8 - 1
frameworks/Python/aiohttp/app/main.py

@@ -1,4 +1,5 @@
 import os
+import multiprocessing
 from pathlib import Path
 
 import aiohttp_jinja2
@@ -43,7 +44,13 @@ def pg_dsn() -> str:
 
 async def startup(app: web.Application):
     dsn = pg_dsn()
-    min_size, max_size = 50, 100
+    # number of gunicorn workers = multiprocessing.cpu_count() as per gunicorn_conf.py
+    # max_connections = 2000 as per toolset/setup/linux/databases/postgresql/postgresql.conf:64
+    # give 10% leeway
+    max_size = min(1800 / multiprocessing.cpu_count(), 160)
+    max_size = max(int(max_size), 1)
+    min_size = max(int(max_size / 2), 1)
+    print(f'connection pool: min size: {min_size}, max size: {max_size}, orm: {CONNECTION_ORM}')
     if CONNECTION_ORM:
         app['pg'] = await aiopg.sa.create_engine(dsn=dsn, minsize=min_size, maxsize=max_size, loop=app.loop)
     else: