sequelize - 连接池大小

时间:2017-09-10 04:42:53

标签: express sequelize.js connection-pool

现在我正在阅读关于http://docs.sequelizejs.com/manual/installation/getting-started.html

的文章

并且无法理解下面写的这句话。

  

如果您要从多个流程连接到数据库,那么您必须这样做   每个进程创建一个实例,但每个实例都应该有一个   最大连接池大小" 最大连接池大小除以实例数"。所以,如果你想要一个最大连接   池大小为90,并且您有3个工作进程,每个进程都有   实例的最大连接池大小应为30。

pool: {
  max: 5,
  min: 0,
  idle: 10000
}

连接池的大小是多少?这意味着什么?

我现在正在理解像这样的连接池。 如果" max"是5,3个用户想要到达DB, 3个连接分配给个人用户。

如果6个用户想要获取数据库, 5个连接都分配给个人用户, 由于只有5个连接,第6个用户必须等待。

所以我无法理解

每个实例的最大连接池大小" 最大连接池大小除以实例数"。

有人可以解释一下吗?

2 个答案:

答案 0 :(得分:2)

这是一个非常广泛的问题,但这里有非常广泛的概述

每当我们连接到数据库服务器时,它实际上都会分配一个新进程来完成该请求。正如您所料,这很昂贵。因此,池允许我们在db服务器中保持活动的进程数。 max表示无论您的应用(节点)获得多少请求,它都不会使用数据库服务器打开新进程。

  

如果6个用户想要获取数据库,则5个连接都被分配给   个人用户,因为只有5个连接,第6个   用户必须等待。

在上述情况下,只有5个并行请求可以与db服务器(不是应用服务器)一起运行

这是good link to read

答案 1 :(得分:2)

下面是一个示例,演示了from rest_framework.decorators import action @actions(detail=True) def comments(self, request, pk): # things to do here pool.max选项的作用。

环境:

  • pool.idle
  • "sequelize": "^5.21.3"node
  • v12.16.1PostgreSQL

客户代码:

9.6

db.ts

const sequelize = new Sequelize({ dialect: 'postgres', host: envVars.POSTGRES_HOST, username: envVars.POSTGRES_USER, password: envVars.POSTGRES_PASSWORD, database: envVars.POSTGRES_DB, port: Number.parseInt(envVars.POSTGRES_PORT, 10), define: { freezeTableName: true, timestamps: false, }, pool: { max: 5, min: 0, idle: 10 * 1000, }, }); export { sequelize };

pool_test.ts

通过docker容器运行PostgreSQL服务器:

import { sequelize } from '../../db';

for (let i = 0; i < 100; i++) {
  sequelize.query('select pg_sleep(1);');
}

运行测试代码:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3c9c0fd1bf53        postgres:9.6        "docker-entrypoint.s…"   5 months ago        Up 27 hours         0.0.0.0:5430->5432/tcp   node-sequelize-examples_pg_1

调试日志:

DEBUG=sequelize* npx ts-node ./pool_test.ts

输入docker容器,检查连接过程:

  sequelize:pool pool created with max/min: 5/0, no replication +0ms
  sequelize:connection:pg connection acquired +0ms
  sequelize:connection:pg connection acquired +38ms
  sequelize:connection:pg connection acquired +3ms
  sequelize:connection:pg connection acquired +0ms
  sequelize:connection:pg connection acquired +1ms
  sequelize:connection:pg connection acquired +1ms
  sequelize:pool connection acquired +97ms
  sequelize:sql:pg Executing (default): select pg_sleep(1); +0ms
Executing (default): select pg_sleep(1);
  sequelize:pool connection acquired +2ms
  sequelize:pool connection acquired +0ms
  sequelize:pool connection acquired +0ms
  sequelize:pool connection acquired +0ms
  sequelize:sql:pg Executing (default): select pg_sleep(1); +2ms
Executing (default): select pg_sleep(1);
  sequelize:sql:pg Executing (default): select pg_sleep(1); +2ms
Executing (default): select pg_sleep(1);
  sequelize:sql:pg Executing (default): select pg_sleep(1); +0ms
Executing (default): select pg_sleep(1);
  sequelize:sql:pg Executing (default): select pg_sleep(1); +0ms
Executing (default): select pg_sleep(1);
  sequelize:sql:pg Executed (default): select pg_sleep(1); +1s
  sequelize:pool connection released +1s
  sequelize:pool connection acquired +1ms
  sequelize:sql:pg Executed (default): select pg_sleep(1); +2ms
  sequelize:sql:pg Executed (default): select pg_sleep(1); +0ms
  sequelize:sql:pg Executed (default): select pg_sleep(1); +0ms
  sequelize:sql:pg Executed (default): select pg_sleep(1); +0ms
  sequelize:sql:pg Executing (default): select pg_sleep(1); +1ms
Executing (default): select pg_sleep(1);
  sequelize:pool connection released +1ms
  sequelize:pool connection released +0ms
  sequelize:pool connection released +0ms
  sequelize:pool connection released +0ms
  sequelize:pool connection acquired +1ms
  sequelize:pool connection acquired +0ms
  sequelize:pool connection acquired +0ms
  sequelize:pool connection acquired +0ms

如您所见,有 5(pool.max)个连接过程。

连接完成IDLE 10(pool.idle)秒后。连接过程将被破坏。

root@3c9c0fd1bf53:/# date '+%A %W %Y %X' && ps aux | grep "postgres: testuser"
Thursday 31 2020 09:51:34 AM
postgres 13615  0.0  0.7 289496 16064 ?        Ss   08:29   0:00 postgres: testuser node-sequelize-examples [local] idle
postgres 14335  0.0  0.5 288384 11248 ?        Ss   09:51   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45704) SELECT
postgres 14336  0.0  0.5 288384 11248 ?        Ss   09:51   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45706) SELECT
postgres 14337  0.0  0.5 288384 11252 ?        Ss   09:51   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45708) SELECT
postgres 14338  0.0  0.5 288384 11248 ?        Ss   09:51   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45710) SELECT
postgres 14339  0.0  0.5 288384 11248 ?        Ss   09:51   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45712) SELECT
postgres 86437  0.0  0.6 288804 13704 ?        Ss   00:57   0:00 postgres: testuser node-sequelize-examples [local] idle

客户端调试日志:

root@3c9c0fd1bf53:/# date '+%A %W %Y %X' && ps aux | grep "postgres: testuser"
Thursday 31 2020 09:53:48 AM
postgres 13615  0.0  0.7 289496 16064 ?        Ss   08:29   0:00 postgres: testuser node-sequelize-examples [local] idle
postgres 14352  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45716) idle
postgres 14353  0.0  0.5 288384 11252 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45718) idle
postgres 14354  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45720) SELECT
postgres 14355  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45722) idle
postgres 14356  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45724) SELECT
root     14440  0.0  0.0  12784   972 pts/3    S+   09:53   0:00 grep postgres: testuser
postgres 86437  0.0  0.6 288804 13704 ?        Ss   00:57   0:00 postgres: testuser node-sequelize-examples [local] idle
root@3c9c0fd1bf53:/# date '+%A %W %Y %X' && ps aux | grep "postgres: testuser"
Thursday 31 2020 09:53:49 AM
postgres 13615  0.0  0.7 289496 16064 ?        Ss   08:29   0:00 postgres: testuser node-sequelize-examples [local] idle
postgres 14352  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45716) idle
postgres 14353  0.0  0.5 288384 11252 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45718) idle
postgres 14354  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45720) idle
postgres 14355  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45722) idle
postgres 14356  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45724) idle
postgres 86437  0.0  0.6 288804 13704 ?        Ss   00:57   0:00 postgres: testuser node-sequelize-examples [local] idle
root@3c9c0fd1bf53:/# date '+%A %W %Y %X' && ps aux | grep "postgres: testuser"
Thursday 31 2020 09:53:55 AM
postgres 13615  0.0  0.7 289496 16064 ?        Ss   08:29   0:00 postgres: testuser node-sequelize-examples [local] idle
postgres 14352  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45716) idle
postgres 14353  0.0  0.5 288384 11252 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45718) idle
postgres 14354  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45720) idle
postgres 14355  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45722) idle
postgres 14356  0.0  0.5 288384 11248 ?        Ss   09:53   0:00 postgres: testuser node-sequelize-examples 172.18.0.1(45724) idle
root     14446  0.0  0.0  12784   932 pts/3    S+   09:53   0:00 grep postgres: testuser
postgres 86437  0.0  0.6 288804 13704 ?        Ss   00:57   0:00 postgres: testuser node-sequelize-examples [local] idle
root@3c9c0fd1bf53:/# date '+%A %W %Y %X' && ps aux | grep "postgres: testuser"
Thursday 31 2020 09:53:58 AM
postgres 13615  0.0  0.7 289496 16064 ?        Ss   08:29   0:00 postgres: testuser node-sequelize-examples [local] idle
root     14449  0.0  0.0  12784   940 pts/3    S+   09:53   0:00 grep postgres: testuser
postgres 86437  0.0  0.6 288804 13704 ?        Ss   00:57   0:00 postgres: testuser node-sequelize-examples [local] idle

如果将... sequelize:pool connection released +25ms sequelize:pool connection destroy +10s sequelize:pool connection destroy +0ms sequelize:pool connection destroy +0ms sequelize:pool connection destroy +0ms sequelize:pool connection destroy +1ms 更改为pool.max,请检查连接过程的数量:

10