Google Cloud Functions 是否有拆解挂钩?
Is there a teardown hook for Google Cloud Functions?
我想在我的云函数的每个实例中共享对 Cloud Spanner 实例和数据库的引用,以便云函数的每次调用都不会创建与 Spanner 的新连接。参见 Optimizing Networking。
在这个 Node.js 8 beta 实现中,是否有拆卸功能可以让我关闭与数据库的连接?
myFunction/index.js
const Spanner = require("@google-cloud/spanner");
const spanner = Spanner();
/**
* Maintain a persistent connection instead of creating a new
* connection upon every function invocation
*/
const instance = spanner.instance("test-instance");
const database = instance.database("example-db");
const myTable = database.table("my-table");
/**
* Creates a Spanner record from data in a pubsub message
*
* @param {!Object} event Event payload.
*/
exports.insertRowToMyTable = (event) => {
const pubsubMessage = event.data;
const decodedMessage = JSON.parse(Buffer.from(pubsubMessage, "base64").toString())
console.log("Decoded Message: ", decodedMessage);
const tableRow = {
fieldOne: decodedMessage.fieldOne,
fieldTwo: decodedMessage.fieldTwo,
fieldThree: decodedMessage.fieldThree,
};
console.log("Inserting: ", tableRow);
myTable.insert([tableRow])
.then(() => {
console.log("Inserted: ", tableRow);
})
.catch(err => {
console.error(err);
})
};
/**
* Is there a teardown function where I can place this such that
* the connection pool is closed when the cloud function instance
* shuts down?
*
* // Close the database when finished.
* database.close();
*/
一个服务器实例 运行 Cloud Functions 中的代码将在不再需要时停止 运行。当发生这种情况时,没有办法知道或拦截通知。您不需要关闭任何连接,因为当实例终止时它们都将被强制关闭。把它想象成简单地关闭电源开关。
使用 Cloud Functions 的 Cloud Spanner 客户端提示:
- 将 grpc 通道和最大会话数限制为 1
- 重用客户端库和会话以节省创建会话的设置时间
参考文献:
- https://cloud.google.com/functions/docs/sql#connection_reuse
- https://cloud.google.com/functions/docs/bestpractices/tips#use_global_variables_to_reuse_objects_in_future_invocations
- https://cloud.google.com/functions/docs/concepts/exec#function_instance_lifespan
用于监控 Cloud Spanner 会话使用情况:
我想在我的云函数的每个实例中共享对 Cloud Spanner 实例和数据库的引用,以便云函数的每次调用都不会创建与 Spanner 的新连接。参见 Optimizing Networking。
在这个 Node.js 8 beta 实现中,是否有拆卸功能可以让我关闭与数据库的连接?
myFunction/index.js
const Spanner = require("@google-cloud/spanner");
const spanner = Spanner();
/**
* Maintain a persistent connection instead of creating a new
* connection upon every function invocation
*/
const instance = spanner.instance("test-instance");
const database = instance.database("example-db");
const myTable = database.table("my-table");
/**
* Creates a Spanner record from data in a pubsub message
*
* @param {!Object} event Event payload.
*/
exports.insertRowToMyTable = (event) => {
const pubsubMessage = event.data;
const decodedMessage = JSON.parse(Buffer.from(pubsubMessage, "base64").toString())
console.log("Decoded Message: ", decodedMessage);
const tableRow = {
fieldOne: decodedMessage.fieldOne,
fieldTwo: decodedMessage.fieldTwo,
fieldThree: decodedMessage.fieldThree,
};
console.log("Inserting: ", tableRow);
myTable.insert([tableRow])
.then(() => {
console.log("Inserted: ", tableRow);
})
.catch(err => {
console.error(err);
})
};
/**
* Is there a teardown function where I can place this such that
* the connection pool is closed when the cloud function instance
* shuts down?
*
* // Close the database when finished.
* database.close();
*/
一个服务器实例 运行 Cloud Functions 中的代码将在不再需要时停止 运行。当发生这种情况时,没有办法知道或拦截通知。您不需要关闭任何连接,因为当实例终止时它们都将被强制关闭。把它想象成简单地关闭电源开关。
使用 Cloud Functions 的 Cloud Spanner 客户端提示:
- 将 grpc 通道和最大会话数限制为 1
- 重用客户端库和会话以节省创建会话的设置时间
参考文献:
- https://cloud.google.com/functions/docs/sql#connection_reuse
- https://cloud.google.com/functions/docs/bestpractices/tips#use_global_variables_to_reuse_objects_in_future_invocations
- https://cloud.google.com/functions/docs/concepts/exec#function_instance_lifespan
用于监控 Cloud Spanner 会话使用情况: