Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Refactor redis module to dynamically build the connection #22

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Service designed for the management and storage of messaging for the Message Pic
| `FCM_SERVICE_BASE_URL` | The base URL for the push notification service. | _Not set by default_ |
| `MONGODB_URI` | The MongoDB URI for connecting to the database. | `mongodb://user:password@localhost:27017/MessagePickupRepository` |
| `REDIS_TYPE` | Allows set redis type works `single` or `cluster` | `single` |
| `REDIS_NODES` | A comma-separated list of Redis nodes' `host:port` for cluster mode. Only required if `REDIS_TYPE` is set to `cluster`. Ignored in single mode. | `redis-node1:6379,redis-node2:6379,redis-node3:6379` |
| `REDIS_NATMAP` | The NAT mapping for Redis nodes in `externalAddress:host:port` format. Required for Redis cluster configurations where external IPs or ports are mapped to internal Redis node addresses. | `10.0.0.1:6379:redis-node1:6379,10.0.0.2:6379:redis-node2:6379` |
| `REDIS_URL` | The Redis database URL for connecting to the server.(only single mode) | `redis://localhost:6379` |
| `THRESHOLD_TIMESTAMP` | Allows set threshold time to execute message persist module on milisecond | `60000` |

Expand Down
4 changes: 3 additions & 1 deletion packages/server/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ services:
- APP_PORT=3500
- WS_PORT=3100
- MONGODB_URI=mongodb://cloud-agent:cloud-agent@mongodb:27017/MessagePickupRepository
- REDIS_TYPE=single
- REDIS_TYPE=cluster
- REDIS_NODES=192.168.10.13:6371,192.168.10.13:6372,192.168.10.13:6373,192.168.10.13:6374,192.168.10.13:6375,192.168.10.13:6376
- REDIS_NATMAP=172.29.0.2:6379:192.168.10.13:6371,172.29.0.3:6379:192.168.10.13:6372,172.29.0.4:6379:192.168.10.13:6373,172.29.0.5:6379:192.168.10.13:6374,172.29.0.6:6379:192.168.10.13:6375,172.29.0.7:6379:192.168.10.13:6376
- REDIS_URL=redis://redis:6379
restart: always
ports:
Expand Down
29 changes: 25 additions & 4 deletions packages/server/src/config/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,34 @@ export default registerAs('appConfig', () => ({
mongoDbUri: process.env.MONGODB_URI || 'mongodb://cloud-agent:cloud-agent@localhost:27017/MessagePickupRepository',

/**
* The Redis database URL for connecting to the Redis server.
* Defaults to a specified local Redis instance if REDIS_URL is not set in the environment variables.
* Defines the Redis mode, which can be 'single' or 'cluster'.
* Defaults to 'single' if REDIS_TYPE is not set in the environment variables.
* @type {string}
*/
redisDbUrl: process.env.REDIS_URL || 'redis://192.168.100.84:6379',
redisType: process.env.REDIS_TYPE || 'single',

redisType: process.env.REDIS_TYPE || 'cluster',
/**
* A comma-separated list of Redis nodes in 'host:port' format, used in cluster mode.
* Only relevant if REDIS_TYPE is set to 'cluster'.
* @type {string | undefined}
*/
redisNodes: process.env.REDIS_NODES,

/**
* The NAT mapping for Redis nodes, defined in 'externalAddress:host:port' format.
* Useful for Redis cluster configurations with external IP mappings.
* @type {string | undefined}
*/
redisNatmap: process.env.REDIS_NATMAP,
/**
* The Redis database URL for connecting to the Redis server Single Mode.
* Defaults to a specified local Redis instance if REDIS_URL is not set in the environment variables.
* @type {string}
*/
redisDbUrl: process.env.REDIS_URL || 'redis://localhost:6379',

/**
*Allows set threshold time to execute messagePersist module on milisecond
*/
thresholdTimestamp: parseInt(process.env.THRESHOLD_TIMESTAMP) || 60000,
}))
42 changes: 25 additions & 17 deletions packages/server/src/modules/redis.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common'
import { Module, Logger } from '@nestjs/common'
import { RedisModule, RedisModuleOptions } from '@nestjs-modules/ioredis'
import { ConfigModule, ConfigService } from '@nestjs/config'

Expand All @@ -8,27 +8,35 @@ import { ConfigModule, ConfigService } from '@nestjs/config'
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService): RedisModuleOptions => {
const logger = new Logger('RedisModule')
const redisType = configService.get<string>('appConfig.redisType', 'single')
logger.log(`[RedisModule] Configuring Redis with type: ${redisType}`)
if (redisType === 'cluster') {
const nodes = configService
.get<string>('appConfig.redisNodes', '')
.split(',')
.map((node) => {
const [host, port] = node.split(':')
return { host, port: parseInt(port, 10) }
})
logger.debug(`[RedisModule] Cluster Nodes: ${nodes}`)
const natMap = configService
.get<string>('appConfig.redisNatmap', '')
.split(',')
.reduce(
(map, entry) => {
const [externalAddress, externalPort, internalHost, internalPort] = entry.split(':')
map[`${externalAddress}:${externalPort}`] = { host: internalHost, port: parseInt(internalPort, 10) }
return map
},
{} as Record<string, { host: string; port: number }>,
)
logger.debug(`[RedisModule] Cluster Nat: ${nodes}`)
return {
type: 'cluster',
nodes: [
{ host: 'localhost', port: 6371 },
{ host: 'localhost', port: 6372 },
{ host: 'localhost', port: 6373 },
{ host: 'localhost', port: 6374 },
{ host: 'localhost', port: 6375 },
{ host: 'localhost', port: 6376 },
],
nodes,
options: {
natMap: {
'172.29.0.2:6379': { host: 'localhost', port: 6371 },
'172.29.0.3:6379': { host: 'localhost', port: 6372 },
'172.29.0.4:6379': { host: 'localhost', port: 6373 },
'172.29.0.5:6379': { host: 'localhost', port: 6374 },
'172.29.0.6:6379': { host: 'localhost', port: 6375 },
'172.29.0.7:6379': { host: 'localhost', port: 6376 },
},
natMap,
redisOptions: {
connectTimeout: 10000, // Maximum wait time for connection in milliseconds
maxRetriesPerRequest: 5, // Maximum number of retries per request
Expand Down
Loading