Recent research has redis source code, and Redis efficiency is admirable.
On our Linux that machine, the CPU model,
Intel (R) Pentium (r) CPU G630 @ 2.70GHz
Intel (R) Pentium (r) CPU G630 @ 2.70GHz
On the set,get can achieve 15W per second request processing volume, really admire the efficiency of this code.
Previous articles. It mainly introduces the main code. For example, string processing, linked list processing. Hash and so on.
This article describes the core of the network, an asynchronous network framework based on event reflection.
Asynchronous network processing. is based on the epoll. The Epoll is divided into two modes. Horizontal triggering and edge triggering. AE uses a horizontal trigger, that is, once the data is available, the Epoll will always be notified until it is finished reading. The Edge trigger is only notified once.
Wait until the state changes to be notified.
Detailed access to the Internet.
1. Structure Source Code Analysis
1.1 Read-write event structure
/* File event structure */typedef struct aefileevent { int mask;/* One of Ae_ (readable| Writable) */ aefileproc *rfileproc; Aefileproc *wfileproc; void *clientdata;} Aefileevent;
The struct represents an FD corresponding event handler function and private data. When we want to register an FD time. The structure is populated.
1.2 Time Event Dog mention
/* Time Event structure */typedef struct Aetimeevent { long long ID;/* time event identifier. * /long when_sec; * seconds */ long When_ms;/* milliseconds */ aetimeproc *timeproc; Aeeventfinalizerproc *finalizerproc; void *clientdata; struct aetimeevent *next;} Aetimeevent;
When we register to handle events on a timed basis. is populated with the corresponding structure and added to the array.
1.3 Triggering of FD
/* A fired event */typedef struct aefiredevent { int fd; int mask;} Aefiredevent;
The struct represents a corresponding readable writable event for the FD
1.4 Total structure of AE events
/* State of a event based program */typedef struct Aeeventloop { int maxfd; /* Highest file descriptor currently registered */ int setsize;/* max number of file descriptors tracked */ long Long Timeeventnextid; time_t Lasttime; /* used to detect system clock skew */ aefileevent *events;/* Registered Events */ aefiredevent *fired;/* Fired Events */ aetimeevent *timeeventhead; int stop; void *apidata; /* This is used for polling API specific data */ aebeforesleepproc *beforesleep;} aeeventloop;
The structure stores the basic data for AE asynchronous events, such as the FD size. The time Event ID, the time pointer of the booklet, and so on.
2.ae_epoll interface
2.1 Epoll Structural Body
typedef struct AEAPISTATE { int epfd; struct epoll_event *events;} Aeapistate;
Provides a variable definition of epoll,
EPFD is created by Epoll_create. Events indicates how many epoll_wait agree to listen.
Fills the aeapistate structure body.
static int aeapicreate (Aeeventloop *eventloop)
Call epoll_wait, get the events we care about,
2. API interface
1. Create EventLoop
Aeeventloop *aecreateeventloop (int setsize)
2. Join the Event
int aecreatefileevent (aeeventloop *eventloop, int fd, int mask, aefileproc *proc, void *clientdata)
3.
Delete events,
void Aedeletefileevent (aeeventloop *eventloop, int fd, int mask)
4.
Create a Time Event
Long Long aecreatetimeevent (Aeeventloop *eventloop, long long milliseconds, aetimeproc *proc, void *clientdata, Aeeventfinalizerproc *finalizerproc)
5. Delete a Time event
int aedeletetimeevent (Aeeventloop *eventloop, long Long ID)
Using the Demo sample
Create loop
Proxy.eventloop = Aecreateeventloop (default_loop_size);
Creating Event Events
if (Aecreatetimeevent (Proxy.eventloop, 1, servercron, NULL, NULL) = = Ae_err)
{
printf ("Can ' t create the Servercron time event\n");
Exit (1);
}
/* Server listens for redisclient connections */
Aecreatefileevent (Proxy.eventloop, PROXY.SERVER_FD, ae_readable, on_client_connected, NULL);
Aecreatefileevent (Proxy.eventloop, proxy.evfd, ae_readable, Reconnect_redis, NULL);
The above is a simple demonstration sample.
I wrote a class of Redisprox, and I'll upload it to you.
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Asynchronous network architecture based on Redis AE