不要怂,就是干,撸起袖子干!

You need to sign in or sign up before continuing.
Commit ceb0de26 by Andy Edwards Committed by GitHub

refactor(dialects/abstract): asyncify methods (#12128)

1 parent 92cbae90
Showing with 69 additions and 75 deletions
......@@ -91,15 +91,15 @@ class ConnectionManager {
* @private
* @returns {Promise}
*/
_onProcessExit() {
async _onProcessExit() {
if (!this.pool) {
return Promise.resolve();
return;
}
return this.pool.drain().then(() => {
debug('connection drain due to process exit');
return this.pool.destroyAllNow();
});
await this.pool.drain();
debug('connection drain due to process exit');
return await this.pool.destroyAllNow();
}
/**
......@@ -107,13 +107,13 @@ class ConnectionManager {
*
* @returns {Promise}
*/
close() {
async close() {
// Mark close of pool
this.getConnection = function getConnection() {
return Promise.reject(new Error('ConnectionManager.getConnection was called after the connection manager was closed!'));
this.getConnection = async function getConnection() {
throw new Error('ConnectionManager.getConnection was called after the connection manager was closed!');
};
return this._onProcessExit();
return await this._onProcessExit();
}
/**
......@@ -127,11 +127,10 @@ class ConnectionManager {
this.pool = new Pool({
name: 'sequelize',
create: () => this._connect(config),
destroy: connection => {
return this._disconnect(connection)
.then(result => {
debug('connection destroy');return result;
});
destroy: async connection => {
const result = await this._disconnect(connection);
debug('connection destroy');
return result;
},
validate: config.pool.validate,
max: config.pool.max,
......@@ -180,27 +179,26 @@ class ConnectionManager {
this.pool[connection.queryType].destroy(connection);
debug('connection destroy');
},
destroyAllNow: () => {
return Promise.all([
destroyAllNow: async () => {
await Promise.all([
this.pool.read.destroyAllNow(),
this.pool.write.destroyAllNow()
]).then(() => { debug('all connections destroyed'); });
},
drain: () => {
return Promise.all([
this.pool.write.drain(),
this.pool.read.drain()
]);
debug('all connections destroyed');
},
drain: async () => Promise.all([
this.pool.write.drain(),
this.pool.read.drain()
]),
read: new Pool({
name: 'sequelize:read',
create: () => {
create: async () => {
// round robin config
const nextRead = reads++ % config.replication.read.length;
return this._connect(config.replication.read[nextRead]).then(connection => {
connection.queryType = 'read';
return connection;
});
const connection = await this._connect(config.replication.read[nextRead]);
connection.queryType = 'read';
return connection;
},
destroy: connection => this._disconnect(connection),
validate: config.pool.validate,
......@@ -213,11 +211,10 @@ class ConnectionManager {
}),
write: new Pool({
name: 'sequelize:write',
create: () => {
return this._connect(config.replication.write).then(connection => {
connection.queryType = 'write';
return connection;
});
create: async () => {
const connection = await this._connect(config.replication.write);
connection.queryType = 'write';
return connection;
},
destroy: connection => this._disconnect(connection),
validate: config.pool.validate,
......@@ -243,16 +240,14 @@ class ConnectionManager {
*
* @returns {Promise<Connection>}
*/
getConnection(options) {
async getConnection(options) {
options = options || {};
let promise;
if (this.sequelize.options.databaseVersion === 0) {
if (this.versionPromise) {
promise = this.versionPromise;
} else {
promise = this.versionPromise = this._connect(this.config.replication.write || this.config)
.then(connection => {
if (!this.versionPromise) {
this.versionPromise = (async () => {
try {
const connection = await this._connect(this.config.replication.write || this.config);
const _options = {};
_options.transaction = { connection }; // Cheat .query to use our private connection
......@@ -262,36 +257,36 @@ class ConnectionManager {
//connection might have set databaseVersion value at initialization,
//avoiding a useless round trip
if (this.sequelize.options.databaseVersion === 0) {
return this.sequelize.databaseVersion(_options).then(version => {
const parsedVersion = _.get(semver.coerce(version), 'version') || version;
this.sequelize.options.databaseVersion = semver.valid(parsedVersion)
? parsedVersion
: this.defaultVersion;
this.versionPromise = null;
return this._disconnect(connection);
});
const version = await this.sequelize.databaseVersion(_options);
const parsedVersion = _.get(semver.coerce(version), 'version') || version;
this.sequelize.options.databaseVersion = semver.valid(parsedVersion)
? parsedVersion
: this.defaultVersion;
}
this.versionPromise = null;
return this._disconnect(connection);
}).catch(err => {
return await this._disconnect(connection);
} catch (err) {
this.versionPromise = null;
throw err;
});
}
})();
}
} else {
promise = Promise.resolve();
await this.versionPromise;
}
return promise.then(() => {
return this.pool.acquire(options.type, options.useMaster)
.catch(error => {
if (error instanceof TimeoutError) throw new errors.ConnectionAcquireTimeoutError(error);
throw error;
});
}).then(result => {
debug('connection acquired');return result;
});
let result;
try {
result = await this.pool.acquire(options.type, options.useMaster);
} catch (error) {
if (error instanceof TimeoutError) throw new errors.ConnectionAcquireTimeoutError(error);
throw error;
}
debug('connection acquired');
return result;
}
/**
......@@ -301,11 +296,9 @@ class ConnectionManager {
*
* @returns {Promise}
*/
releaseConnection(connection) {
return Promise.resolve().then(() => {
this.pool.release(connection);
debug('connection released');
});
async releaseConnection(connection) {
this.pool.release(connection);
debug('connection released');
}
/**
......@@ -315,10 +308,11 @@ class ConnectionManager {
* @private
* @returns {Promise<Connection>}
*/
_connect(config) {
return this.sequelize.runHooks('beforeConnect', config)
.then(() => this.dialect.connectionManager.connect(config))
.then(connection => this.sequelize.runHooks('afterConnect', connection, config).then(() => connection));
async _connect(config) {
await this.sequelize.runHooks('beforeConnect', config);
const connection = await this.dialect.connectionManager.connect(config);
await this.sequelize.runHooks('afterConnect', connection, config);
return connection;
}
/**
......@@ -328,10 +322,10 @@ class ConnectionManager {
* @private
* @returns {Promise}
*/
_disconnect(connection) {
return this.sequelize.runHooks('beforeDisconnect', connection)
.then(() => this.dialect.connectionManager.disconnect(connection))
.then(() => this.sequelize.runHooks('afterDisconnect', connection));
async _disconnect(connection) {
await this.sequelize.runHooks('beforeDisconnect', connection);
await this.dialect.connectionManager.disconnect(connection);
return this.sequelize.runHooks('afterDisconnect', connection);
}
/**
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!