[英]Intermittend connection failures to databases from Google Cloud Run
我在 Google Cloud Run 中运行多个 NodeJS v16.16 应用程序。 These applications connect to a Google Cloud SQL server and a MongoDB Atlas cluster through a VPC connector with a static IP address that is whitelisted for both the MongoDB Atlas cluster and the SQL server.
当新实例启动时,我们会遇到与这些数据库的间歇性连接失败,就像 IP 地址未列入白名单一样。 对于使用mongoose的连接,我们MongooseServerSelectionError: Server selection timed out after 30000 ms at NativeConnection.Connection.openUri
。 对于使用sequalize的连接,我们得到一个Error connecting MySQL: SequelizeConnectionError: connect ETIMEDOUT
我通过测试、记录和检查配置建立的东西:
https://api.ipify.org?format=json
添加了一行 http 请求并记录了结果。间歇性连接失败是一个问题,因为当这种情况发生时,实例无法启动并且用户会收到 503 错误作为响应。
版本:
我们发现了问题。 在我们开始监听传入请求后,我们正在初始化与数据库的连接(下面的示例)。 我们将其更改为在侦听请求之前连接到数据库,此后没有出现错误。 为什么会发生这种情况尚不清楚,但我认为这与 Google Cloud Run 用于准备实例以处理请求的过程有关。
旧代码:
// start the Express server
app.listen(port, async () => {
database.connect();
console.log("Server started!");
});
新代码:
database.connect().then(() => {
// start the Express server
app.listen(port, async () => {
console.log("Server started!");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.