-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: support redis cluster mode
- Loading branch information
1 parent
96c4dcd
commit 5599b32
Showing
13 changed files
with
169 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,72 @@ | ||
import config from 'config'; | ||
import { | ||
createClient, | ||
createCluster, | ||
type RedisClientOptions, | ||
type RedisClientType, | ||
type RedisClusterOptions, | ||
type RedisClusterType, | ||
type RedisDefaultModules, | ||
type RedisFunctions, | ||
} from 'redis'; | ||
import Bluebird from 'bluebird'; | ||
import { type RedisScripts, scripts } from './scripts.js'; | ||
import { scopedLogger } from '../logger.js'; | ||
|
||
const logger = scopedLogger('redis-client'); | ||
|
||
type ClusterExtensions = { | ||
mapMasters: typeof mapMasters, | ||
reduceMasters: typeof reduceMasters, | ||
}; | ||
|
||
export type RedisClient = RedisClientType<RedisDefaultModules, RedisFunctions, RedisScripts>; | ||
export type RedisCluster = RedisClusterType<RedisDefaultModules, RedisFunctions, RedisScripts> & ClusterExtensions; | ||
export type RedisClientInternal = { connectPromise: Promise<unknown>, client: RedisClient }; | ||
export type RedisClusterInternal = { connectPromise: Promise<unknown>, client: RedisCluster }; | ||
|
||
export const createRedisClientInternal = (options?: RedisClientOptions): RedisClient => { | ||
export const createRedisClientInternal = (options: RedisClientOptions): RedisClientInternal => { | ||
const client = createClient({ | ||
...config.util.toObject(config.get('redis')) as RedisClientOptions, | ||
...options, | ||
scripts, | ||
}); | ||
|
||
client | ||
const connectPromise = client | ||
.on('error', (error: Error) => logger.error('Redis connection error:', error)) | ||
.on('ready', () => logger.info('Redis connection ready.')) | ||
.on('reconnecting', () => logger.info('Redis reconnecting.')) | ||
.connect().catch((error: Error) => logger.error('Redis connection error:', error)); | ||
|
||
return client; | ||
return { client, connectPromise }; | ||
}; | ||
|
||
export const createRedisClusterInternal = (options: RedisClusterOptions): RedisClusterInternal => { | ||
const cluster = createCluster({ | ||
...options, | ||
scripts, | ||
}); | ||
|
||
const client = Object.assign(cluster, { | ||
mapMasters, | ||
reduceMasters, | ||
}); | ||
|
||
const connectPromise = client | ||
.on('error', (error: Error) => logger.error('Redis connection error:', error)) | ||
.on('ready', () => logger.info('Redis connection ready.')) | ||
.on('reconnecting', () => logger.info('Redis reconnecting.')) | ||
.connect().catch((error: Error) => logger.error('Redis connection error:', error)); | ||
|
||
return { client, connectPromise }; | ||
}; | ||
|
||
function mapMasters<Result> (this: RedisCluster, mapper: (client: RedisClient) => Promise<Result>) { | ||
return Bluebird.map(this.masters, (node) => { | ||
return this.nodeClient(node); | ||
}).map(mapper); | ||
} | ||
|
||
function reduceMasters<Result> (this: RedisCluster, reducer: (accumulator: Result, client: RedisClient) => Promise<Result>, initialValue: Result) { | ||
return Bluebird.map(this.masters, (node) => { | ||
return this.nodeClient(node); | ||
}).reduce(reducer, initialValue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
import config from 'config'; | ||
import type { RedisClientOptions } from 'redis'; | ||
import { createRedisClientInternal, type RedisClient } from './shared.js'; | ||
import { createRedisClientInternal, type RedisClientInternal } from './shared.js'; | ||
|
||
export type { RedisClient } from './shared.js'; | ||
|
||
let redis: RedisClient; | ||
|
||
export const initSubscriptionRedisClient = async () => { | ||
redis = createSubscriptionRedisClient(); | ||
return redis; | ||
const { connectPromise, client } = createSubscriptionRedisClient(); | ||
await connectPromise; | ||
return client; | ||
}; | ||
|
||
export const createSubscriptionRedisClient = (options?: RedisClientOptions): RedisClient => { | ||
export const createSubscriptionRedisClient = (options?: RedisClientOptions): RedisClientInternal => { | ||
return createRedisClientInternal({ | ||
...config.util.toObject(config.get('redis.shared')) as RedisClientOptions, | ||
...config.util.toObject(config.get('redis.standaloneNonPersistent')) as RedisClientOptions, | ||
...options, | ||
name: 'subscription', | ||
}); | ||
}; | ||
|
||
export const getSubscriptionRedisClient = (): RedisClient => { | ||
if (!redis) { | ||
redis = createSubscriptionRedisClient(); | ||
} | ||
|
||
return redis; | ||
}; |
Oops, something went wrong.