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

fix(microservices): messages emitted before kafka client is ready fail #11026

Merged
merged 2 commits into from
Feb 3, 2023
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
84 changes: 47 additions & 37 deletions packages/microservices/client/client-kafka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ let kafkaPackage: any = {};

export class ClientKafka extends ClientProxy {
protected logger = new Logger(ClientKafka.name);
protected client: Kafka = null;
protected consumer: Consumer = null;
protected producer: Producer = null;
protected parser: KafkaParser = null;
protected client: Kafka | null = null;
protected consumer: Consumer | null = null;
protected producer: Producer | null = null;
protected parser: KafkaParser | null = null;
protected initialized: Promise<void> | null = null;
protected responsePatterns: string[] = [];
protected consumerAssignments: { [key: string]: number } = {};

protected brokers: string[] | BrokersFunction;
protected clientId: string;
protected groupId: string;
Expand Down Expand Up @@ -94,46 +94,56 @@ export class ClientKafka extends ClientProxy {
this.consumer && (await this.consumer.disconnect());
this.producer = null;
this.consumer = null;
this.initialized = null;
this.client = null;
}

public async connect(): Promise<Producer> {
if (this.client) {
return this.producer;
if (this.initialized) {
return this.initialized.then(() => this.producer);
}
this.client = this.createClient();

if (!this.producerOnlyMode) {
const partitionAssigners = [
(
config: ConstructorParameters<typeof KafkaReplyPartitionAssigner>[1],
) => new KafkaReplyPartitionAssigner(this, config),
] as any[];

const consumerOptions = Object.assign(
{
partitionAssigners,
},
this.options.consumer || {},
{
groupId: this.groupId,
},
);
this.initialized = new Promise(async (resolve, reject) => {
try {
this.client = this.createClient();

if (!this.producerOnlyMode) {
const partitionAssigners = [
(
config: ConstructorParameters<
typeof KafkaReplyPartitionAssigner
>[1],
) => new KafkaReplyPartitionAssigner(this, config),
];

const consumerOptions = Object.assign(
{
partitionAssigners,
},
this.options.consumer || {},
{
groupId: this.groupId,
},
);

this.consumer = this.client.consumer(consumerOptions);
// set member assignments on join and rebalance
this.consumer.on(
this.consumer.events.GROUP_JOIN,
this.setConsumerAssignments.bind(this),
);
await this.consumer.connect();
await this.bindTopics();
}
this.consumer = this.client.consumer(consumerOptions);
// set member assignments on join and rebalance
this.consumer.on(
this.consumer.events.GROUP_JOIN,
this.setConsumerAssignments.bind(this),
);
await this.consumer.connect();
await this.bindTopics();
}

this.producer = this.client.producer(this.options.producer || {});
await this.producer.connect();
this.producer = this.client.producer(this.options.producer || {});
await this.producer.connect();

return this.producer;
resolve();
} catch (err) {
reject(err);
}
});
return this.initialized.then(() => this.producer);
}

public async bindTopics(): Promise<void> {
Expand Down
32 changes: 5 additions & 27 deletions packages/microservices/test/client/client-kafka.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from '../../external/kafka.interface';

describe('ClientKafka', () => {
// static
const topic = 'test.topic';
const partition = 0;
const replyTopic = 'test.topic.reply';
Expand All @@ -25,7 +24,6 @@ describe('ClientKafka', () => {
const heartbeat = async () => {};
const pause = () => () => {};

// message
const message: KafkaMessage = {
key: Buffer.from(key),
offset,
Expand All @@ -35,7 +33,6 @@ describe('ClientKafka', () => {
attributes,
};

// deserialized message
const deserializedMessage: any = {
key,
offset,
Expand All @@ -47,7 +44,6 @@ describe('ClientKafka', () => {
partition,
};

// payloads
const payload: EachMessagePayload = {
topic,
partition,
Expand Down Expand Up @@ -152,26 +148,6 @@ describe('ClientKafka', () => {
pause,
};

const deserializedPayloadError: EachMessagePayload = {
topic,
partition,
message: Object.assign(
{
headers: {
[KafkaHeaders.CORRELATION_ID]: correlationId,
[KafkaHeaders.NEST_ERR]: NO_MESSAGE_HANDLER,
},
},
deserializedMessage,
{
size: 0,
value: null,
},
),
heartbeat,
pause,
};

let client: ClientKafka;
let callback: sinon.SinonSpy;
let connect: sinon.SinonSpy;
Expand All @@ -182,7 +158,7 @@ describe('ClientKafka', () => {
let consumerStub: sinon.SinonStub;
let producerStub: sinon.SinonStub;
let createClientStub: sinon.SinonStub;
let kafkaClient;
let kafkaClient: any;

beforeEach(() => {
client = new ClientKafka({});
Expand Down Expand Up @@ -322,7 +298,8 @@ describe('ClientKafka', () => {
});

it('should expect the connection to be reused', async () => {
(client as any).client = kafkaClient;
(client as any).initialized = Promise.resolve({});

await client.connect();

expect(createClientStub.calledOnce).to.be.false;
Expand Down Expand Up @@ -368,7 +345,8 @@ describe('ClientKafka', () => {
});

it('should expect the connection to be reused', async () => {
(client as any).client = kafkaClient;
(client as any).initialized = Promise.resolve({});

await client.connect();

expect(createClientStub.calledOnce).to.be.false;
Expand Down