alanxz / rabbitmq-c

RabbitMQ C client
MIT License
1.77k stars 671 forks source link

Add async API #370

Open alanxz opened 8 years ago

alanxz commented 8 years ago

Make the necessary changes to make rabbitmq-c support an async API.

This will likely require some fairly significant refactoring of the way that rabbitmq-c works both internally and at an external API level.

Likely rabbitmq-c will want to move towards a model suggested in #138. A 'connection' object will be something that can be plugged into an arbitrary event loop and react to 'read', 'write', and 'timeout', as well as a method to submit things to that event loop. The initial revision will likely not be thread-safe, and will assume that only one thread is accessing the connection at a time. (this is a reaction to the observation that there are no good cross-platform libraries for C that help do multi-threading (unlike C++), building up an abstraction around native platform primitives is doable, but difficult to get correct). Careful design of the API can hopefully make this less of an issue for most users.

Currently the way rabbitmq-c manages memory (an internal set of arenas that are per-channel) are incompatible with async: making decisions on when its safe to release memory becomes very difficult under this model. We will need to move to a model where memory is managed at a per-frame or per-message level, thus the frame owns the memory and can be released when the frame is no longer needed. This will likely result in a change in the amqp_frame_t object at minimum, probably changes in the way amqp_bytes_t objects work as well.

An incomplete somewhat handwavy list of milestones necessary to make this happen:

tkren commented 8 years ago

As an aside, libcurl has support for async APIs with curl_multi_socket_action: https://curl.haxx.se/libcurl/c/curl_multi_socket_action.html

It is more or less easy to integrate libcurl with event-driven libraries this way. Maybe this is a starting point for a rabbitmq-c async API?

alanxz commented 8 years ago

@tkren thanks for the tip on that. The other similar one I was looking at is c-ares.