当前位置:网站首页>Brpc source code analysis (VIII) -- detailed explanation of the basic class eventdispatcher
Brpc source code analysis (VIII) -- detailed explanation of the basic class eventdispatcher
2022-07-25 11:51:00 【wxj1992】
Catalog
I was busy a while ago , It hasn't been updated for a long time , Strive to restore the update frequency in the future , This time, I'll start with a relatively simple , introduce EventDispatcher class , The previous article dealt with this concept , EventDispatcher, seeing the name of a thing one thinks of its function , It is used to distribute events , stay brpc Inside , Because there is no distinction io Threads and user threads , This EventDispatcher, It is only used for event distribution , Not responsible for specific data reading and writing , So its throughput can be very large . This class is important , But itself is relatively simple , Here are the core functions and class variables .
1. Start related functions
(1)void Run()
Core function , call epoll_wait Wait for the event and call the function to process ,epoll_in Call after the event Socket:: StartInputEvent,epoll_out Call after the event Socket::HandleEpollOut,Socket:: StartInputEvent and Socket::HandleEpollOut It is to execute the corresponding socket The corresponding user processing function in . The core code is as follows :
(2)static void* RunThis(void* arg);
Encapsulates the Run, Used in bthread Start the call in Run.
(3)virtual int Start(const bthread_attr_t* consumer_thread_attr);
newly build bthread With RunThis Start the current EventDispatcher, Start listening epoll Event and distribute .
2. add to epoll_in Event function
int AddConsumer(SocketId socket_id, int fd)
stay fd Add epoll_in event ( Can read the event ) monitor , Parameters socket_id Will be saved to the added event , The following events happened, which need to be based on this socket_id address Actually socket, Take the inside _on_edge_triggered_events Wait for actual processing , To sum up, call this function to add the required socket_id Corresponding socket To deal with occurs in fd The edge on triggers epoll_in event . What is called directly after the event is Socket:: StartInputEvent.
3. add to epoll_out Event function
AddEpollOut(SocketId socket_id, int fd, bool pollin)
Add listening epoll_out event ( Can write event ), And add epollin In a similar way ,epoll_out event , One more. pollin Parameters , If true They'll listen at the same time epoll_in, What is called directly after the event is Socket::HandleEpollOut, For example, the previous article mentioned ,brpc It's going somewhere socket When writing data in , If it is not connected yet, initiate the connection and use “ Continue to write the function ” Register as a callback epollout Back directly , Let the callback complete the subsequent write operation .
4. obtain EventDispatcher function
Brpc Support multiple EventDispatcher, Specifically EventDispatcher The quantity is determined by the parameters , The default quantity is 1, Every EventDispatcher Responsible for part of fd Monitoring and processing of .
each fd All calls GetGlobalEventDispatcher To get the corresponding EventDispatcher, This function is right here event_dispatcher.cpp in ,EventDispatcher Out of class brpc namespace The next function , as follows :
If it is not initialized, it will be initialized and run first ,InitializeGlobalDispatchers Is a specific initialization function , from pthread_once Promise to do it only once . This is based on MurmurHash3 take fd Distribute evenly to each EventDispatcher.InitializeGlobalDispatchers Function as follows :
5. Core class variables
(1)int _epfd: Monitoring events epfd
(2)bthread_t _tid:EventDispatcher Current bthread Of id
(3)bthread_attr_t _consumer_thread_attr:epoll_in perhaps epoll_out New after the event bthread Thread properties used to execute user callback functions
边栏推荐
- Maskgae: masked graph modeling meets graph autoencoders
- JaveScript循环
- Flinksql client connection Kafka select * from table has no data error, how to solve it?
- return 和 finally的执行顺序 ?各位大佬请看过来,
- 圆角大杀器,使用滤镜构建圆角及波浪效果!
- Teach you how to configure S2E as the working mode of TCP server through MCU
- Leetcode sword finger offer 27. image of binary tree
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- JS中的对象
- Similarity matrix, diagonalization condition
猜你喜欢

论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》

SQL language (6)

Talking about Devops monitoring, how does the team choose monitoring tools?

矩阵的特征值和特征向量

Small and micro enterprise smart business card management applet

Maskgae: masked graph modeling meets graph autoencoders

小微企业智能名片管理小程序

Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)

Filter过滤器解决request请求参数乱码的原理解析

教你如何通过MCU将S2E配置为UDP的工作模式
随机推荐
圆角大杀器,使用滤镜构建圆角及波浪效果!
Emmet syntax quick query syntax basic syntax part
贪心问题01_活动安排问题
Small program of vegetable distribution in community
cookie and session
动态规划问题05_导弹拦截
教你如何通过MCU配置S2E为TCP Server的工作模式
SQL injection less23 (filter comment)
Use three.js to realize the cool cyberpunk style 3D digital earth large screen
Brief description of model deployment
Reinforcement learning (IV)
Risks in software testing phase
Onenet platform control w5500 development board LED light
JS运算符
Oil monkey script link
信号与槽机制==PYQT5
return 和 finally的执行顺序 ?各位大佬请看过来,
小区蔬菜配送的小程序
如何解决“W5500芯片在TCP_Client模式下,断电重启之后无法立即连接到服务器”的问题
Common linear modulation methods based on MATLAB