pocketnetteam / pocketnet.core

Decentralized social network based on the blockchain
https://pocketnet.app
Apache License 2.0
114 stars 28 forks source link

Implement a class to implement a WebSocket message queue. #119

Closed andyoknen closed 2 years ago

andyoknen commented 2 years ago

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.

lostystyg commented 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.

andyoknen commented 2 years ago

@lostystyg Could you take this to work?

tawmaz commented 2 years ago

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.

andyoknen commented 2 years ago

I propose to consider the implementation of this task for version 0.20.18

lostystyg commented 2 years ago

Here websocket queue processing is extracted to a different thread and works well with shallow-minded testing. I'll create a PR after 0.20.18 released because we are targeting these changes for 0.20.19