gpt4 book ai didi

google-cloud-platform - 与同一VPC中的GCP Redis实例的Firebase功能连接不断断开

转载 作者:行者123 更新时间:2023-12-03 06:44:28 25 4
gpt4 key购买 nike

我正在研究多个Firebase云功能(全部托管在同一区域),这些功能使用VPC连接器与同一区域中GCP托管的Redis实例连接。我正在为Redis使用nodejs库的3.0.2版本。在云功能的调试日志中,我看到了频繁的连接重置日志,它们是针对每个云功能触发的,在连接重置的时间轴周围没有固定的模式。并且每次,在错误事件处理程序中捕获的错误都是ECONNRESET。在创建Redis实例时,我提供了一个retry_strategy在5毫秒后重新连接(最多10次此类尝试),以及将retry_unfulfilled_commands设置为true,期望连接重置时任何未完成的命令将自动重试(请参阅代码下面)。

const redisLib = require('redis');
const client = redisLib.createClient(REDIS_PORT, REDIS_HOST, {
enable_offline_queue: true,
retry_unfulfilled_commands: true,
retry_strategy: function(options) {
if (options.error && options.error.code === "ECONNREFUSED") {
// End reconnecting on a specific error and flush all commands with
// a individual error
return new Error("The server refused the connection");
}

if (options.attempt > REDIS_CONNECTION_RETRY_ATTEMPTS) {
// End reconnecting with built in error
console.log('Connection retry count exceeded 10');
return undefined;
}

// reconnect after 5 ms
console.log('Retrying connection after 5 ms');
return 5;
},
});

client.on('connect', () => {
console.log('Redis instance connected');
});

client.on('error', (err) => {
console.error(`Error connecting to Redis instance - ${err}`);
});

exports.getUserDataForId = (userId) => {
console.log('getUserDataForId invoked');
return new Promise((resolve, reject) => {
if(!client.connected) {
console.log('Redis instance not yet connected');
}

client.get(userId, (err, reply) => {
if(err) {
console.error(JSON.stringify(err));
reject(err);
} else {
resolve(reply);
}
});
});
}

// more such exports for different operations
以下是我面临的问题。
  • 为什么间歇性重置连接?
  • 我看到了日志,即使正在执行云功能,与Redis服务器的连接也会丢失,从而导致命令失败。
  • 将retry_unfulfilled_commands设置为true,我希望它能够处理上面第2点中提到的方案,但是根据调试日志,云功能在这种情况下会超时。这就是我在这种情况下在日志中观察到的。
  • getUserDataForId invoked
    Retrying connection after 5 ms
    Redis instance connected
    Function execution took 60002 ms, finished with status: 'timeout' --> coming from wrapper cloud function
  • 我是否应该尝试在每个这样的Redis操作期间创建连接,而不是在全局级别上使用Redis连接实例?它可能存在一些性能问题以及与并发Redis连接数有关的问题(因为我有多个云功能,并且所有这些功能都将为每次同时调用创建Redis连接),对吗?

  • 因此,由于我在开发过程中会遇到所有这些问题,因此如何最好地处理它,因此不确定是代码相关问题还是某些基础架构配置相关问题。

    最佳答案

    此行为可能是由后台 Activity 引起的。

    "Background activity is anything that happens after your function hasterminated"


    当后台 Activity 干扰Cloud Functions中的后续调用时,可能会发生意外行为和难以诊断的错误。功能终止后访问网络通常会导致 “ECONNRESET” 错误。
    要解决此问题,请通过在日志中说调用完成的行之后搜索日志以查找条目,以确保没有后台 Activity 。有时可以将后台 Activity 深埋在代码中,特别是当存在诸如回调或计时器之类的异步操作时。在终止函数之前,请检查代码以确保所有异步操作完成。
    Source

    关于google-cloud-platform - 与同一VPC中的GCP Redis实例的Firebase功能连接不断断开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63762701/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com