【问题标题】:Express-session uses Redis. When Redis stops, the request is blockedExpress-session 使用 Redis。当 Redis 停止时,请求被阻塞
【发布时间】:2022-09-27 09:14:12
【问题描述】:

在 NestJS 中,我使用 Express-Session 并使用 Redis 作为 Store。

Redis 地址是负载均衡地址。当我下线Redis服务器时,我发送了一个新请求,发现我一直在加载直到超时。我想在检测到redis错误后返回响应。

const RedisStore = connectRedis(session);
const redisClient = new Redis(\'redis://****:6379\');
redisClient.on(\'error\', (err) => {
  console.error(err);
});

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.use(
    session({
      store: new RedisStore({ client: redisClient }),
      secret: \'asdasdasd\',
      resave: false,
      saveUninitialized: false,
      cookie: { httpOnly: true, maxAge: 1000 * 60 * 10 },
    }),
  );

redis 错误

Error: connect ETIMEDOUT
    at Socket.<anonymous> (/Users/code/nestjs-session/node_modules/ioredis/built/Redis.js:168:41)
    at Object.onceWrapper (events.js:312:28)
    at Socket.emit (events.js:223:5)
    at Socket._onTimeout (net.js:474:8)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7) {
  errorno: \'ETIMEDOUT\',
  code: \'ETIMEDOUT\',
  syscall: \'connect\'
}
  • 同样的问题。你有什么解决办法吗?

标签: express nestjs express-session


【解决方案1】:

尝试使用 redis 遗留模式

const redis = createClient({ socket: { host: "localhost", port: 6379 }, legacyMode: true });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 2022-01-10
    • 2017-10-28
    • 2013-12-22
    • 1970-01-01
    相关资源
    最近更新 更多