surgemq main process and advantages and disadvantages

main process


  1. Server Listen tcp port
  2. for loop Accept
  3. Accept后go handleConnection(conn)
  4. handleConnection creates srv service, srv is equivalent to client
  5. There are 3 goroutines in the service: processor(), receiver(), sender()
  6. The in buffer and out buffer are created in the service, and the buffer is the ring buffer
  7. receiver() receives data to in buffer
  8. sender() fetches data from the out buffer and sends
  9. processor() processes mqtt business logic, fetches data from in buffer, and writes data to outbuffer when sending data

Advantages and disadvantages


It can be seen from the above that a service, that is, a client, occupies 4 goroutines, but the ringbuffer is used, so the speed is still relatively fast, but if the ringbuffer is not well controlled, errors may occur.
Compared to gnatsd's client using only one goroutine, surgemq takes up too many goroutines.
But gnatsd uses more complex lock control, which is more complicated in program logic than surgemq.
The mqtt message of surgemq uses the Message class to parse and process, which is clearer and less coupled with the main program than the parse of gnatsd.

surgemq is still quite incomplete, doesn't support clustering, doesn't support persistence, etc.

I carefully studied surgemq and found that the ringbuf used by it occupies a surprising amount of memory. One ringbuf occupies 256K of memory, and a client connects two ringbufs in and out with a total of 512K bytes, so 1,000 clients occupy 512M bytes. too terrifying. There is also a goroutine that occupies 2k bytes, and a client goes to 8K, and the memory overhead is not small when there are more client connections.

attached


I took a look at the code and realized the ack of qos1 and 2, but did not realize the retransmission...
For example, qos1: send an ack immediately after receiving the publish message from the client, and send the publish message to other subs at the same time, and save it in the queue to wait The ack message of the sub. However, if the ack message from the sub has not been received, the publish will not be resent, so there is no guarantee that the message will reach the sub.

In addition, I took a look at volantmq, which claims to inherit surgemq. It has been changed beyond recognition, and the code is really ugly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325671363&siteId=291194637
Recommended