This is an automated email from the ASF dual-hosted git repository.

massakam pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-node.git


The following commit(s) were added to refs/heads/master by this push:
     new 364f36d  Create reference for client (#85)
364f36d is described below

commit 364f36d9a42259fae95f3dfd8c1e7774f9773917
Author: Yosi Attias <yosy...@gmail.com>
AuthorDate: Thu Mar 26 04:25:33 2020 +0200

    Create reference for client (#85)
    
    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.
---
 src/Client.cc | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/Client.cc b/src/Client.cc
index 1893af2..b6f301d 100644
--- a/src/Client.cc
+++ b/src/Client.cc
@@ -151,6 +151,7 @@ Client::Client(const Napi::CallbackInfo &info) : 
Napi::ObjectWrap<Client>(info)
 
   this->cClient = pulsar_client_create(serviceUrl.Utf8Value().c_str(), 
cClientConfig);
   pulsar_client_configuration_free(cClientConfig);
+  this->Ref();
 }
 
 Client::~Client() {
@@ -218,6 +219,8 @@ class ClientCloseWorker : public Napi::AsyncWorker {
 };
 
 Napi::Value Client::Close(const Napi::CallbackInfo &info) {
+  this->Unref();
+
   Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(info.Env());
   ClientCloseWorker *wk = new ClientCloseWorker(deferred, this->cClient);
   wk->Queue();

Reply via email to