Details of file events and time events in redis, details of redis file events
Details of file events and time events in redis
The redis Server is an event driver. It can be divided into file events and time events. File events are the abstraction of the socket connection between the server and the client, and time events are classified into scheduled events and periodic events. Currently, redis only implements periodic events. redis file events are packaged through such functions as select/epoll. example: Associate the event to fdstatic int aeApiAddEvent (aeEventLoop * eventLoop, int fd, int mask) {aeApiState * state = eventLoop-> apidata; struct epoll_event through the following function; // according to fd. to determine whether to add or mod the int op = eventLoop-> events [fd]. mask = AE _NONE? EPOLL_CTL_ADD: EPOLL_CTL_MOD; // register the event to epoll ee. events = 0; mask | = eventLoop-> events [fd]. mask;/* Merge old events */if (mask & AE _READABLE) ee. events | = EPOLLIN; if (mask & AE _WRITABLE) ee. events | = EPOLLOUT; ee. data. u64 = 0;/* avoid valgrind warning */ee. data. fd = fd; // set event registration to kernel space if (epoll_ctl (state-> epfd, op, fd, & ee) =-1) return-1; return 0;} For example, use the following function to obtain the executable time static int aeApiP Oll (aeEventLoop * eventLoop, struct timeval * tvp) {aeApiState * state = eventLoop-> apidata; int retval, numevents = 0; // wait for the event ready retval = epoll_wait (state-> epfd, state-> events, eventLoop-> setsize, tvp? (Tvp-> TV _sec * 1000 + tvp-> TV _usec/1000):-1); // at least one event ready if (retval> 0) {int j; // We can see that there are only two types of file events: read and write. Here we will add these two events // to the fired array numevents = retval; for (j = 0; j <numevents; j ++) {int mask = 0; struct epoll_event * e = state-> events + j; if (e-> events & EPOLLIN) mask | = AE _READABLE; if (e-> events & EPOLLOUT) mask | = AE _WRITABLE; if (e-> events & EPOLLERR) mask | = AE _WRITABLE; if (e-> events & EPOLLHUP) mask | = AE _WRITABLE; eventLoop-> fired [j]. fd = e-> data. fd; eventLoop-> fired [j]. mask = mask ;}/// return the number of ready events return numevents;} the server generally only executes one periodic time event of servercron, file events and time events are executed in turn and are not preemptible. In general, time events are processed a little later than the set time.