本文详细介绍了redis源码的事件处理流程。
一、涉及的类图如下:
二、主要的结构体如下
/* State of an 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;
aeBeforeSleepProc *aftersleep;
} aeEventLoop;
struct redisServer {
dict *commands; /* Command table */
dict *orig_commands; /* Command table before command renaming. */
aeEventLoop *el;
typedef struct aeApiState {
int epfd;
struct epoll_event *events;
} aeApiState;
/* Global vars */
struct redisServer server; /* Server global state */
三、整个源码流程如下
1、添加事件和删除事件
static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
aeApiState *state = eventLoop->apidata;
epoll_ctl(state->epfd,op,fd,&ee)
aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask)
epoll_ctl(state->epfd,EPOLL_CTL_MOD,fd,&ee);
epoll_ctl(state->epfd,EPOLL_CTL_DEL,fd,&ee);
2、主函数
3 接收客户并处理客户的命令请求
4、写事件,发送消息