restinio icon indicating copy to clipboard operation
restinio copied to clipboard

restinio and amqp-cpp

Open jm130794 opened this issue 6 years ago • 6 comments

Hello, I am asked if it is possible to use amqp-cpp with restinio. As I don't know, I ask the question to the developers of restinio:)

Do you think this is possible easily enough?

Jean-Marc

jm130794 avatar Mar 04 '19 19:03 jm130794

Hi!

Do you want to use the same event-loop for amqp-cpp and RESTinio?

eao197 avatar Mar 04 '19 19:03 eao197

Hi Jean-Marc,

I've taken a look on amqp-cpp, and I'm pretty sure restinio and amqp-cpp can coexist in a single application. Even though boost-asio is not manifested as officially supported you have the following options:

  1. Use different event-loop engines (a simple way: run separate threads for amqp-loop and restinio-loop, a hard way: you still can leverage a single thread if you really need it).
  2. Anyway use an existing boost-asio wrapper and run restinio on the same io_context.

PS Depending on how complicated your internal logic is (seems like you want to be very async) it might worth an effort to look at Actor model and apply it to your case. For example, we've used our own amqp-client (back in 2014) with SObjectizer (a library implementing actor model in C++) in production, so I can say that amqp+sobjectizer works nicely.

ngrodzitski avatar Mar 04 '19 19:03 ngrodzitski

I have never used SObjectizer but indeed, your slides are very interesting. I'm going to test this library.

jm130794 avatar Mar 04 '19 20:03 jm130794

I don't expect any serious problem if:

  • you are using amqp-cpp and restinio on different work threads (it means that libev will work on its own context with its own event-loop, restinio will also work on own thread with own event loop);
  • amqp-cpp provides thread-safety and you can pass info to work thread where amqp-cpp is working.

May be if you tell some more details about your task and desired application design we could make more useful advices.

eao197 avatar Mar 04 '19 20:03 eao197

Hi, I started studying SObjectizer. It seems to me that I have a good understanding of how agents and associated mboxes work. I have made one or two programs and everything is working well.

I have a question: if I integrate AMQP-CPP into an agent (maybe I'm wrong), will the agent be able to handle the events to which it has subscribed? Isn't the ev_loop loop blocking?

Jean-Marc

jm130794 avatar Mar 13 '19 17:03 jm130794

if I integrate AMQP-CPP into an agent (maybe I'm wrong), will the agent be able to handle the events to which it has subscribed? Isn't the ev_loop loop blocking?

If I understand you correctly you want to run AMQP-CPP (with underlying ev_loop) in so_evt_start() of some agent. Right?

In that case, I think, ev_loop will block the work thread of that agent and agent can't handle messages. Maybe it is possible to write a special dispatcher that will use ev_loop from AMQP-CPP to dispatch events for agents bound to that dispatcher (like we do in so_5_extra for Aiso: asio_thread_pool). But it is a complex task.

If you can spawn several wok threads in your app then, I think, the easiest way can be:

  • run one thread for AMQP-CPP;
  • run one thread for RESTinio;
  • run one thread for handling requests from two previous threads (or you can launch SObjectizer with agents that will receive messages from two previous threads).

There are no problems with RESTinio -- it's an asynchronous framework that allows delegating of actual request processing to separate worker thread. Unfortunately, I don't know does AMQP-CPP allow this.

eao197 avatar Mar 13 '19 18:03 eao197