(Original) NSQ source code reading and analysis (1)

 Original Source: https: //www.cnblogs.com/lihaiping/p/12324371.html

This article documents some of his notes in learning to read and learning nsq source when the main purpose is to facilitate post personal summary and review.

Author: Lihapidagl603@aliyunkcom

date:2020/01/13

NSQ for a message sent out is how to ensure the reliability of

For the message router channel will be the topic of the next () function routing, it will transfer the message to the memory chan, that channel in incomingMsgChan to memoryMsgChan, if memoryMsgChan news is full, blocked, it would be a message written to the backend.

For memoryMsgChan message, the channel will be in messagePump () function once again flow, he sends the message received from memoryMsgChan channel, and then forwards again clientMsgChan go.

For each client network link, NSQ will create a link to this client network IOLoop () to handle all of goroutine and client messages. When the client sends a subscription SUB command, client based on the topic and its subscription channel, and then start a goroutine to push the channel msg to client, this function is messagePump () function. In messagePump client, the main task is to send and receive information from the heartbeat and the clientMsgChan channel message, and then send the message package to the client. Of course, the message sent to the client may fail, so NSQ here to do a good fault tolerance strategy failed, when we push a message to the client's time, since the message may fail, so we need to keep up the message, so client this message will be in it in the channel, start a timeout policy, if time-out, then the message will be again with the same pet topic circulation to flow into the channel of news chanel transfer process.

And before sending a network message client, by calling client.Channel.StartInFlightTimeout (msg, client) function to generate the client with a message msg and other message object inFlightMessage, then increase the timeout the message objects is further encapsulated into pqueue.Item, and are stored in the channel and the inFlightMessages inFlightPQ, where inFlightMessages is a map, he stores the id msg, it will be convenient when the client receives a response message later, to delete operation key. While inFlightPQ, this is mainly to do with the overtime, because channel will start a goroutine function inFlightWorker to deal specifically when inFlightPQ Super news.

In inFlightWorker () in the main to continue to take based on the timeout period from pq messages in the queue, if the timeout time is up, the message has not been removed from inFlightPQ queue, indicating that this message may be lost or what problem arises, we will need to re the news flow.

 

Guess you like

Origin www.cnblogs.com/lihaiping/p/12324371.html