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

feat(microservices): add Kafka heartbeat callback to KafkaContext #9954

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
8 changes: 8 additions & 0 deletions packages/microservices/ctx-host/kafka.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type KafkaContextArgs = [
partition: number,
topic: string,
consumer: Consumer,
heartbeat: () => Promise<void>,
];

export class KafkaContext extends BaseRpcContext<KafkaContextArgs> {
Expand Down Expand Up @@ -40,4 +41,11 @@ export class KafkaContext extends BaseRpcContext<KafkaContextArgs> {
getConsumer() {
return this.args[3];
}

/**
* Returns the Kafka heartbeat callback.
*/
getHeartbeat() {
return this.args[4];
}
}
2 changes: 2 additions & 0 deletions packages/microservices/external/kafka.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,8 @@ export interface EachMessagePayload {
topic: string;
partition: number;
message: KafkaMessage;
heartbeat(): Promise<void>;
pause(): () => void;
}

export interface EachBatchPayload {
Expand Down
1 change: 1 addition & 0 deletions packages/microservices/server/server-kafka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export class ServerKafka extends Server implements CustomTransportStrategy {
payload.partition,
payload.topic,
this.consumer,
payload.heartbeat,
]);
const handler = this.getHandlerByPattern(packet.pattern);
// if the correlation id or reply topic is not set
Expand Down
16 changes: 16 additions & 0 deletions packages/microservices/test/client/client-kafka.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe('ClientKafka', () => {
const timestamp = new Date().toISOString();
const attributes = 1;
const messageValue = 'test-message';
const heartbeat = async () => {};
const pause = () => () => {};

// message
const message: KafkaMessage = {
Expand Down Expand Up @@ -57,6 +59,8 @@ describe('ClientKafka', () => {
},
message,
),
heartbeat,
pause,
};

const payloadDisposed: EachMessagePayload = {
Expand All @@ -75,6 +79,8 @@ describe('ClientKafka', () => {
value: { test: true },
},
),
heartbeat,
pause,
};

const payloadError: EachMessagePayload = {
Expand All @@ -93,6 +99,8 @@ describe('ClientKafka', () => {
value: null,
},
),
heartbeat,
pause,
};

const payloadWithoutCorrelation: EachMessagePayload = {
Expand All @@ -104,6 +112,8 @@ describe('ClientKafka', () => {
},
message,
),
heartbeat,
pause,
};

// deserialized payload
Expand All @@ -118,6 +128,8 @@ describe('ClientKafka', () => {
},
deserializedMessage,
),
heartbeat,
pause,
};

const deserializedPayloadDisposed: EachMessagePayload = {
Expand All @@ -136,6 +148,8 @@ describe('ClientKafka', () => {
value: { test: true },
},
),
heartbeat,
pause,
};

const deserializedPayloadError: EachMessagePayload = {
Expand All @@ -154,6 +168,8 @@ describe('ClientKafka', () => {
value: null,
},
),
heartbeat,
pause,
};

let client: ClientKafka;
Expand Down
9 changes: 7 additions & 2 deletions packages/microservices/test/ctx-host/kafka.context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { KafkaContext } from '../../ctx-host';
import { Consumer, KafkaMessage } from '../../external/kafka.interface';

describe('KafkaContext', () => {
const args = ['test', { test: true }, undefined, { test: 'consumer' }];
const args = ['test', { test: true }, undefined, { test: 'consumer' }, () => {}];
let context: KafkaContext;

beforeEach(() => {
context = new KafkaContext(
args as [KafkaMessage, number, string, Consumer],
args as [KafkaMessage, number, string, Consumer, () => Promise<void>],
);
});
describe('getTopic', () => {
Expand All @@ -31,4 +31,9 @@ describe('KafkaContext', () => {
expect(context.getConsumer()).to.deep.eq({ test: 'consumer' });
});
});
describe('getHeartbeat', () => {
it('should return heartbeat callback', () => {
expect(context.getHeartbeat()).to.be.eql(args[4]);
});
});
});
8 changes: 8 additions & 0 deletions packages/microservices/test/server/server-kafka.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ describe('ServerKafka', () => {
const key = '1';
const timestamp = new Date().toISOString();
const messageValue = 'test-message';
const heartbeat = async () => {};
const pause = () => () => {};

const eventMessage: KafkaMessage = {
key: Buffer.from(key),
Expand All @@ -44,6 +46,8 @@ describe('ServerKafka', () => {
},
eventMessage,
),
heartbeat,
pause,
};

const eventWithCorrelationIdPayload: EachMessagePayload = {
Expand All @@ -57,6 +61,8 @@ describe('ServerKafka', () => {
},
eventMessage,
),
heartbeat,
pause,
};

const message: KafkaMessage = Object.assign(
Expand All @@ -73,6 +79,8 @@ describe('ServerKafka', () => {
topic,
partition: 0,
message,
heartbeat,
pause,
};

let server: ServerKafka;
Expand Down