当前位置: 首页 > news >正文

网站是用dreamer做的_为什么后台能进去前台进不去了/杭州网站优化效果

网站是用dreamer做的_为什么后台能进去前台进不去了,杭州网站优化效果,网站降权不收录,深圳宝安商城网站建设公司redis 服务底层采用了异步事件管理(aeEventLoop):管理时间事件和文件事件。对于大量网络文件描述符(fd)的事件管理,redis 建立在安装系统对应的事件驱动基础上(例如 Linux 的 epoll)…

redis 服务底层采用了异步事件管理(aeEventLoop):管理时间事件和文件事件。对于大量网络文件描述符(fd)的事件管理,redis 建立在安装系统对应的事件驱动基础上(例如 Linux 的 epoll)。

  • 关于事件驱动,本章主要讲述 Linux 系统的 epoll 事件驱动。
  • 关于事件处理,本章主要讲述文件事件,时间事件可以参考帖子 [redis 源码走读] 事件 - 定时器。

🔥文章来源:wenfh2020.com

1. 事件驱动

redis 根据安装系统选择对应的事件驱动。

// ae.c
/* Include the best multiplexing layer supported by this system.* The following should be ordered by performances, descending. */
#ifdef HAVE_EVPORT
#include "ae_evport.c"
#else#ifdef HAVE_EPOLL#include "ae_epoll.c"#else#ifdef HAVE_KQUEUE#include "ae_kqueue.c"#else#include "ae_select.c"#endif#endif
#endif

2. 异步事件管理

epoll 是异步事件驱动,上层逻辑操作和下层事件驱动要通过 fd 文件描述符串联起来。异步事件管理(aeEventLoop),对 epoll 做了一些封装,方便异步事件回调处理。

有关 epoll 工作流程,可以参考我的帖子:epoll 多路复用 I/O工作流程

redis 文件事件封装

层次描述
ae.c关联异步业务事件和 epoll 接口,处理 fd 对应事件逻辑。
ae_epoll.c对 epoll 接口进行封装,方便上层操作。
epollLinux 内核多路复用 I/O 模型,主要为了高效处理大批量文件描述符事件。

2.1. 数据结构

// ae.c// 文件事件结构
typedef struct aeFileEvent {int mask; // 事件类型组合(one of AE_(READABLE|WRITABLE|BARRIER))aeFileProc *rfileProc; // 读事件回调操作。aeFileProc *wfileProc; // 写事件回调操作。void *clientData;      // 业务传入的私有数据。方便回调使用。
} aeFileEvent;// 就绪事件
typedef struct aeFiredEvent {int fd;   // 文件描述符。int mask; // 事件类型组合。
} aeFiredEvent;// 事件管理结构
typedef struct aeEventLoop {int maxfd;   // 监控的最大文件描述符。int setsize; // 处理文件描述符个数。...aeFileEvent *events; // 根据 fd 监听事件。aeFiredEvent *fired; // 从内核取出的就绪事件。...
} aeEventLoop;
结构描述
aeEventLoop文件事件和时间事件管理。
aeFileEvent文件事件结构,方便异步回调逻辑调用。aeEventLoop 会创建一个 aeFileEvent 数组,数组下标是 fd,fd 对应 aeFileEvent 数据结构。
aeFiredEvent从内核获取的就绪事件。(例如 Linux 系统通过 epoll_wait 接口获取就绪事件,每个事件分别存储在 aeFiredEvent 数组中)

2.2. 创建事件管理对象

创建事件管理对象,对监控的文件数量设置了上限。

  • 文件监控上限配置。
# redis.conf
#
# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
# maxclients 10000
  • 创建事件管理对象。
#define CONFIG_MIN_RESERVED_FDS 32
#define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96)// server.c
void initServer(void) {...server.el = aeCreateEventLoop(server.maxclients+CONFIG_FDSET_INCR);...
}int main(int argc, char **argv) {...initServer();...
}

2.3. 事件处理流程

  • 循环处理事件
// server.c
int main(int argc, char **argv) {...aeMain(server.el);...
}// ae.c
// 循环处理事件
void aeMain(aeEventLoop *eventLoop) {eventLoop->stop = 0;while (!eventLoop->stop) {if (eventLoop->beforesleep != NULL)eventLoop->beforesleep(eventLoop);aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_AFTER_SLEEP);}
}
  • 添加事件,关联 fd 事件与异步回调相关信息。
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,aeFileProc *proc, void *clientData) {if (fd >= eventLoop->setsize) {errno = ERANGE;return AE_ERR;}aeFileEvent *fe = &eventLoop->events[fd];// 调用底层 epoll_ctl 注册事件。if (aeApiAddEvent(eventLoop, fd, mask) == -1)return AE_ERR;fe->mask |= mask;if (mask & AE_READABLE) fe->rfileProc = proc;if (mask & AE_WRITABLE) fe->wfileProc = proc;fe->clientData = clientData;if (fd > eventLoop->maxfd)eventLoop->maxfd = fd;return AE_OK;
}
  • 删除事件,删除对应 fd 的事件。
void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask) {if (fd >= eventLoop->setsize) return;aeFileEvent *fe = &eventLoop->events[fd];if (fe->mask == AE_NONE) return;// 如果删除的是写事件,要把写事件优先处理的事件也去掉,恢复优先处理读事件,再处理写事件逻辑。if (mask & AE_WRITABLE) mask |= AE_BARRIER;// 调用底层 epoll_ctl 修改删除事件。aeApiDelEvent(eventLoop, fd, mask);fe->mask = fe->mask & (~mask);if (fd == eventLoop->maxfd && fe->mask == AE_NONE) {/* Update the max fd */int j;for (j = eventLoop->maxfd-1; j >= 0; j--)if (eventLoop->events[j].mask != AE_NONE) break;eventLoop->maxfd = j;}
}

2.4. 事件处理逻辑

文件事件处理逻辑,从内核取出就绪事件,根据事件的读写类型,分别进行回调处理相关业务逻辑。

// ae.c
int aeProcessEvents(aeEventLoop *eventLoop, int flags) {...// 多路复用接口,从内核取出就绪事件。numevents = aeApiPoll(eventLoop, tvp);...for (j = 0; j < numevents; j++) {// 根据就绪事件 fd,取出对应的异步文件事件进行逻辑处理。aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];int mask = eventLoop->fired[j].mask;int fd = eventLoop->fired[j].fd;int fired = 0; /* Number of events fired for current fd. *//* AE_BARRIER 表示优先可写事件。正常情况,一般先读后写。* AE_BARRIER 使用场景,有兴趣的朋友,可以查找源码关键字:CONN_FLAG_WRITE_BARRIER* 理解这部分的逻辑。 */int invert = fe->mask & AE_BARRIER;if (!invert && fe->mask & mask & AE_READABLE) {fe->rfileProc(eventLoop,fd,fe->clientData,mask);fired++;}if (fe->mask & mask & AE_WRITABLE) {if (!fired || fe->wfileProc != fe->rfileProc) {fe->wfileProc(eventLoop,fd,fe->clientData,mask);fired++;}}if (invert && fe->mask & mask & AE_READABLE) {if (!fired || fe->wfileProc != fe->rfileProc) {fe->rfileProc(eventLoop,fd,fe->clientData,mask);fired++;}}...}...
}

2.5. 获取待处理事件

通过 epoll_wait 从系统内核取出就绪文件事件进行处理。

// ae_epoll.c
static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {aeApiState *state = eventLoop->apidata;int retval, numevents = 0;// 从内核取出就绪文件事件进行处理。retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);if (retval > 0) {int j;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|AE_READABLE;if (e->events & EPOLLHUP) mask |= AE_WRITABLE|AE_READABLE;// 就绪事件和fd保存到 fired。eventLoop->fired[j].fd = e->data.fd;eventLoop->fired[j].mask = mask;}}return numevents;
}

3. 总结

  • redis 没有使用第三方库,实现跨平台的异步事件驱动。对文件事件驱动封装也比较简洁高效。

4. 参考

  • 用 gdb 调试 redis
  • UML类图与类的关系详解
  • 《redis 设计与实现》
  • Redis 多线程的 Redis
http://www.jmfq.cn/news/4901887.html

相关文章:

  • 做网站是什么鬼/怎么把网站排名排上去
  • 集团网站建设流程/产品经理培训
  • 网站建设对图片有哪些要求/最近一周的国内新闻
  • 影响网站排名重要因素/网站关键词快速优化
  • wordpress插件包/朝阳区搜索优化seosem
  • 网站如何盈利/成都今天重大新闻事件
  • 重庆家居网站制作公司/天津优化公司哪家好
  • 临沂外贸网站建设/seo公司推荐推广平台
  • 胖咯科技 网站建设/全网营销系统1700元真实吗
  • 今日财经重大新闻/seo优化专员工作内容
  • 企业内网网站/备案域名交易平台
  • 安徽电子工程学校/企业网站seo排名优化
  • 建网站的工具/河北网站建设公司排名
  • 郑州哪里有做网站/谷粉搜索谷歌搜索
  • 一个小型网站设计/软文范例
  • 淄博天一建设项目招标代理有限公司网站/百度网首页登录入口
  • 网站策划怎么样/软文范文200字
  • 湖南专业做网站公司/南宁seo收费
  • 群晖安装wordpress/seo公司服务
  • 我的世界做壁纸的网站/seo排名优化培训网站
  • 建立网站可以赚钱吗?/百度搜索热度指数
  • 广州市增城区建设局网站/seo扣费系统源码
  • 专业群建设 网站/小广告网站
  • 国外做外贸哪个网站好些/今天的新闻有哪些
  • 智慧团建网站什么时候维护好/seo免费优化网站
  • 建网站首选公司/百度导航怎么下载
  • 旅游网站管理系统论文/淄博网站优化
  • 什么是网络营销?网络营销有哪些特点?/宁波seo网络推广代理公司
  • 怎么用自己电脑做服务器发布网站吗/城市更新论坛破圈
  • vps 香港/seo怎么收费