【问题标题】:Google cloud functions error on redis createClient when using in async mode在异步模式下使用时,redis createClient 上的 Google 云函数错误
【发布时间】:2019-09-09 10:21:05
【问题描述】:

我有一些使用 redis 内存存储的谷歌云功能,它给了我这个 Redis 连接到:6379 failed - 在 TCP 上读取 ECONNRESET。每次部署任何功能时都会出现 onread 错误。以前,我通过创建一个单独的 util 文件并将它们包含在 CF 中来与所有函数共享 createClient() 代码,我认为这就是问题所在。但请注意,除了此错误之外,此 Redis 缓存正在按预期工作。

然后我尝试将 util 代码放入每个使用 redis 客户端创建客户端的谷歌云函数中。但是,当我每次部署任何云功能时,我仍然会从每个云功能中收到此错误。即使在部署不使用redis的功能时。

这是我创建客户端的方法:

const bluebird = require('bluebird');
const redis = bluebird.promisifyAll(require('redis'));

const cache = redis.createClient({ port: REDIS_PORT, host: REDIS_HOST });

cache.on("error",  (err) => {
  console.log("API One - Redis cache error : " + err);
});


const list = async(data) => {
 // Do something with data.
 let cachedData;
 if(cache.connected) { 
   await cache.hgetAsync(key); // Get cached Data.
 }

 // Do something with cached data if cachedData available.

 if(cache.connected) {
   await cache.hsetAsync(key, data); // Set Some Data.
 }

 return data;
}

module.exports = functions.https.onCall(list);

为什么我在每个云功能日志中都看到此错误?

我得到的示例错误日志:

API One - Redis cache error : Error: Redis connection to <Ip Address>:6379 failed - read ECONNRESET
API Two - Redis cache error : Error: Redis connection to <Ip Address>:6379 failed - read ECONNRESET

【问题讨论】:

    标签: javascript redis async-await google-cloud-functions


    【解决方案1】:

    你有没有试过在函数完成前关闭redis连接?

    redis 模块在客户端的生命周期内可能会激活后台回调,在函数终止之前未关闭连接可能会导致云函数终止时连接超时。确保所有异步操作在函数终止之前完成。

    例如: Example

    让我知道这是否适合你。

    【讨论】:

    • 我没有尝试关闭连接,因为我需要保持连接处于活动状态以服务来自 API 的其他请求。每秒可能有大约 5 个请求到达此 API。不确定每次以这个请求率关闭连接是否可以? [我更新了上面的代码,请看看并给我建议。]谢谢。
    • 你好 Chamika,我看到你已经添加了等待,但我认为错误仍然存​​在于在终止函数之前实际上没有关闭连接。看看我找到的以下相关信息。也许它可以帮助你。尝试在某个时候关闭它,看看会发生什么,让我知道,我们可以进一步研究它。 1
    猜你喜欢
    • 2020-03-10
    • 2017-06-11
    • 2018-02-18
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多