redis的socket event loop

时间:2021-08-22 15:01:59

很早之前就因为nosql就听说了redis,直到去年才真正去了解,只能说相见恨晚。

因为数据库相关,我以为这应该是个庞然大物,万万没想到,源码不到2M,所以,我不知道该说啥了。。。

还是来点靠谱的:

 /* 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

按照redis作者的排位,从上到下,性能是递减的,也就是evport>epoll>kqueue>select。libevent库上面有个benchmark,可能比较权威,http://libevent.org/ (那破图我能说点什么吗)

这个排位估计众说纷纭,对我来说,够用就好。

下面是redis定义的事件轮询函数原型:

 /* Prototypes */
aeEventLoop *aeCreateEventLoop(int setsize);
void aeDeleteEventLoop(aeEventLoop *eventLoop);
void aeStop(aeEventLoop *eventLoop);
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData);
void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
aeTimeProc *proc, void *clientData,
aeEventFinalizerProc *finalizerProc);
int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
int aeProcessEvents(aeEventLoop *eventLoop, int flags);
int aeWait(int fd, int mask, long long milliseconds);
void aeMain(aeEventLoop *eventLoop);
char *aeGetApiName(void);
void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
int aeGetSetSize(aeEventLoop *eventLoop);
int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);

看到这个让我想起了大概是2007年微软推出mvc三层架构里的数据库访问层,封装各类数据库api,来实现一个抽象统一的数据访问中间层,

那么,在这里对不同socket IO复用实现也进行了统一的封装。或者很类似设计模式里的bridge模式。

多的我也不说了,参考大神的博客:

http://www.kegel.com/c10k.html

http://pl.atyp.us/content/tech/servers.html