Redis event processing source code analysis

2023-01-19 23:21:07

<p style="text-align: left;">Today, the editor will share with you the relevant knowledge points of Redis event processing source code analysis. After reading this article, everyone has gained something, let&#39;s take a look at it together.</p><h3 style="text-align: left;">1. Introduction to Redis events</h3><p style="text-align: left;">The Redis server is an <code>event driver</code>. The so-called event driver is to enter a command and press Enter, and then the message is Assemble into the format of <code>Redis</code> protocol and send it to the <code>Redis</code> server. At this time, an event will be generated, and the <code>Redis</code> server will receive the modification command and process the command And send a reply, and when we are not interacting with the server, the server will be in a blocked waiting state, it will let the CPU go to sleep, and when the event is triggered, it will be woken up by the operating system.</p><p style="text-align: left;">The Redis server needs to handle the following two types of events:</p><p style="text-align: left;"><code>File events</code>: The Redis server connects with the client (or other Redis servers) through sockets, and the file events are The server abstracts socket operations. The communication between the server and the client (or other servers) will generate corresponding file events, and the server will complete a series of network communication operations by listening to and processing these events.</p><p style="text-align: left;"><code>Time event</code>: Some operations in the Redis server (such as the serverCron function) need to be executed at a given point in time, and time events are the server&#39;s abstraction of such timing operations.</p><h3 style="text-align: left;">2. Abstraction of events</h3><p style="text-align: left;">Redis abstracts <code>file events</code> and <code>time events</code> into a data structure for management.</p><h4 style="text-align: left;">2.1 File event structure</h4><p style="text-align: left;">typedef struct aeFileEvent {&nbsp;</p><p style="text-align: left;">// File time type: AE_NONE, AE_READABLE, AE_WRITABLE&nbsp;</p><p style="text-align: left;">int mask; // Readable processing function aeFileProc *rfileProc;&nbsp;</p><p style="text-align: left;">// writable processing function&nbsp;</p><p style="text-align: left;">aeFileProc *wfileProc;&nbsp;</p><p style="text-align: left;">// The data passed in by the client void *clientData; }&nbsp;</p><p style="text-align: left;">aeFileEvent;&nbsp;</p><p style="text-align: left;">//File event</p><p style="text-align: left;">Where <code>rfileProc</code> and <code>wfileProThe c</code> members are two function pointers, and their prototypes are:</p><p style="text-align: left;">typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);</p><p style="text-align: left;"><br/></p><p style="text-align: left;">This function is a <code>callback function</code>, if the event type specified by the current file event occurs, the corresponding <code>callback function</code> will be called to handle the event.</p><p style="text-align: left;">When the event is ready, we need to know the file descriptor and event type of the file event to lock the event, so the <code>aeFiredEvent</code> structure is defined for unified management:</p><p style="text-align: left;">typedef struct aeFiredEvent {&nbsp;</p><p style="text-align: left;">// The file descriptor for the ready event&nbsp;</p><p style="text-align: left;">int fd;&nbsp;</p><p style="text-align: left;">// Ready event types: AE_NONE, AE_READABLE, AE_WRITABLE&nbsp;</p><p style="text-align: left;">int mask; } aeFiredEvent;&nbsp;</p><p style="text-align: left;">//Ready event</p><p style="text-align: left;">Type of file event:</p><p style="text-align: left;">#define AE_NONE 0 //Not set&nbsp;</p><p style="text-align: left;">#define AE_READABLE 1 //event is readable&nbsp;</p><p style="text-align: left;">#define AE_WRITABLE 2 //Events can be written</p><h4 style="text-align: left;">2.2 Time event structure</h4><p style="text-align: left;">typedef struct aeTimeEvent {&nbsp;</p><p style="text-align: left;">// The id of the time event&nbsp;</p><p style="text-align: left;">long long id;&nbsp;</p><p style="text-align: left;">// The number of seconds when the time event arrives&nbsp;</p><p style="text-align: left;">long when_sec; /* seconds */&nbsp;</p><p style="text-align: left;">// The number of milliseconds when the time event&nbsp;</p><p style="text-align: left;">arrives long when_ms; /* milliseconds */&nbsp;</p><p style="text-align: left;">// Time event handler function&nbsp;</p><p style="text-align: left;">aeTimeProc *timeProc;&nbsp;</p><p style="text-align: left;">// Time event termination function&nbsp;</p><p style="text-align: left;">aeEventFinalizerProc *finalizerProc;&nbsp;</p><p style="text-align: left;">// The data passed in by the client&nbsp;</p><p style="text-align: left;">void *clientData;&nbsp;</p><p style="text-align: left;">// point to the next time event&nbsp;</p><p style="text-align: left;">struct aeTimeEvent *next; } aeTimeEvent; //Time event</p><p style="text-align: left;"><br/></p><p style="text-align: left;">It can be seen that the structure of a time event is a node of a linked list, because<code>struct aeTimeEvent *next</code> is the pointer to the next time event.</p><p style="text-align: left;">Same as the file event, when the event specified by the time event occurs, the corresponding <code> will also be called Callback function</code>, structure members <code>timeProc</code> and <code>finalizerProc</code> are callback functions, the function prototype is as follows:</p><p style="text-align: left;">typedef int aeTimeProc(struct aeEventLoop *eventLoop , long long id, void *clientData);&nbsp;</p><p style="text-align: left;">typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);</p><p style="text-align: left;"><br/></p><p style="text-align: left;">Although both file events and time events have been abstracted, <code>Redis</code> still needs to make an overall abstraction of events , used to describe the state of an event. That is, the event state structure to be introduced below: <code>aeEventLoop</code>.</p>


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us