Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection configuration provider - without changes to drivers #3497

Merged
merged 14 commits into from Oct 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 13 additions & 31 deletions lib/client.js
Expand Up @@ -259,17 +259,7 @@ Object.assign(Client.prototype, {
// choose the smallest, positive timeout setting and set on poolConfig
poolConfig.acquireTimeoutMillis = Math.min(...timeouts);

const handleConnectionConfigProviderResult = (result) => {
if (result.expirationChecker) {
this.connectionConfigExpirationChecker = result.expirationChecker;
delete result.expirationChecker;
} else {
delete this.connectionConfigExpirationChecker;
}
this.connectionSettings = result;
};

const updatePoolConnectionSettingsFromProvider = () => {
const updatePoolConnectionSettingsFromProvider = async () => {
if (!this.connectionConfigProvider) {
return; // static configuration, nothing to update
}
Expand All @@ -279,34 +269,26 @@ Object.assign(Client.prototype, {
) {
return; // not expired, reuse existing connection
}
const providerResult = this.connectionConfigProvider();
if (!providerResult.then) {
handleConnectionConfigProviderResult(providerResult);
return; // provider is synchronous, no need to return a promise
const providerResult = await this.connectionConfigProvider();
if (providerResult.expirationChecker) {
this.connectionConfigExpirationChecker =
providerResult.expirationChecker;
delete providerResult.expirationChecker;
oranoran marked this conversation as resolved.
Show resolved Hide resolved
} else {
delete this.connectionConfigExpirationChecker;
}
return providerResult.then((connectionSettings) => {
handleConnectionConfigProviderResult(connectionSettings);
});
this.connectionSettings = providerResult;
};

const createPoolConnection = () => {
return this.acquireRawConnection().then(async (connection) => {
return Object.assign(poolConfig, {
create: async () => {
await updatePoolConnectionSettingsFromProvider();
const connection = await this.acquireRawConnection();
connection.__knexUid = uniqueId('__knexUid');

if (poolConfig.afterCreate) {
await promisify(poolConfig.afterCreate)(connection);
}
return connection;
});
};

return Object.assign(poolConfig, {
create: () => {
const promiseOfUpdate = updatePoolConnectionSettingsFromProvider();
if (promiseOfUpdate) {
return promiseOfUpdate.then(createPoolConnection.bind(this));
}
return createPoolConnection.bind(this)();
},

destroy: (connection) => {
Expand Down
1 change: 0 additions & 1 deletion test/tape/index.js
Expand Up @@ -13,7 +13,6 @@ require('./migrate');
require('./pool');
require('./knex');
require('./invalid-db-setup')(knexfile);
require('./connection-config-provider');

Object.keys(knexfile).forEach(function(key) {
var knex = makeKnex(knexfile[key]);
Expand Down