Skip to content

feat(RedisBroker): ability to explicitly tell the library to pick a random group #11002

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 16 additions & 5 deletions packages/brokers/src/brokers/redis/BaseRedis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ declare module 'ioredis' {
}
}

export const kUseRandomGroupName = Symbol.for('djs.brokers.useRandomGroupName');

/**
* Options specific for a Redis broker
*/
Expand All @@ -25,11 +27,11 @@ export interface RedisBrokerOptions extends BaseBrokerOptions {
blockTimeout?: number;

/**
* Consumer group name to use for this broker
* Consumer group name to use for this broker. For fanning out events, use {@link kUseRandomGroupName}
*
* @see {@link https://redis.io/commands/xreadgroup/}
*/
group: string;
group: string | typeof kUseRandomGroupName;

/**
* Max number of messages to poll at once
Expand Down Expand Up @@ -84,6 +86,14 @@ export abstract class BaseRedisBroker<
*/
protected readonly streamReadClient: Redis;

/**
* The group being used by this broker.
*
* @privateRemarks
* Stored as its own field to do the "use random group" resolution in the constructor.
*/
protected readonly group: string;

/**
* Whether this broker is currently polling events
*/
Expand All @@ -95,6 +105,7 @@ export abstract class BaseRedisBroker<
) {
super();
this.options = { ...DefaultRedisBrokerOptions, ...options };
this.group = this.options.group === kUseRandomGroupName ? randomBytes(16).toString('hex') : this.options.group;
redisClient.defineCommand('xcleangroup', {
numberOfKeys: 1,
lua: readFileSync(resolve(__dirname, '..', 'scripts', 'xcleangroup.lua'), 'utf8'),
Expand All @@ -111,7 +122,7 @@ export abstract class BaseRedisBroker<
events.map(async (event) => {
this.subscribedEvents.add(event as string);
try {
return await this.redisClient.xgroup('CREATE', event as string, this.options.group, 0, 'MKSTREAM');
return await this.redisClient.xgroup('CREATE', event as string, this.group, 0, 'MKSTREAM');
} catch (error) {
if (!(error instanceof ReplyError)) {
throw error;
Expand Down Expand Up @@ -154,7 +165,7 @@ export abstract class BaseRedisBroker<
try {
const data = await this.streamReadClient.xreadgroupBuffer(
'GROUP',
this.options.group,
this.group,
this.options.name,
'COUNT',
String(this.options.maxChunk),
Expand All @@ -181,7 +192,7 @@ export abstract class BaseRedisBroker<
continue;
}

this.emitEvent(id, this.options.group, event.toString('utf8'), this.options.decode(data));
this.emitEvent(id, this.group, event.toString('utf8'), this.options.decode(data));
}
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/brokers/src/brokers/redis/RPCRedis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class RPCRedisBroker<TEvents extends Record<string, any[]>, TResponses ex
const payload: { ack(): Promise<void>; data: unknown; reply(data: unknown): Promise<void> } = {
data,
ack: async () => {
await this.redisClient.xack(event, this.options.group, id);
await this.redisClient.xack(event, this.group, id);
},
reply: async (data) => {
await this.redisClient.publish(`${event}:${id.toString()}`, this.options.encode(data));
Expand Down