当前位置:网站首页>Libuv Library - Design Overview (Chinese version)
Libuv Library - Design Overview (Chinese version)
2022-07-03 13:29:00 【ufgnix0802】
Libuv library - Design Overview
The following is a libuv Official website Design overview Chinese translation of .
http://docs.libuv.org/en/v1.x/design.html
libuv It is a cross platform support library , Originally, it was just for Node.js And write . It is asynchronous around event driven I/O Designed for modeling .
The library provides more than just in different I/O Simple abstraction on polling mechanism , and ”handles“ and ”stream“ The appearance of is sockets And other entities provide a more advanced abstraction . There are also cross platform I/O More functions such as file and thread functions .
Here is a chart , Explain the composition libuv And their relationship with the corresponding subsystems :

TIPS:
The following is an explanation of the above chart , Reprinted to :https://toutiao.io/posts/9ucbgr/preview
It is divided into two parts from left to right , Part is with NetWork I/O Related requests , The other part is made up of FIle I/O、DNS Ops as well as User code Composed request . As you can see from the diagram , about NetWork I/O and File I/O For each type of request represented , The underlying support mechanism of asynchronous processing is completely different . about NetWork I/O Related requests , according to OS Different platforms , The underlying support mechanism of different pairs , Such as Linux Upper use epoll,OSX and BSD Class system uses kqueue,SunOS It uses event ports as well as Windows It uses IOCP Mechanism . And for File I/O For the representative's request , Then use Thread pool. utilize Thread pool To realize asynchronous request processing , In all kinds of OS Can get good support on .
In the article :https://blog.libtorrent.org/2012/10/asynchronous-disk-io/ For disk I/O,Libuv Why should the team choose thread pool The mechanism of . Basically, the reason is that the coding and maintenance complexity is too high 、 Supportable API Too few and poor quality 、 Weak technical support , While using thread pool These problems are well avoided . This is a very interesting article , It can be seen from it that libuv At that time, the team made various researches and attempts to achieve cross platform asynchronous compatibility .
Handles and requests
libuv combination event loop Two abstractions are provided for users , Namely handles and requests.
Handles Represents the long-term survival object , Be able to handle specified events during active periods . such as :
- Every time an event is polled , Active prepare handle All callback events will be called .
- Every time a new connection comes ,TCP Server side handle The connection request callback will be called .
requests Usually refers to short-term survival operation . These operations are all through handle Realized ,write requests It's through handle To write data , Or no handle You can also write data .getaddrinfo requests Don't need to, handle, It runs directly on loop On .
I/O polling
I/O( Or events ) Polling is libuv The core of . It constitutes all I/O operation , It means that it is single threaded .libuv Yes, you can run multiple event loop Of , Just for each event loop Just create a thread ( That is, multiple event polling means multithreading ).libuv The event loop of ( It mainly refers to polling and handle dependent API) yes Thread unsafe Of . Unless otherwise stated .
This event loop It follows the very popular single thread asynchronous I/O Model : be-all ( The Internet )I/O Are based on non blocking socket To achieve , And these socket Polling is done using the best mechanism available on a given platform , such as Linux On the platform epoll Model 、OSX and BSD Class system uses kqueue、SunOS The system uses event ports and Windows The platform uses IOCP. As part of event polling , The loop will block waiting socket I/O Incident . Has been added to the polling mechanism socket I/O The callback event of the event will be in socket Conditions ( Can be read / Writable condition ) Called when triggered , Thus making handle Able to read and write data , Or perform I/O operation .
In order to better understand the operation mechanism of event polling , The following figure illustrates all stages of circular polling :

- to update loop The timestamp . At the beginning of each event poll ,loop Will cache the current time . This is to reduce the number of time-dependent system calls .
- If loop Still active , So let's continue polling , Otherwise we exit polling . So how to judge loop Be active ? as long as loop There are also active and quoted handle、 Active request、 Closing handle, It's thought that loop Still active .
- All expired timers are executed . All the time has passed loop Do not call the corresponding callback event in the active timer of the timestamp .
- pending The callback event of is called . Most of the I/O The callback event is in I/O Called immediately after polling ; But those who need to delay to the next round of event polling in the last event polling I/O Callback , That's when it's called .
- idle handle The callback event of is called . In each event poll , Active idle handle Will be run .
- prepare handle The callback event of is called .prepare handle The callback of is in I/O Called before polling .
- Calculate the timeout of polling . Blocking I/O Before ,loop You need to calculate how long it should block . The rules for calculating the timeout are as follows :
- Timeout set to 0, If loop In order to UV_RUN_NOWAIT mode .
- If loop Has stopped ( call uv_loop() event ). Timeout set to 0.
- If there is no active handle perhaps request. Timeout set to 0.
- If there is a need to delay closing handle. Timeout set to 0.
- besides , If there is an active timer , The timeout is set to the time closest to the timer , Otherwise, the timeout will be infinite .
- I/O Of loop modular . here , Within the duration calculated in the previous step , The cycle will be blocked during this time . In the process , All that are monitoring the given file descriptor I/O dependent handles Will call their callback events at this time .
- Call check handle The callback event for . Check handle The callback event of is in I/O Called after polling .check handle and prepare handle formal loop The equivalent part of .
- The close callback is called . If handle The shutdown is due to the call uv_close(), Then the close callback event will be called .
- In particular , If loop In order to UV_RUN_ONCE Mode for polling , stay I/O After polling, there may not be I/O Callback called . But the callback event of the timeout timer will be called .
- End of poll , If loop The mode of operation is UV_RUN_NOWAIT perhaps UV_RUN_ONCE, So at this time uv_run() It will return the result , If loop The mode of operation is UV_RUN_DEFAULT, Then the next poll will take place .
TIPS:libuv Use thread pools to make asynchronous files I/O Operation becomes possible , But the Internet I/O Throughout The thread that executes each loop in a single thread .
Be careful , Although different platforms have different polling mechanisms , however libuv stay Unix and Windows The system maintains a consistent operating mode .
file I/O
And NetWork I/O Different ,libuv There are no platform related files I/O Mechanisms can depend on , So the current method is to deal with blocking in the process pool I/O File operations .
Want to understand cross platform I/O Design of documents , May refer to :https://blog.libtorrent.org/2012/10/asynchronous-disk-io/
libuv Currently, a global thread pool is used , All loops can be queued . At present, there are three types of operations running on the thread pool :
- File system operations .
- DNS function (getaddrinfo and getnameinfo)
- The code written by the user passes uv_queue_work() function .
边栏推荐
- Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
- Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
- This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
- Sword finger offer 14- I. cut rope
- Servlet
- Swiftui development experience: the five most powerful principles that a programmer needs to master
- Some thoughts on business
- MySQL constraints
- Introduction to the implementation principle of rxjs observable filter operator
- Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
猜你喜欢

Sword finger offer 12 Path in matrix
![[sort] bucket sort](/img/52/95514b5a70cea75821883e016d8adf.jpg)
[sort] bucket sort

18W word Flink SQL God Road manual, born in the sky

User and group command exercises
![[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]](/img/47/78d9dd098dcb894ba1f459873d5f52.png)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]

This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version

Road construction issues
![[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]](/img/b4/3442c62586306b4fceca992ce6294a.png)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]

CVPR 2022 | 美团技术团队精选6篇优秀论文解读

Logseq 评测:优点、缺点、评价、学习教程
随机推荐
R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
February 14, 2022, incluxdb survey - mind map
[sort] bucket sort
The 35 required questions in MySQL interview are illustrated, which is too easy to understand
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
Sword finger offer 14- ii Cut rope II
【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
elk笔记24--用gohangout替代logstash消费日志
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
Setting up Oracle datagurd environment
Red Hat Satellite 6:更好地管理服务器和云
Multi table query of MySQL - multi table relationship and related exercises
Servlet
Logback 日志框架
rxjs Observable filter Operator 的实现原理介绍
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】