Skip to content

Commit 5c965d3

Browse files
committed
Override Knex pooling behavior
Knex uses tarn to handle connection pooling. Tarn doesn't work well for connection-less querying. By default it wants to keep connections around, which prevents Lambda functions from returning as the event loop still has things in it. This change creates a dummy pool for Knex that returns query parameters with an incrementing Knex connection ID, allowing for any number of simultaneous queries without any overhead.
1 parent 78aed1b commit 5c965d3

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

index.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,33 @@ class Client_AuroraDataMySQL extends Client_MySQL { // eslint-disable-line camel
9090
return new RDSDataService(config);
9191
}
9292

93-
acquireRawConnection () {
94-
return {
95-
client: this.driver,
96-
parameters: {
97-
// common parameters for Data API requests
98-
database: this.config.connection.database,
99-
resourceArn: this.config.connection.resourceArn,
100-
secretArn: this.config.connection.secretArn
101-
}
102-
};
103-
}
93+
initializePool() {
94+
/* istanbul ignore if */
95+
if (this.pool) {
96+
this.logger.warn('The pool has already been initialized');
97+
return;
98+
}
10499

105-
destroyRawConnection (connection) {}
100+
this.knexUid = 0;
106101

107-
validateConnection (connection) {
108-
return true;
102+
// common parameters for Data API requests
103+
const parameters = {
104+
database: this.config.connection.database,
105+
resourceArn: this.config.connection.resourceArn,
106+
secretArn: this.config.connection.secretArn
107+
};
108+
109+
this.pool = {
110+
acquire: () => ({
111+
promise: Promise.resolve({
112+
client: this.driver,
113+
parameters,
114+
__knexUid: this.knexUid++
115+
})
116+
}),
117+
release: () => true,
118+
destroy: () => true
119+
};
109120
}
110121

111122
prepBindings (bindings) {

0 commit comments

Comments
 (0)