liaoliaots/nestjs-redis

Redis Sentinel Configuration

engmsilva opened this issue · 1 comments

My application works correctly accessing only one instance of Redis, but I am not able to configure my application with Redis Sentinel.

I tried to follow the example of the project's repositories, but it seems that nothing happens.

My module looks like this:

import { Module } from '@nestjs/common';
import { RedisModule } from '@liaoliaots/nestjs-redis';
import { NestRedisService } from './nestjs-redis.service';
import { RedisConfigService } from './nestjs-redis.config.service';

@Module({
  imports: [
    RedisModule.forRootAsync({
      useClass: RedisConfigService,
    }),
  ],
  providers: [NestRedisService],
  exports: [NestRedisService],
})
export class NestjsRedisModule {}

My connection configuration file looks like this:

import { Injectable } from '@nestjs/common';
import {
  RedisOptionsFactory,
  RedisModuleOptions,
} from '@liaoliaots/nestjs-redis';
import { InjectPinoLogger } from 'nestjs-pino/InjectPinoLogger';
import { PinoLogger } from 'nestjs-pino';

export interface ErrnoException extends Error {
  errno?: number;
  code?: string;
  path?: string;
  syscall?: string;
  stack?: string;
}

@Injectable()
export class RedisConfigService implements RedisOptionsFactory {
  constructor(
    @InjectPinoLogger(RedisConfigService.name)
    private readonly logger: PinoLogger,
  ) {}

  async createRedisOptions(): Promise<RedisModuleOptions> {
    const logger = this.logger;
    return {
      config: {
        role: 'master',
        host: process.env.REDIS_HOST,
        port: parseInt(process.env.REDIS_PORT),
        password: process.env.REDIS_PASSWORD,
        maxRetriesPerRequest: 0,
        onClientCreated(client) {
          client.on('error', (error: ErrnoException) => {
            logger.info(
              { description: '[Create Conection][Error]' },
              JSON.stringify(error),
            );
          });
        },
        retryStrategy(times) {
          const delay = Math.min(times * 50, 2000);
          return delay;
        },
      },
    };
  }
}

Are these my Redis and Sentinel servers?

Master - 10.1.2.3 - 6379 - has password
Slave - 10.1.2.4 - 6379 - has password
Slave - 10.1.2.5 - 6379 - has password
Sentinel - 10.1.2.3 - 26379 - has no password
Sentinel - 10.1.2.4 - 26379 - has no password

What would be the best way to adapt the code above to connect with Redis sentinel?

For anyone experiencing the same problem, here's the change I made to the connection file to be able to use redis with sentinel.

import { Injectable } from '@nestjs/common';
import {
  RedisOptionsFactory,
  RedisModuleOptions,
} from '@liaoliaots/nestjs-redis';
import { InjectPinoLogger } from 'nestjs-pino/InjectPinoLogger';
import { PinoLogger } from 'nestjs-pino';

export interface ErrnoException extends Error {
  errno?: number;
  code?: string;
  path?: string;
  syscall?: string;
  stack?: string;
}

@Injectable()
export class RedisConfigService implements RedisOptionsFactory {
  constructor(
    @InjectPinoLogger(RedisConfigService.name)
    private readonly logger: PinoLogger,
  ) {}

  async createRedisOptions(): Promise<RedisModuleOptions> {
    const logger = this.logger;
    return {
      readyLog: true,
      commonOptions: {
        name: process.env.REDIS_SENTINEL_MASTER,
        sentinels: [
          {
            host: process.env.REDIS_SENTINEL_01,
            port: parseInt(process.env.REDIS_SENTINEL_PORT),
          },
          {
            host: process.env.REDIS_SENTINEL_02,
            port: parseInt(process.env.REDIS_SENTINEL_PORT),
          },
          {
            host: process.env.REDIS_SENTINEL_03,
            port: parseInt(process.env.REDIS_SENTINEL_PORT),
          },
        ],
      },
      config: [
        {
          role: 'master',
          password: process.env.REDIS_PASSWORD,
          maxRetriesPerRequest: 0,
          onClientCreated(client) {
            client.on('error', (error: ErrnoException) => {
              logger.info(
                { description: '[Create Conection][Error]' },
                JSON.stringify(error),
              );
            });
          },
          retryStrategy(times) {
            const delay = Math.min(times * 50, 2000);
            return delay;
          },
        },
      ],
    };
  }
}