Sail.js multiple connections on start

六眼飞鱼酱① 提交于 2019-12-11 03:04:00

问题


I've got an odd problem - on start of my sails app (which is connecting with postgres and deployed on heroku ) there are multiple connections (around 10) to database, and since it's free account, if I then try to launch app on localhost to test some new code I get an error "too many connections for a role". So does anyone know why there are so many connections to database and can I change it, to have only one connection per app?

EDIT: Error creating a connection to Postgresql: error: too many connections for role "xwoellnkvjcupt" Error creating a connection to Postgresql: error: too many connections for role "xwoellnkvjcupt" error: Hook failed to load: orm (error: too many connections for role "xwoellnkv jcupt") error: Error encountered while loading Sails core! error: error: too many connections for role "xwoellnkvjcupt" at Connection.parseE (C:\Studia\szachman2\node_modules\sails-postgresql\node _modules\pg\lib\connection.js:561:11) at Connection.parseMessage (C:\Studia\szachman2\node_modules\sails-postgresq l\node_modules\pg\lib\connection.js:390:17) at null. (C:\Studia\szachman2\node_modules\sails-postgresql\node_ modules\pg\lib\connection.js:98:18) at CleartextStream.EventEmitter.emit (events.js:95:17) at CleartextStream. (_stream_readable.js:746:14) at CleartextStream.EventEmitter.emit (events.js:92:17) at emitReadable_ (_stream_readable.js:408:10) at _stream_readable.js:401:7 at process._tickDomainCallback (node.js:459:13)

this is an error I am getting often when trying to test some new code on localhost.


回答1:


@jantar @sgress454 I just added a troubleshooting message in sails-postgresql to try and make this better. Here's what it says:

-> Maybe your poolSize configuration is set too high? e.g. If your Postgresql database only supports 20 concurrent connections, you should make sure you have your poolSize set as something < 20. The default poolSize is 10.

To override the default poolSize, specify a poolSize property on the relevant Postgresql "connection" config object. If you're using Sails, this is generally located in config/connections.js, or wherever your environment-specific database configuration is set.

-> Do you have multiple Sails instances sharing the same Postgresql database? Each Sails instance may use up to the configured poolSize # of connections. Assuming all of the Sails instances are just copies of one another (a reasonable best practice) we can calculate the actual # of Postgresql connections used (C) by multiplying the configured poolSize (P) by the number of Sails instances (N). If the actual number of connections (C) exceeds the total # of AVAILABLE connections to your Postgresql database (V), then you have problems. If this applies to you, try reducing your poolSize configuration. A reasonable poolSize setting would be V/N.




回答2:


This is due to Sails's auto-migration feature which attempts to keep your models and database synced up. It's not intended to be used in production. You can turn auto-migration off on a single model by adding migrate: safe to the model definition:

module.exports = {
    migrate: 'safe',
    attributes: {...}
}

You can turn auto-migration off for all models by adding a model config, usually in your config/locals.js:

module.exports = {

    model: {
        migrate: 'safe'
    },
    environment: 'production',
    ...other local config...
}



回答3:


A little update for the V1. Your adapter in config/datastore.js should look like this if you want to set a maximum size for the connection pool :

{
  adapter: 'sails-postgresql',
  url: 'yourconnectionurl',
  max: 1 // This is the important part for poolSize, I set 1 because I don't want more than 1 connection ^^
}

If you want to know all infos you can set, look here : https://github.com/sailshq/machinepack-postgresql/blob/176413efeab90dc5099dc60718e8b520942ce3be/machines/create-manager.js , at line 162 :

// Basic:
        'host', 'port', 'database', 'user', 'password', 'ssl',

        // Advanced Client Config:
        'application_name', 'fallback_application_name',

        // General Pool Config:
        'max', 'min', 'refreshIdle', 'idleTimeoutMillis',

        // Advanced Pool Config:
        // These should only be used if you know what you are doing.
        // https://github.com/coopernurse/node-pool#documentation
        'name', 'create', 'destroy', 'reapIntervalMillis', 'returnToHead',
        'priorityRange', 'validate', 'validateAsync', 'log'


来源:https://stackoverflow.com/questions/22963860/sail-js-multiple-connections-on-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!