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

GCF MySQL samples: use pools instead of connections #735

Merged
merged 1 commit into from
Sep 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions functions/sql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ if (process.env.NODE_ENV === 'production') {
mysqlConfig.socketPath = `/cloudsql/${connectionName}`;
}

const mysqlPool = mysql.createPool(mysqlConfig);
// Connection pools reuse connections between invocations,
// and handle dropped or expired connections automatically.
let mysqlPool;

exports.mysqlDemo = (req, res) => {
// Initialize the pool lazily, in case SQL access isn't needed for this
// GCF instance. Doing so minimizes the number of active SQL connections,
// which helps keep your GCF instances under SQL connection limits.
if (!mysqlPool) {
mysqlPool = mysql.createPool(mysqlConfig);
}

mysqlPool.query('SELECT NOW() AS now', (err, results) => {
if (err) {
console.error(err);
Expand All @@ -61,6 +70,9 @@ exports.mysqlDemo = (req, res) => {
res.send(JSON.stringify(results));
}
});

// Close any SQL resources that were declared inside this function.
// Keep any declared in global scope (e.g. mysqlPool) for later reuse.
};
// [END functions_sql_mysql]

Expand All @@ -76,9 +88,18 @@ if (process.env.NODE_ENV === 'production') {
pgConfig.socketPath = `/cloudsql/${connectionName}`;
}

const pgPool = new pg.Pool(pgConfig);
// Connection pools reuse connections between invocations,
// and handle dropped or expired connections automatically.
let pgPool;

exports.postgresDemo = (req, res) => {
// Initialize the pool lazily, in case SQL access isn't needed for this
// GCF instance. Doing so minimizes the number of active SQL connections,
// which helps keep your GCF instances under SQL connection limits.
if (!pgPool) {
pgPool = new pg.Pool(pgConfig);
}

pgPool.query('SELECT NOW() as now', (err, results) => {
if (err) {
console.error(err);
Expand All @@ -87,5 +108,8 @@ exports.postgresDemo = (req, res) => {
res.send(JSON.stringify(results));
}
});

// Close any SQL resources that were declared inside this function.
// Keep any declared in global scope (e.g. mysqlPool) for later reuse.
};
// [END functions_sql_postgres]