Closed andyoknen closed 2 years ago
Currently we are using boost::asio::strand for managing message queue. All actions with queue are performed inside strand wrapper which is guaranteed to be thread safe. (See About Strands). E.x. adding message to queue:
strand.post([self, out_header, out_message, callback]() {
self->send_queue.emplace_back(out_header, out_message, callback);
if(self->send_queue.size() == 1)
self->send_from_queue();
});
Also all other actions with queue are performed inside async handlers that are wrapped by calling strand->wrap() method that guarantees that onl;y one async handler will be invoked in a time.
@lostystyg Could you take this to work?
Created pull request to fix segmentation fault and make accesses to WSConnections map thread safe: https://github.com/pocketnetteam/pocketnet.core/pull/129 As @lostystyg mentioned we are using asio for queueing so we should be good to go with this patch. We may look at optimizing the lock mechanism in the future if it is found to be a bottleneck.
I propose to consider the implementation of this task for version 0.20.18
The class must have a thread-safe stack/queue. Message preparation and sending should be highlighted in the thread. Creating and deleting WebSocket connections should be thread-safe.