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's take a look at it together.
The Redis server is an event driver
. The so-called event driver is to enter a command and press Enter, and then the message is Assemble into the format of Redis
protocol and send it to the Redis
server. At this time, an event will be generated, and the Redis
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.
The Redis server needs to handle the following two types of events:
File events
: 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.
Time event
: 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's abstraction of such timing operations.
Redis abstracts file events
and time events
into a data structure for management.
typedef struct aeFileEvent {
// File time type: AE_NONE, AE_READABLE, AE_WRITABLE
int mask; // Readable processing function aeFileProc *rfileProc;
// writable processing function
aeFileProc *wfileProc;
// The data passed in by the client void *clientData; }
aeFileEvent;
//File event
Where rfileProc
and wfileProThe c
members are two function pointers, and their prototypes are:
typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
This function is a callback function
, if the event type specified by the current file event occurs, the corresponding callback function
will be called to handle the event.
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 aeFiredEvent
structure is defined for unified management:
typedef struct aeFiredEvent {
// The file descriptor for the ready event
int fd;
// Ready event types: AE_NONE, AE_READABLE, AE_WRITABLE
int mask; } aeFiredEvent;
//Ready event
Type of file event:
#define AE_NONE 0 //Not set
#define AE_READABLE 1 //event is readable
#define AE_WRITABLE 2 //Events can be written
typedef struct aeTimeEvent {
// The id of the time event
long long id;
// The number of seconds when the time event arrives
long when_sec; /* seconds */
// The number of milliseconds when the time event
arrives long when_ms; /* milliseconds */
// Time event handler function
aeTimeProc *timeProc;
// Time event termination function
aeEventFinalizerProc *finalizerProc;
// The data passed in by the client
void *clientData;
// point to the next time event
struct aeTimeEvent *next; } aeTimeEvent; //Time event
It can be seen that the structure of a time event is a node of a linked list, becausestruct aeTimeEvent *next
is the pointer to the next time event.
Same as the file event, when the event specified by the time event occurs, the corresponding will also be called Callback function
, structure members timeProc
and finalizerProc
are callback functions, the function prototype is as follows:
typedef int aeTimeProc(struct aeEventLoop *eventLoop , long long id, void *clientData);
typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);
Although both file events and time events have been abstracted, Redis
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: aeEventLoop
.
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