ConnectionTimeoutError: Connection timeout

未关闭
#2,045 2 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
javascript, next.js, node.js, redis
领域
api, backend, database

调研方向

从 /api/cache/client.js 开始,重点查看 redis.createClient({ url }) 和 client.connect(),然后将连接行为与 node_modules/@node-redis/client/dist/lib/client/socket.js 中的超时堆栈跟踪进行比较。重现 API 请求并确定 Redis 连接超时的原因;完成的标准是缓存仍能报告命中或未命中,且不再反复出现超时和重新连接消息。

由索引模型根据 Issue 内容生成。

描述

Bug stale

I am using this module to cache queries on a nextjs/node application.

Within nextjs I have a simple file within the /api folder (/api/cache/client.js) that contains the following code:

const redis = require("redis");

const REDIS_CLIENT_URI = process.env.REDIS_URL || "redis://127.0.0.1:6379";
let client = null;

const onRedisError = (err) => {
  console.error(err);
};
const onRedisConnect = () => {
  console.log("Redis connected");
};
const onRedisReconnecting = () => {
  console.log("Redis reconnecting");
};
const onRedisReady = () => {
  console.log("Redis ready!");
};

async function redisClient() {
  try {
    if (client === null) {
      client = redis.createClient({
        url: REDIS_CLIENT_URI,
      });
      await client.connect();
    }
    client.on("error", onRedisError);
    client.on("connect", onRedisConnect);
    client.on("reconnecting", onRedisReconnecting);
    client.on("ready", onRedisReady);
    return client;
  } catch (e) {
    console.log("not connected");
    console.log(e.message);
    return null;
  }
}

async function redisGet(key, func) {
  try {
    const client = await redisClient();
    if (client === null) {
      throw new Error("Redis is not connected");
    }
    const data = await client.get(key);
    if (data === null) {
      console.log("miss");
      const query = await func();
      await redisSet(key, JSON.stringify(query));
      return query;
    } else {
      console.log("hit");
      return JSON.parse(data);
    }
  } catch (e) {
    console.log(e.message);
    return await func();
  }
}

async function redisSet(key, data) {
  try {
    const client = await redisClient();
    if (client === null) {
      throw new Error("Redis is not connected");
    }
    await client.set(key, data, { EX: 86400 * 31 });
  } catch (e) {
    console.log(e.message);
  }
}
module.exports.redisGet = redisGet;

I then require this file in any /api files where I need to use redis to cache the results of any asycnhronous function that makes a call to an external API e.g.

const { redisGet } = require("../cache/client");

export default async function handler(req, res) {
  try {
    const data = JSON.parse(req.body);
    const query = await redisGet(
      `${data.customer_id}:${data.fromDate}/${data.toDate}`,
      async () => {
        try {
          const res = await fetch(
            `https://jsonplaceholder.typicode.com/comments`
          );
          const data = await res.json();
          return data;
        } catch (e) {
          console.log(e);
          throw e;
        }
      }
    );
    console.log(query);
    res.status(200).send({ query });
  } catch (e) {
    console.log(e);
    console.log(e.message);
    res.status(500).json(e.message);
  }
}
}

This works fine because I am able to get the commands in my output such as hit and miss whenever I call any of the API endpoints where I am using redisGet, however, my server also seems to constantly be giving me a connection timeout error followed by Redis reconnecting:

    at TLSSocket.<anonymous> (/Users/kamrankhan/Sites/2020/visifii/visifii/node_modules/@node-redis/client/dist/lib/client/socket.js:169:124)
    at Object.onceWrapper (node:events:509:28)
    at TLSSocket.emit (node:events:390:28)
    at TLSSocket.emit (node:domain:475:12)
    at TLSSocket.Socket._onTimeout (node:net:486:8)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7)
Redis reconnecting

I don't know what I'm doing wrong here. I have also set the timeout config time to 10000 and also set the maxmemory-policy config to allkeys-lru. Can anyone nudge me in the right direction? Am I missing something? Any help would be appreciated!

Environment:

  • Node.js Version:
  • v16.10.0
  • Redis Server Version:
  • Redis server v=6.2.6 sha=00000000:0 malloc=libc bits=64 build=c6f3693d1aced7d9
  • Node Redis Version:
  • [email protected]
  • Platform:
  • Mac OS Mojave 10.14.6
主要语言
TypeScript
星标
17.6k
派生
2k
平均合并
1 天 22 小时
30 天内合并 PR
62

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

redis/node-redis 的其他 Issue

查看 redis/node-redis 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。