I am working on creating a websocket server that receives a message and saves it to an embedded database . For reading messages I use boost asio . To save messages in the embedded database, I see several options in front of me:
I am sure that the second answer is what I want. However, I am not sure how to transfer messages from the socket stream to the input / output stream. I see the following options:
In addition, I believe that this will help to have several input / output streams that will receive messages in a cyclic mode for saving to the built-in database.
Which architecture will be most effective? Are there any other alternatives that I mentioned?
A few notes:
, unix-, . , , , .
API , (, ), boost::asio::io_service . io_service::strand io_service strand::dispatch() ( io_service::post()) . strand , , io_service .
boost::asio::io_service
io_service::strand
io_service
strand::dispatch()
io_service::post()
strand
, io_service? , . , strand::dispatch() , (, strand ), .
, , , . , / - , , .
/ , MPMC Queue Facebook 50%. non-blocking write, - . , .
SPSC cond boost . , . - , .
, ( UDP ) , . .
, , io_service - , , io_service, boost::asio::io_service::run . 8- .
boost::asio::io_service::run
. , , . , , boost::lockfree::queue, , io_service , .
boost::lockfree::queue
? . , , , . - : , , , , , .
void Consumer( lockfree::queue<uint64_t> &message_queue ) { // Connect to database... while (!Finished) { message_queue.consume_all( add_to_database ); // add_to_database is a Functor that takes a message cond_var.wait_for( ... ); // Use a timed wait to avoid missing a signal. It OK to consume_all() even if there nothing in the queue. } } void Producer( lockfree::queue<uint64_t> &message_queue ) { while (!Finished) { uint64_t m = receive_from_network( ); message_queue.push( m ); cond_var.notify_all( ); } }
, cxx11 , std:: async, .