当前位置:网站首页>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
边栏推荐
- 【mysql学习08】
- Use three.js to realize the cool cyberpunk style 3D digital earth large screen
- Web APIs(获取元素 事件基础 操作元素)
- 【IMX6ULL笔记】--内核底层驱动初步探究
- JVM性能调优方法
- How to judge the performance of static code quality analysis tools? These five factors must be considered
- [electronic device notes 5] diode parameters and selection
- 【mysql学习09】
- 各种控件==PYQT5
- 贪心问题01_活动安排问题
猜你喜欢

【电子器件笔记5】二极管参数和选型

Introduction to shortcut keys in debug chapter

第4章线性方程组

任何时间,任何地点,超级侦探,认真办案!

Information management system for typical works of urban sculpture (picture sharing system SSM)

JS process control

Fillet big killer, use filter to build fillet and wave effect!

Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees

JS data types and mutual conversion

Leetcode sword finger offer 27. image of binary tree
随机推荐
SQL injection LESS18 (header injection + error injection)
RedisUtil
JS operator
什么是全局事件总线?
WIZnet W5500系列培训活动之“MQTT协议讲解和实践(接入OneNET)”
Use three.js to realize the cool cyberpunk style 3D digital earth large screen
[leetcode brush questions]
相似矩阵,可对角化条件
JS数据类型以及相互转换
Leetcode sword finger offer 28. symmetric binary tree
JS process control
Classification parameter stack of JS common built-in object data types
cookie and session
教你如何通过MCU配置S2E为TCP Server的工作模式
【mysql学习08】
flinksql client 连接kafka select * from table没有数据报错,如何解决?
return 和 finally的执行顺序 ?各位大佬请看过来,
Menu bar + status bar + toolbar ==pyqt5
第4章线性方程组
11. Reading rumors spread with deep learning