【问题标题】:Connection to Redis instance closed every couple of minutes与 Redis 实例的连接每隔几分钟关闭一次
【发布时间】:2019-09-03 13:45:26
【问题描述】:

我正在使用 redis 缓存服务器的 Google Cloud 上运行 node.js。它运行了好几个月,但突然开始抛出连接错误并且偶尔停止响应。

应用在标准环境中运行,并通过 VPC 连接器连接到运行 Redis 实例的虚拟机。我怀疑这是一个网络问题,因为当我从自己的计算机(连接到同一个 Redis 服务器)运行 Node.js 应用程序时,或者当应用程序在 flex 环境中运行并连接到直接子网。但是,我更希望应用程序在标准环境中运行,因为据我所知,这是通过 https 强制流量的唯一方法。

当我通过 Redis-cli 进行监控时,服务器在连接失败时没有收到任何命令。

redis.conf 中的超时设置为 0

Redis 版本:5.0.5

这是 Redis 代码。不过我不认为这是问题所在,几周前它运行时没有问题。

const redis = require('redis')
const redisOptions = {
  host: process.env.REDIS_IP,
  port: process.env.REDIS_PORT,
  password: process.env.REDIS_PASS,
  enable_offline_queue: false,
}
const client = redis.createClient(redisOptions.host, redisOptions.port)

// Log any errors
client.on('error', function(error) {
  console.log('Error:')
  console.log(error)
})

module.exports = client

这些错误会定期显示在 Google App 引擎日志中。当它们发生时,发送到 Redis 的命令不会显示在日志中。

A 2019-08-31T12:42:27.162834Z { Error: Redis connection to 10.128.15.197:6379 failed - read ETIMEDOUT "
A 2019-08-31T12:42:27.162868Z     at TCP.onStreamRead (internal/stream_base_commons.js:111:27) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'read' }

【问题讨论】:

  • 基于documentation,不支持Redis 5。你可以尝试使用 Redis 4 或 Redis 3 吗?
  • 我在计算引擎上运行 Redis,主要是因为它比使用 Google 的 Memorystore 解决方案更便宜。但我也尝试了 Memorystore Redis 4 实例,但它给了我同样的问题。
  • 澄清:我可以每隔几分钟强制关闭连接作为解决方法。这不是一个很好的解决方案,但至少它可以继续以这种方式检索数据。 Google Memorystore 实例似乎也在做同样的事情,定期超时连接。
  • 您是否确保 Redis 允许远程连接。更多细节here

标签: node.js google-app-engine redis google-cloud-platform vpc


【解决方案1】:

我在不同的数据库中多次看到相同的问题。你已经发现了问题。打开的连接数 - 是有限且昂贵的资源。尝试使用以下模式(这只是一个示例):

   // Inside your db module
   function dbCall(userFunc) {
      const client = anyDb.createClient(host, port, ...);
      userFunc(client, () => { client.quit(); /* client.close() or whatever */ }
   }

   // Usage
   dbCall((client, done) => {
       client.doSomethingWithCallback(..., () => {
         // user code
         done();
       });
   });

   dbCall((client, done) => {
       client.doSomePromise(...)
       .finally(done);
   });


【讨论】:

    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2019-02-23
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    相关资源
    最近更新 更多