当前位置:网站首页>Redis network IO
Redis network IO
2022-07-27 08:42:00 【Mangxiao】
redis The Internet IO
Come to the point ,redis Use it directly IO In the multiplexing model epoll Model implementation and kernel event delivery .
How to realize this process , Analyze slowly
epoll Source code linux
https://editor.csdn.net/md/?articleId=121450854
initServer Stage
1. establish epoll Model examples
establish aeEventLoop server.el = aeCreateEventLoop(server.maxclients+CONFIG_FDSET_INCR)
aeApiCreate(eventLoop)
state->epfd = epoll_create(1024) establish epoll example
epoll_create
2. Listening port listenToPort
3. Add file event epoll_ctl take fd Parameters are passed to the kernel
if (aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE, acceptTcpHandler,NULL) == AE_ERR)
if (aeApiAddEvent(eventLoop, fd, mask) == -1) return AE_ERR
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
aeFileProc *proc, void *clientData)
{
Parameters :
aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE, acceptTcpHandler,NULL)
- server.el
- server.ipfd[j]
- AE_READABLE Here is a readable event , So when readable events come , Will pass acceptTcpHandler Connect the processing transponder for processing
- acceptTcpHandler

aeFileProc Event handler
aeFileProc The function pointer is defined as follows :
typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
stay c In language , Callback is realized through function pointer . By adding the callback function address Pass to Modulated function , So as to realize callback . ad locum , By defining function pointers aeFileProc, The caller implements the specific function content , In the actual calling function , hold aeFileProc The address of the implementation function is passed in . In fact, it is equivalent to defining an interface , The caller implements the interface
aeApiAddEvent

static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
aeApiState *state = eventLoop->apidata;
struct epoll_event ee = {
0}; /* avoid valgrind warning */
/* If the fd was already monitored for some event, we need a MOD * operation. Otherwise we need an ADD operation. */
* there op Means add event Or modify the existing event, Because every time I pass epoll_ctl Not only to the kernel , It's an action ,epollctl There are only three supported actions
* EPOLL_CTL_ADD: Indicates that you want to add .
* EPOLL_CTL_DEL: Indicates to delete .
* EPOLL_CTL_MOD: It means to modify .
int op = eventLoop->events[fd].mask == AE_NONE ?
EPOLL_CTL_ADD : EPOLL_CTL_MOD;
ee.events = 0;
mask |= eventLoop->events[fd].mask; /* Merge old events */
// Monitor read and write events , If mask For readability , Set up ee.events |= EPOLLIN; 0 Or with any binary All binary bits are equivalent to ee.events = EPOLLIN
if (mask & AE_READABLE) ee.events |= EPOLLIN;
if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;
//# To monitor fd
ee.data.fd = fd;
// Set the front epfd/op/fd/ee adopt epoll_ctl Set to kernel space
if (epoll_ctl(state->epfd,op,fd,&ee) == -1) return -1;
return 0;
}
aeEventLoop start-up epoll_wait
aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_AFTER_SLEEP)
numevents = aeApiPoll(eventLoop, tvp);
aeApiPoll

3: Finally through epoll_wait To wait for event The occurrence of time .
static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
aeApiState *state = eventLoop->apidata;
int retval, numevents = 0;
# adopt epoll_wait wait for epfd The occurrence of the incident , There are settings timeout Time for , Within the specified time epoll_wait Must return
retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,
tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
#retval Greater than 0 It means that there is event It happened. , If it is timeout Return rule retval be equal to -1
if (retval > 0) {
int j;
numevents = retval;
# Traverse state->events Data to find out whether to read or write event
for (j = 0; j < numevents; j++) {
int mask = 0;
struct epoll_event *e = state->events+j;
# according to mask Judge whether to read or write events
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;
# Keep happening event Of fd And preservation fd For the mask
eventLoop->fired[j].fd = e->data.fd;
eventLoop->fired[j].mask = mask;
}
}
return numevents;
}
————————————————
Copyright notice : This paper is about CSDN Blogger 「tiantao2012」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/tiantao2012/article/details/79527715
边栏推荐
- How to upload qiniu cloud
- How to permanently set source
- Implementation of registration function
- Using ecological power, opengauss breaks through the performance bottleneck
- After downloading URL loader and specifying the size of the image with limit, the image will not be displayed
- Installation and use of beef XSS
- Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
- 永久设置source的方法
- 网络IO总结文
- JS basic knowledge - daily learning summary ①
猜你喜欢
随机推荐
JWT authentication and login function implementation, exit login
微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?
Linear list (sequential storage, chain storage) (linked list of leading nodes, linked list of non leading nodes)
好吃难吃饱七分为宜;好喝难喝醉三分为佳
How to upload qiniu cloud
[uni app advanced practice] take you hand-in-hand to learn the development of a purely practical complex project 1/100
JS rotation chart
4276. 擅长C
Node installation and debugging
Transaction, order system add transaction
CMD command and NPM command
借生态力量,openGauss突破性能瓶颈
【nonebot2】几个简单的机器人模块(一言+彩虹屁+每日60s)
4279. 笛卡尔树
Help send some recruitment. If you are interested, you can have a look
Make a game by yourself with pyGame 01
【渗透测试工具分享】【dnslog服务器搭建指导】
UVM Introduction Experiment 1
Include error in vs Code (new header file)
Have a good laugh









