Skip to content

Create reference for client - #85

Merged
massakam merged 1 commit into
apache:masterfrom
yosiat:producer-client-ref
Mar 26, 2020
Merged

Create reference for client#85
massakam merged 1 commit into
apache:masterfrom
yosiat:producer-client-ref

Conversation

@yosiat

@yosiat yosiat commented Mar 25, 2020

Copy link
Copy Markdown
Contributor

Since we don’t have reference for the client, v8 can garbage collect this instance, which can cause the next code:

const Pulsar = require('pulsar-client');

(async () => {
  // Create a client
  const client = new Pulsar.Client({
    serviceUrl: 'pulsar://localhost:6650'
  });

  // Create a producer
  const producer = await client.createProducer({
    topic: 'persistent://public/default/my-topic',
    sendTimeoutMs: 30000,
    batchingEnabled: true,
  });

  let i =0;
  async function produceMessage() {
    i = i + 1;

    const msg = `my-message-${i}`;
    const ret = await producer.send({
      data: Buffer.from(msg),
    });
    console.log(`Sent message: ${msg}`);
    console.log(ret);

    setTimeout(produceMessage, 1000);
  }

  produceMessage();
})();

To close the connection suddenly and fail the producer.

Fixes: #78

Since we don’t have reference for the client, v8 can garbage collect this instance, which can cause the next code:

```
const Pulsar = require('pulsar-client');

(async () => {
  // Create a client
  const client = new Pulsar.Client({
    serviceUrl: 'pulsar://localhost:6650'
  });

  // Create a producer
  const producer = await client.createProducer({
    topic: 'persistent://public/default/my-topic',
    sendTimeoutMs: 30000,
    batchingEnabled: true,
  });

  let i =0;
  async function produceMessage() {
    i = i + 1;

    const msg = `my-message-${i}`;
    const ret = await producer.send({
      data: Buffer.from(msg),
    });
    console.log(`Sent message: ${msg}`);
    console.log(ret);

    setTimeout(produceMessage, 1000);
  }

  produceMessage();
})();
```

To close the connection suddenly and fail the producer.
@yosiat

yosiat commented Mar 25, 2020

Copy link
Copy Markdown
Contributor Author

@hrsakai @sijie can you please review this PR ?

@sijie

sijie commented Mar 26, 2020

Copy link
Copy Markdown
Member

@yosiat this is cool! Thank you for your contribution!

@sijie sijie added the type/bug label Mar 26, 2020
@sijie sijie added this to the 1.2.0 milestone Mar 26, 2020

@massakam massakam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@massakam
massakam merged commit 364f36d into apache:master Mar 26, 2020
@yosiat
yosiat deleted the producer-client-ref branch March 26, 2020 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error: Failed to send message: AlreadyClosed

3 participants