Skip to content

[feat][cpp] Add async receive function to C API - #17452

Merged
BewareMyPower merged 5 commits into
apache:masterfrom
equanz:add_c_receive_async
Sep 8, 2022
Merged

BewareMyPower merged 5 commits into
apache:masterfrom
equanz:add_c_receive_async

Conversation

@equanz

@equanz equanz commented Sep 4, 2022 •

Copy link
Copy Markdown
Contributor

Master Issue: #14452

Motivation

I want to rewrite the pulsar-client-node receive method to rely exclusively on async operations rather than worker threads.
However, an async receive function is not defined currently in C API.

Modifications

  • Define pulsar_consumer_receive_async and pulsar_receive_callback to use async receive operation in C API

Verifying this change

  • Make sure that the change passes the CI checks.

(Please pick either of the following options)

This change is already covered by existing tests, such as C++ tests.

Does this pull request potentially affect one of the following parts:

If yes was chosen, please highlight the changes

  • Dependencies (does it add or upgrade a dependency): (no)
  • The public API: (yes)
    • typedef void (*pulsar_receive_callback)(pulsar_result result, pulsar_message_t *msg, void *ctx);
    • /**
      * Asynchronously receive a single message.
      *
      * This method will initiate the operation and return immediately. The provided callback
      * will be triggered when the operation is complete.
      *
      * @param callback callback that will be triggered when the message is available
      */
      PULSAR_PUBLIC void pulsar_consumer_receive_async(pulsar_consumer_t *consumer, pulsar_receive_callback callback,
  • The schema: (no)
  • The default values of configurations: (no)
  • The wire protocol: (no)
  • The rest endpoints: (no)
  • The admin cli options: (no)
  • Anything that affects deployment: (no)

Documentation

Check the box below or label this PR directly.

Need to update docs?

  • doc-not-needed

@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Sep 4, 2022
@equanz
equanz force-pushed the add_c_receive_async branch from 7a975c8 to 66765af Compare September 4, 2022 02:54
@tisonkun

tisonkun commented Sep 4, 2022

Copy link
Copy Markdown
Member

@equanz

equanz commented Sep 4, 2022

Copy link
Copy Markdown
Contributor Author

/pulsarbot rerun-failure-checks

@BewareMyPower BewareMyPower added this to the 2.12.0 milestone Sep 4, 2022
@BewareMyPower BewareMyPower added type/feature The PR added a new feature or issue requested a new feature component/client-c++ labels Sep 4, 2022

@BewareMyPower BewareMyPower 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.

Could you add a basic e2e test for demonstrate it works? I found currently there are nearly no C API related tests. But for new APIs, we should add some tests.

@equanz

equanz commented Sep 5, 2022

Copy link
Copy Markdown
Contributor Author

@BewareMyPower

Could you add a basic e2e test for demonstrate it works?

Okay. I'll address it.

@equanz equanz changed the title [feat][c] Add async receive function to C API [feat][cpp] Add async receive function to C API Sep 5, 2022
pulsar_message_t *received_msg;
receive_promise.getFuture().get(received_msg);
ASSERT_STREQ(content, static_cast<const char *>(pulsar_message_get_data(received_msg)));
pulsar_message_free(received_msg);

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.

I think we should release the received_msg after the callback is done. It's better to use pure C semantics here, i.e. use C functions instead of lambda, and do not use pulsar::Promise, we should define a C struct and pass it to ctx.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll modify it.

However, I also use pulsar::Promise to wait for the callback completion in the test thread.
To achieve this process, I'll use std::future instead of conditional wait, etc.

Comment thread pulsar-client-cpp/lib/c/c_Consumer.cc Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs type/feature The PR added a new feature or issue requested a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants