Skip to content
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: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/message-broker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@user-office-software/duo-message-broker",
"version": "1.8.0",
"version": "1.9.0",
"description": "",
"author": "SWAP",
"license": "ISC",
Expand Down
6 changes: 4 additions & 2 deletions packages/message-broker/src/consumer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { ConsumerCallback } from './index';
import { ConsumerCallback, ListenOnOptions } from './index';

// This class is used to store the callback function and whether it is registered or not.
export class Consumer {
callback: ConsumerCallback;
registered: boolean;
options: ListenOnOptions;

constructor(callback: ConsumerCallback) {
constructor(callback: ConsumerCallback, options: ListenOnOptions = {}) {
this.callback = callback;
this.registered = false;
this.options = options;
}

register() {
Expand Down
20 changes: 20 additions & 0 deletions packages/message-broker/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('RabbitMQMessageBroker', () => {
bindQueue: jest.fn(),
publish: jest.fn(),
consume: jest.fn().mockResolvedValue({}),
prefetch: jest.fn(),
on: jest.fn(),
};

Expand Down Expand Up @@ -109,6 +110,25 @@ describe('RabbitMQMessageBroker', () => {
await broker.listenOn(Queue.SCHEDULING_PROPOSAL, consumer);
expect(mockAmqpChannel.consume).toHaveBeenCalledTimes(2);
});

it('should call channel.prefetch with the given value before consume when prefetch option is set', async () => {
await broker.listenOn(Queue.SCHEDULING_PROPOSAL, consumer, {
prefetch: 10,
});

expect(mockAmqpChannel.prefetch).toHaveBeenCalledWith(10, false);
const prefetchOrder = (mockAmqpChannel.prefetch as jest.Mock).mock
.invocationCallOrder[0];
const consumeOrder = (mockAmqpChannel.consume as jest.Mock).mock
.invocationCallOrder[0];
expect(prefetchOrder).toBeLessThan(consumeOrder);
});

it('should not call channel.prefetch when prefetch option is not set', async () => {
await broker.listenOn(Queue.SCHEDULING_PROPOSAL, consumer);

expect(mockAmqpChannel.prefetch).not.toHaveBeenCalled();
});
});

describe('scheduleReconnect', () => {
Expand Down
22 changes: 19 additions & 3 deletions packages/message-broker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export type ConsumerCallback = (
properties: MessageProperties
) => Promise<void>;

export type ListenOnOptions = {
prefetch?: number;
};

export interface MessageBroker {
sendMessage(queue: Queue, type: string, message: string): Promise<void>;
sendBroadcast(queue: Queue, type: string, message: string): Promise<void>;
Expand All @@ -36,7 +40,11 @@ export interface MessageBroker {
queueName: string,
exchangeName: string
): Promise<void>;
listenOn(queue: Queue, cb: ConsumerCallback): Promise<void>;
listenOn(
queue: Queue,
cb: ConsumerCallback,
options?: ListenOnOptions
): Promise<void>;
listenOnBroadcast(cb: ConsumerCallback): void;
}

Expand Down Expand Up @@ -110,12 +118,16 @@ export class RabbitMQMessageBroker implements MessageBroker {
}
}

async listenOn(queue: Queue, cb: ConsumerCallback) {
async listenOn(
queue: Queue,
cb: ConsumerCallback,
options: ListenOnOptions = {}
) {
if (!this.queueConsumers.has(queue)) {
this.queueConsumers.set(queue, []);
}

this.queueConsumers.get(queue)?.push(new Consumer(cb));
this.queueConsumers.get(queue)?.push(new Consumer(cb, options));

if (this.channel) {
await this.registerConsumers();
Expand Down Expand Up @@ -385,6 +397,10 @@ export class RabbitMQMessageBroker implements MessageBroker {
consumer.register();
}

if (consumer.options.prefetch !== undefined) {
await this.channel.prefetch(consumer.options.prefetch, false);
}

await this.channel
.consume(
queue,
Expand Down
Loading