连接池该设多大?池太小会怎样?worker 数乘池大小超过数据库连接上限又会怎样?这篇用真实的 gunicorn 与 PostgreSQL 把三个问题逐个量出来(池的机制本身见第 91、92 篇)。
pool_size 请求/秒 平均 ms p95 ms 理想上限(pool/0.02s)
1 41 732.5 791 50
2 82 370.0 405 100
5 214 141.8 159 250
10 428 70.7 81 500
20 829 36.5 48 1000上一篇:Worker Timeout;下一篇:缓存命中率。
300 个请求:成功 16,失败 284({'TimeoutError': 284}) 400 个请求:成功 130,失败 270;失败原因:{'connection failed: connection to server at "127.0.0.1", port 9478 fail': 270};此刻 PG 上的连接数 psql: error: connection to server at "127.0.0.1", port 9478 failed: FATAL: sorry, too many clients alreadyPostgreSQL 是自己起的实例,max_connections 设成 20 来演示。