Repository navigation
fix(abstract/connection-manager): acquire call never finishes - #8330
Conversation
This is basically a hack but pooling library has given us no options. Pool acquire call now resolves even if there is an error. We simply check if its an error instance or not and reject accordingly. Read more here #7884 (comment) _determineConnection method can be used to check if thing returned by pooling library is actaully a connection or error
|
@sushantdhiman, thanks for your PR! By analyzing the history of the files in this pull request, we identified @felixfbecker, @swordfish444 and @Greenkeeper to be potential reviewers. |
|
This is sort of work in progress, I will ask for final review from @sequelize/core when I am happy with this PR and may be add a few tests |
| } | ||
|
|
||
| return resolve(mayBeConnection); | ||
| }); |
There was a problem hiding this comment.
How do you feel about this? Since you're using bluebird, it reduces a little boilerplate and I think this is equivalent.
_determineConnection(mayBeConnection) {
if (mayBeConnection instanceof Error) {
return Promise.resolve(this.pool.destroy(mayBeConnection))
.catch(/Resource not currently part of this pool/, () => {}) // suppress this error
.then(() => { throw mayBeConnection; });
}
return Promise.resolve(mayBeConnection);
}|
For a "hack" this certainly looks cleaner to me :) |
| .then(() => this.dialect.connectionManager.connect(config)) | ||
| .then(connection => this.sequelize.runHooks('afterConnect', connection, config).return(connection)) | ||
| .then(connection => resolve(connection)) | ||
| .catch(e => { resolve(e); }); |
There was a problem hiding this comment.
No need to wrap in new Promise i reckon? Can just return err from catch i think?
| this.poolError = e; | ||
| }); | ||
| }), | ||
| create: () => this._connect(config), |
There was a problem hiding this comment.
I think converting reject(err) to resolve(err) should be here. This hack is only for pool. _connect shouldn't be hacky.
this._connect(config).catch(err => err);| }); | ||
| }), | ||
| create: () => this._connect(config), | ||
| destroy: connection => { |
There was a problem hiding this comment.
suggest changing name connection to mayBeConnection
And, I think "connection destroy" shouldn't be logged if error.
if (mayBeConnection instanceof Error) {
return Promise.resolve(); // avoid "connection destroy" debug log
}
return this._disconnect(mayBeConnection).tap(() => {
debug('connection destroy');
});| ._connect(config.replication.read[nextRead]) | ||
| .tap(connection => { | ||
| connection.queryType = 'read'; | ||
| }); |
There was a problem hiding this comment.
As I mentioned above, I think the hack should be here.
return this
._connect(config.replication.read[nextRead])
.then(connection => { // not to apply to error object, use "then", not "tap"
connection.queryType = 'read';
})
.catch(err => err);| connection.queryType = 'read'; | ||
| }); | ||
| }, | ||
| destroy: connection => this._disconnect(connection), |
There was a problem hiding this comment.
Same as above:
connection => {
if (mayBeConnection instanceof Error) {
return Promise.resolve();
}
return this._disconnect(connection);
}| }) | ||
| .catch(e => { | ||
| this.poolError = e; | ||
| }); |
There was a problem hiding this comment.
Same as above:
return this
._connect(config.replication.write)
.then(connection => { // not to apply to error object, use "then", not "tap"
connection.queryType = 'write';
})
.catch(err => err);| .then(() => this.dialect.connectionManager.connect(config)) | ||
| .then(connection => this.sequelize.runHooks('afterConnect', connection, config).return(connection)); | ||
| .then(connection => this.sequelize.runHooks('afterConnect', connection, config).return(connection)) | ||
| .catch(error => { return error; }); |
There was a problem hiding this comment.
As I mentioned above, I think the hack shouldn't be here.
Delete a call of catch.
| // hacky, _determineConnection could send Error instance for disconnection | ||
| if (connection instanceof Error) { | ||
| return Promise.resolve(); | ||
| } |
There was a problem hiding this comment.
As I mentioned above, I think the hack shouldn't be here.
Delete a statement of if (connection instanceof Error) { ... }.
|
I suggest a little bit changes, but it works nice! |
| idle: 10000, | ||
| acquire: 10000, | ||
| evict: 60000, | ||
| evict: 10000, |
There was a problem hiding this comment.
We need to set evict ~= idle , reason being even if idle time is crossed its the evictor that will remove those idle connections.
More details coopernurse/node-pool#178
There was a problem hiding this comment.
Maybe a good idea to mention this in the API docs. (perhaps just a link to that issue)
There was a problem hiding this comment.
@sushantdhiman by saying evict ~= idle, do you mean evict ≈ idle ? i think it's fine to set evict to some value a little smaller than idle.
There was a problem hiding this comment.
@xianxingg as mentioned in coopernurse/node-pool#178 (comment)
They should be close (or even evict < idle) because only evictor remove idle connection from pool
| } | ||
|
|
||
| config.pool =_.defaults(config.pool || {}, defaultPoolingConfig, { | ||
| this.config.pool =_.defaults(config.pool || {}, defaultPoolingConfig, { |
There was a problem hiding this comment.
Woosh, user supplied pooling.config was never used
There was a problem hiding this comment.
While I agree with the change because it's clearer, doesn't this.config === config? And wouldn't that mean that changing config also changes this.config?
There was a problem hiding this comment.
const config is deep cloned version of this.config, so any changes made to it will not reflect into this.config
No, you are right actually. Its my mistake, may be because I was working late on this last night 😜
| idle: 10000, | ||
| acquire: 10000, | ||
| evict: 60000, | ||
| evict: 10000, |
There was a problem hiding this comment.
Maybe a good idea to mention this in the API docs. (perhaps just a link to that issue)
| .then(() => sequelize.Promise.delay(100)) | ||
| .then(() => { | ||
| // This query will be queued just after the `client.end` is executed and before its callback is called | ||
| expect(sequelize.connectionManager.pool.size).to.equal(0); |
There was a problem hiding this comment.
Good catch - Otherwise we're not really sure what this test is testing! :D
| return this._disconnect(connection).tap(() => { | ||
| debug('connection destroy'); | ||
| }); | ||
| create: () => this._connect(config).catch(err => err ), |
| database: process.env.SEQ_MSSQL_DB || process.env.SEQ_DB || 'sequelize_test', | ||
| username: process.env.SEQ_MSSQL_USER || process.env.SEQ_USER || 'sequelize', | ||
| password: process.env.SEQ_MSSQL_PW || process.env.SEQ_PW || 'nEGkLma26gXVHFUAHJxcmsrK', | ||
| host: process.env.SEQ_MSSQL_HOST || process.env.SEQ_HOST || 'mssql.sequelizejs.com', |
There was a problem hiding this comment.
did we host that? If so, we can shut it down then :)
|
Thanks for putting the time into this fix. I've deployed the update, however under load I'm still seeing the "ResourceRequest timed out" errors even with a max pool size of 50. I then increased pool.acquire to 20000 and thus far the errors appeared to have stopped. |
|
can we listen this error event?when "ResourceRequest timed out" occured ,we just use "process.exit(0)", then pm2 will restart application? |
Pull Request check-list
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?Description of change
This is basically a hack but pooling library has given us no options. Pool acquire call
now resolves even if there is an error. We simply check if its an error instance or not
and reject accordingly.
Read more here #7884 (comment)
_determineConnectionmethod can be used to check if thing returned by pooling library isactaully a connection or error
Closes #7884
Closes #7616
Closes #8014
Closes #8133
Closes #8322
Closes #8185
Closes #8098
Closes #8076
Closes #8014