当前位置:网站首页>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
边栏推荐
- 信号与槽机制==PYQT5
- Introduction to shortcut keys in debug chapter
- Chapter 4 linear equations
- JS operator
- 论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
- 基于W5500实现的考勤系统
- 【USB设备设计】--复合设备,双HID高速(64Byte 和 1024Byte)
- JS data types and mutual conversion
- Summary of combination problems of Li Kou brush questions (backtracking)
- Information management system for typical works of urban sculpture (picture sharing system SSM)
猜你喜欢
随机推荐
【mysql学习09】
Esp8266 uses drv8833 drive board to drive N20 motor
JS scope and pre parsing
第4章线性方程组
JDBC summary
Various controls ==pyqt5
ESP8266 使用 DRV8833驱动板驱动N20电机
Emmet syntax quick query syntax basic syntax part
Plot ==pyqt5
工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
SQL language (V)
SQL injection less23 (filter comment)
活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
Small program of vegetable distribution in community
JVM performance tuning methods
MIIdock简述
W5500上传温湿度到oneNET平台
动态规划问题03_最大子段和
Teach you how to configure S2E as the working mode of TCP client through MCU
大话DevOps监控,团队如何选择监控工具?









