欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

redis源码阅读:事件处理流程及源码分析

程序员文章站 2022-05-19 16:13:45
...

   本文详细介绍了redis源码的事件处理流程。

一、涉及的类图如下:

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、主函数

redis源码阅读:事件处理流程及源码分析

3 接收客户并处理客户的命令请求

redis源码阅读:事件处理流程及源码分析

4、写事件,发送消息

redis源码阅读:事件处理流程及源码分析

相关标签: redis 事件处理