当前位置:网站首页>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
边栏推荐
- Hundreds of people participated. What are these people talking about in the opengauss open source community?
- I drew a Gu ailing with characters!
- Minio 安装与使用
- User management - restrictions
- How to uninstall -- Qianxin secure terminal management system
- Installation and use of Supervisor
- Day6 --- Sqlalchemy advanced
- Installation and use of beef XSS
- 如何在qsim查看软件对象的实例?
- Implementation of queue (sequential storage, chain storage)
猜你喜欢

面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?

Chapter 2 foreground data display

第2章 前台数据展现

Login to homepage function implementation

Block, there is a gap between the block elements in the row

Minio 安装与使用

On Valentine's day, I drew an object with characters!

Use of flask

Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?

General Administration of Customs: the import of such products is suspended
随机推荐
General Administration of Customs: the import of such products is suspended
Arm undefined instruction exception assembly
Day6 --- Sqlalchemy advanced
Installation and use of beef XSS
3428. 放苹果
JS advanced knowledge - function
低成本、低门槛、易部署,4800+万户中小企业数字化转型新选择
List删除集合元素
List delete collection elements
Forced login, seven cattle cloud upload pictures
面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
redis的string类型及bitmap
Help send some recruitment. If you are interested, you can have a look
Realization of background channel group management function
while Loop
Is it safe to buy funds every day? Online and other answers
Pass parameters and returned responses of flask
Chapter 2 foreground data display
691. 立方体IV
Flink1.15 source code reading Flink clients client execution process (reading is boring)