当前位置: 首页 > news >正文

rust-tokio发布考古

源头: Carl Lerche  Aug 4, 2016

​ I’m very excited to announce a project that has been a long time in the making.
我很兴奋地宣布一个酝酿已久的项目。

Tokio is a network application framework for rapid development and highly scalable deployments of clients and servers in the Rust programming language. ​

Tokio 是一个网络应用框架,用于Rust编程语言中客户端和服务器的快速开发和高度可扩展的部署。

It strives to make writing robust, scalable, and production ready network clients and servers as easy as possible. It does this by focusing on small and reusable components… and by being really, really, fast.

它致力于尽可能轻松地编写健壮、可扩展且可用于生产的网络客户端和服务器。 它通过专注于小型且可重复使用的组件来实现这一点……并且非常非常快。

GitHub - tokio-rs/tokio: A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...

Let’s start our tour of Tokio.
让我们开始我们的Tokio之旅。

Service Trait 服务特点

​Writing composable and reusable components is made possible by standardizing the interface that clients and servers use. In Tokio, this standardized interface is the `Service` trait and it is the cornerstone of Tokio’s building blocks. This trait is heavily inspired (Ok, stolen) from Finagle. ​
通过标准化客户端和服务器使用的接口,可编写可组合和可重用的组件。 在 Tokio 中,这个标准化接口是“Service”特征,它是 Tokio 构建块的基石。 这个特性很大程度上受到 Finagle 的启发(好吧,是偷来的)。

Here is what the Service trait looks like:
下面是Service trait的样子:

pub trait Service: Send + 'static {type Req: Send + 'static;type Resp: Send + 'static;type Error: Send + 'static;type Fut: Future<Item = Self::Resp, Error = Self::Error>;fn call(&self, req: Self::Req) -> Self::Fut;
}

A relatively simple trait, but it unlocks a world of possibility. This symmetric and uniform API reduces your server or client into a function, thus enabling middleware (or filters, depending on your lingo).

这是一个相对简单的特征,但它开启了一个充满可能性的世界。 这种对称且统一的 API 将您的服务器或客户端简化为一个函数,从而启用中间件(或过滤器,具体取决于您的常用术语)。

A Service is an asynchronous function from Request to Response, where asynchronicity is managed by the brilliant Futures crate.
服务是从请求到响应的异步功能,其中的异步性由出色的Futures crate管理。

A middleware is a component that acts both as a client and a server. It intercepts a request, modifies it, and passes it on to an upstream Service value.

中间件是一个既当客户端又当服务器的组件。 它拦截请求,修改请求,并将其传递给上游Service值。

Let us use timeouts as an example, something every network application requires.
让我们以超时为例,这是每个网络应用程序都需要的。

pub struct Timeout<T> {upstream: T,delay: Duration,timer: Timer,
}impl<T> Timeout<T> {pub fn new(upstream: T, delay: Duration) -> Timeout<T> {Timeout {upstream: upstream,delay: delay,timer: Timer::default(),}}
}impl<T> Service for Timeout<T>where T: Service,T::Error: From<Expired>,
{type Req = T::Req;type Resp = T::Resp;type Error = T::Error;type Fut = Box<Future<Item = Self::Resp, Error = Self::Error>>;fn call(&self, req: Self::Req) -> Self::Fut {// Get a future representing the timeout and map it to the Service error typelet timeout = self.timer.timeout(self.delay).and_then(|timeout| Err(Self::Error::from(timeout)));// Call the upstream service, passing along the requestself.upstream.call(req)// Wait for either the upstream response or the timeout to happen.select(timeout)// Map the result of the select back to the expected Response type.map(|(v, _)| v).map_err(|(e, _)| e).boxed()}
}

This Timeout middleware, shown above, only has to be written once, then it can be inserted as part of any network client for any protocol, as shown below.

如上所示,这个超时中间件只需编写一次,然后它就可以作为任何协议的任何网络客户端的一部分插入,如下所示。

let client = Timeout::new(http::Client::new(),Duration::from_secs(60));client.call(http::Request::get("https://www.rust-lang.org")).and_then(|response| {// Process the responseOk(())}).forget(); // Process the future

Now, the really interesting thing is that the exact same middleware could be used in a server as well, shown below.
现在,真正有趣的事情是,完全相同的中间件也可以在服务器中使用,如下所示。

http::Server::new().serve(Timeout::new(MyService, Duration::from_secs(60)));

Tokio’s Reactor Tokio’s 的Reactor 

The Service trait addresses how all the various network services fit together to build one cohesive application, but it doesn’t care about the details of how each service is built. This is where the Tokio reactor fits in.
Service trait解决了如何将各种网络服务组合在一起以构建一个高内聚应用程序的问题,但它并不关心每个服务是如何构建的细节。这就是Tokio reactor的用武之地。

The Tokio reactor is a non-blocking runtime built on top of Mio. It has the job of scheduling tasks to run based on readiness notifications of the underlying sockets.
Tokio Reactor 是构建在 Mio 之上的非阻塞运行时。 它的任务是根据底层socket的就绪通知来调度任务运行。

Let us start with a simple example: listening on a socket and accepting inbound connections.
让我们从一个简单的例子开始:监听socket并接受入站连接。

struct Listener {socket: TcpListener,
}impl Task for Listener {fn tick(&mut self) -> io::Result<Tick> {// As long as there are sockets to accept, accept and process themwhile let Some(socket) = try!(self.socket.accept()) {// Do something with the socket}Ok(Tick::WouldBlock)}
}pub fn main() {let reactor = Reactor::default().unwrap();// Run a closure on the reactorreactor.handle().oneshot(|| {let addr = "0.0.0.0:0".parse().unwrap();let listener = try!(TcpListener::bind(&addr));// Schedule the task managing the listenerreactor::schedule(Listener { socket: listener });Ok(())});reactor.run();
}

Look at all the things that I’m not doing. This is all non-blocking code. There is no interest explicitly registered with the reactor, there are no callbacks passed around, everything happens right there in the `tick` function.

看看我没有做的所有事情,这都是非阻塞代码。 没有向reactor显式登记注册,没有回调传递,一切都发生在“tick”函数中。

This is where I think Tokio does something new and interesting (if this has been done before, I am not aware of it).
这就是我认为Tokio 做了一些新的有趣的事情的地方(如果这是以前做过的,我不知道)。

Instead of requiring that you explicitly register interest in a socket in order to receive readiness notifications, as Mio requires, Tokio observes which sources (in this case, a TcpListener) the task depends on by noticing which sources the task uses. In other words, by simply using your sockets, timers, channels, etc… you are expressing readiness interest to the reactor.
Mio要求您显式地注册对socket 的兴趣以接收就绪通知,而不是像Mio那样,Tokio 通过注意任务使用的哪些源来观察任务所依赖的源(在本例中是TcpListener)。换句话说,通过简单地使用你的sockets,计时器,通道等..

So, in this example, the first time the reactor calls the Listener task, `self.socket.accept()` will return `Ok(None)` which indicates that the listener socket is not ready to complete the accept operation. However, the listener socket will also register interest in itself with the reactor. When the listener becomes ready, the task is invoked again by the reactor. This time the accept will succeed. The reactor is able to make its observations with virtually no performance overhead. You can see for yourself if you don’t believe me. ​

因此,在这个例子中,反应器第一次调用监听器任务时,“self.socket.accept()”将返回“Ok(None)”,这表明listener socket尚未准备好完成接受操作。 然而,listener socket也会向reactor注册登记。 当listener准备就绪时,reactor将再次调用该任务。 这次accept就成功了。

该reactor能够在几乎没有任何性能开销的情况下进行观测。 如果您不相信我,您可以亲自看看。

​ This pattern works out nicely for more complex cases as well. I’m looking forward to seeing what people come up with. ​
这种模式也适用于更复杂的情况。我很期待看到人们想出什么。

Community 社区

Tokio is now open to the world while still at a very early stage. This is intentional. Release early, release often. I am hoping to get the Rust community involved now to shape the direction of Tokio and to get the protocol & middleware ecosystem jump started. It is up to you to build these reusable components and share them.
Tokio 现在向世界开放,但仍处于非常早期的阶段。这是故意的。尽早发布,经常发布。我希望现在就让 Rust 社区参与进来,以塑造 Tokio 的方向,并让协议和中间件生态系统快速启动。 您可以构建这些可重用组件并共享它们。

Do you want a Cassandra driver? Write it. Do you want an HTTP 2 server? Do it!
你想要一个Cassandra 数据库驱动程序吗?开始写吧。你想要一个HTTP 2服务器吗?动手吧!

A number of Rust community members have already jumped in to help shape Tokio to what it is today.
一些Rust社区成员已经加入进来,帮助时雄塑造今天的样子​ 。

Sean McArthur of Hyper has already started working to integrate the Tokio reactor in Hyper for the 0.10 release. His feedback has been invaluable. We already have a proof of concept HTTP server providing a Service trait based API. ​
Hyper 的 Sean McArthur 已经开始致力于将 Tokio Reactor 集成到 Hyper 0.10 版本中。他的反馈是无价的。我们已经有了一个概念验证 HTTP 服务器,提供基于服务特征的 API​ 。 ​

Tikue is working on getting his RPC framework Tarpc working on Tokio using the provided building blocks. We’ve already discovered further components Tokio could provide to reduce the amount of time he has to spend writing networking code. ​

Tikue 正在致力于使用提供的构建块让他的 RPC 框架 Tarpc 在 Tokio 上运行。 我们已经发现 Tokio 可以提供更多组件来减少他编写网络代码所需的时间。 ​

This is the beginning, not the end. There is still so much to do and I hope that we can all do it together. If you want to build something with Tokio, get involved. Talk to me and the other members of the Tokio community.  Let’s build this together.

这是开始,而不是结束。 还有很多事情要做,我希望我们大家能够一起完成。 如果您想与 Tokio 一起构建一些东西,请参与其中。 与我和 Tokio 社区的其他成员交谈。 让我们一起构建这个。

相关文章:

rust-tokio发布考古

源头&#xff1a; Carl Lerche Aug 4, 2016 ​ I’m very excited to announce a project that has been a long time in the making. 我很兴奋地宣布一个酝酿已久的项目。 Tokio is a network application framework for rapid development and highly scalable deployments…...

3D医疗图像配准 | 基于Vision-Transformer+Pytorch实现的3D医疗图像配准算法

项目应用场景 面向医疗图像配准场景&#xff0c;项目采用 Pytorch ViT 来实现&#xff0c;形态为 3D 医疗图像的配准。 项目效果 项目细节 > 具体参见项目 README.md (1) 模型架构 (2) Vision Transformer 架构 (3) 量化结果分析 项目获取 https://download.csdn.net/down…...

设计模式(18):状态模式

核心 用于解决系统中复杂对象的状态转换以及不同状态下行为的封装问题 结构 环境类(Context): 环境类中维护一个State对象&#xff0c;它定义了当前的状态&#xff0c;并委托当前状态处理一些请求&#xff1b; 抽象状态类(State): 用于封装对象的一个特定状态所对应的行为&a…...

如果用大模型考公,kimi、通义千问谁能考高分?

都说大模型要超越人类了&#xff0c;今天就试试让kimi和通义千问做公务员考试题目&#xff0c;谁能考高分&#xff1f; 测评结果再次让人震惊&#xff01; 问题提干&#xff1a;大小两种规格的盒装鸡蛋&#xff0c;大盒装23个&#xff0c;小盒装16个&#xff0c;采购员小王买了…...

如何在Java中创建对象输入流

在Java中创建对象输入流&#xff08;ObjectInputStream&#xff09;通常涉及以下步骤&#xff1a; 获取源输入流&#xff1a;首先&#xff0c;你需要有一个源输入流&#xff0c;它可能来自文件、网络连接或其他任何可以提供字节序列的源。 包装源输入流&#xff1a;接着&#…...

Vue 打包或运行时报错Error: error:0308010C

问题描述&#xff1a; 报错&#xff1a;Error: error:0308010C 报错原因&#xff1a; 主要是因为 nodeJs V17 版本发布了 OpenSSL3.0 对算法和秘钥大小增加了更为严格的限制&#xff0c;nodeJs v17 之前版本没影响&#xff0c;但 V17 和之后版本会出现这个错误…...

222222222222222222222222

欢迎关注博主 Mindtechnist 或加入【Linux C/C/Python社区】一起学习和分享Linux、C、C、Python、Matlab&#xff0c;机器人运动控制、多机器人协作&#xff0c;智能优化算法&#xff0c;滤波估计、多传感器信息融合&#xff0c;机器学习&#xff0c;人工智能等相关领域的知识和…...

微信小程序 电影院售票选座票务系统5w7l6

uni-app框架&#xff1a;使用Vue.js开发跨平台应用的前端框架&#xff0c;编写一套代码&#xff0c;可编译到Android、小程序等平台。 框架支持:springboot/Ssm/thinkphp/django/flask/express均支持 前端开发:vue.js 可选语言&#xff1a;pythonjavanode.jsphp均支持 运行软件…...

C#:用定时器监控定时器,实现中止定时器正在执行的任务,并重启

Windows服务中使用的比较多的是定时器&#xff0c;但这种定时任务有个比较大的毛病&#xff1a;有时会莫名其妙地停止执行&#xff08;长时间执行不完&#xff0c;假死&#xff09;&#xff0c;必须得手工重启Windows服务才能恢复正常。这个就太麻烦了。 有没有办法来实现定时…...

计算机组成原理 — CPU 的结构和功能

CPU 的结构和功能 CPU 的结构和功能CPU 概述控制器概述CPU 框架图CPU 寄存器控制单元 CU 指令周期概述指令周期的数据流 指令流水概述指令流水的原理影响流水线性能的因素流水线的性能流水线的多发技术流水线结构 中断系统概述中断请求标记和中断判优逻辑中断请求标记 INTR中断…...

npm包安装与管理:深入解析命令行工具的全方位操作指南,涵盖脚本执行与包发布流程

npm&#xff0c;全称为Node Package Manager&#xff0c;是专为JavaScript生态系统设计的软件包管理系统&#xff0c;尤其与Node.js平台紧密关联。作为Node.js的默认包管理工具&#xff0c;npm为开发者提供了便捷的方式来安装、共享、分发和管理代码模块。 npm作为JavaScript世…...

序列化结构(protobuf)实现一个TCP服务器(C++)

Protocol Buffers&#xff08;protobuf&#xff09;是一种由Google开发的用于序列化结构化数据的方法&#xff0c;通常用于在不同应用程序之间进行数据交换或存储数据。它是一种语言无关、平台无关、可扩展的机制&#xff0c;可以用于各种编程语言和环境中。 1、首先建立proto文…...

Python中的list()和map() 用法

list() 在Python中&#xff0c;list() 是一个内置函数&#xff0c;用于创建列表&#xff08;list&#xff09;对象。它有几个不同的用途&#xff0c;但最常见的是将一个可迭代对象&#xff08;如元组、字符串、集合或其他列表&#xff09;转换为一个新的列表。 以下是一些使用…...

公网环境下如何端口映射?

公网端口映射是一种网络技术&#xff0c;它允许将本地网络中的设备暴露在公共互联网上&#xff0c;以便能够从任何地方访问这些设备。通过公网端口映射&#xff0c;用户可以通过互联网直接访问和控制局域网中的设备&#xff0c;而无需在本地网络中进行复杂的配置。 公网端口映射…...

7-36 输入年份和月份

输入一个年份和月份&#xff0c;输出这个月的天数。 输入格式: 输入年份year和月份month&#xff0c;年份和月份中间用一个空格隔开。 输出格式: 输入year年的month月对应的天数。 输入样例: 2000 2输出样例: 29输入样例: 1900 2输出样例: 28输入样例: 1900 6输出样例…...

Linux C++ 023-类模板

Linux C 023-类模板 本节关键字&#xff1a;Linux、C、类模板 相关库函数&#xff1a;getCapacity、getSize 类模板语法 类模板的作用&#xff1a;建立一个通用的类&#xff0c;类中的成员 数据类型可以不具体制定&#xff0c; 用一个虚拟的类型代表语法&#xff1a; templa…...

Android图形显示架构概览

图形显示系统作为Android系统核心的子系统&#xff0c;掌握它对于理解Android系统很有帮助&#xff0c;下面从整体上简单介绍图形显示系统的架构&#xff0c;如下图所示。 这个框架只包含了用户空间的图形组件&#xff0c;不涉及底层的显示驱动。框架主要包括以下4个图形组件。…...

算法学习17:背包问题(动态规划)

算法学习17&#xff1a;背包问题&#xff08;动态规划&#xff09; 文章目录 算法学习17&#xff1a;背包问题&#xff08;动态规划&#xff09;前言一、01背包问题&#xff1a;1.朴素版&#xff1a;&#xff08;二维&#xff09;2.优化版&#xff1a;&#xff08;一维&#xf…...

axios-mock-adapter使用

文章目录 1. 安装 axios-mock-adapter2. 引入所需的库3. 创建一个模拟适配器实例4. 定义模拟响应5. 在你的代码中使用 axios6. 在测试或开发完成后清理模拟 axios-mock-adapter 是一个用于模拟 axios HTTP 请求的库。它允许你在测试或开发过程中&#xff0c;为 axios 实例提供…...

基于单片机的家用无线火灾报警系统设计

摘 要:针对普通家庭的火灾防范需求,设计一种基于单片机的家用无线智能火灾报警系统。该系统主要由传感器、单片机、无线通信模块、GSM 模块、输入显示模块、声光报警电路和GSM 报警电路组成。系统工作时,检测部分单片机判断是否发生火灾,并将信息通过无线通信模块传…...

LangChain:索引(Indexes)--基础知识

引言 在当今信息爆炸的时代&#xff0c;如何高效地获取、处理和利用信息成为了关键。LangChain&#xff0c;作为一种先进的语言模型框架&#xff0c;提供了强大的索引功能&#xff0c;帮助用户更好地管理和应用文本数据。本文将详细介绍LangChain索引中的几个核心组件&#xf…...

Cortex-M4架构

第一章 嵌入式系统概论 1.1 嵌入式系统概念 用于控制、监视或者辅助操作机器和设备的装置&#xff0c;是一种专用计算机系统。 更宽泛的定义&#xff1a;是在产品内部&#xff0c;具有特定功能的计算机系统。 1.2 嵌入式系统组成 硬件 ①处理器&#xff1a;CPU ②存储器…...

对称排序(蓝桥杯)

文章目录 对称排序问题描述模拟 对称排序 问题描述 小蓝是一名软件工程师&#xff0c;他正在研究一种基于交换的排序算法&#xff0c;以提高排序的效率。 给定一个长度为 N 的数组 A&#xff0c;小蓝希望通过交换对称元素的方式对该数组进行排序。 具体来说&#xff0c;小蓝…...

React - 你使用过高阶组件吗

难度级别:初级及以上 提问概率:55% 高阶组件并不能单纯的说它是一个函数,或是一个组件,在React中,函数也可以做为一种组件。而高阶组件就是将一个组件做为入参,被传入一个函数或者组件中,经过一定的加工处理,最终再返回一个组件的组合…...

【C语言】结构体、枚举、联合(自定义类型)

文章目录 前言一、结构体1.结构体的声明2.结构体的自引用3.结构体变量的定义和初始化4.结构体成员的访问5.结构体内存对齐&#xff08;重点&#xff09;6.#pragma修改默认对齐数7.结构体传参 二、位段1.位段的声明2.位段的内存分配3.位段的跨平台问题 三、枚举四、联合 &#x…...

用vue.js写案例——ToDoList待办事项 (步骤和全码解析)

目录 一.准备工作 二.编写各个组件的页面结构 三.实现初始任务列表的渲染 四.新增任务 五.删除任务 六.展示未完成条数 七.切换状态-筛选数据 八.待办事项&#xff08;全&#xff09;代码 一.准备工作 在开发“ToDoList”案例之前&#xff0c;需要先完成一些准备工作&a…...

提高大型语言模型 (LLM) 性能的四种数据清理技术

原文地址&#xff1a;four-data-cleaning-techniques-to-improve-large-language-model-llm-performance 2024 年 4 月 2 日 检索增强生成&#xff08;RAG&#xff09;过程因其增强对大语言模型&#xff08;LLM&#xff09;的理解、为它们提供上下文并帮助防止幻觉的潜力而受…...

Rust 练手小项目:猜数游戏

好久没写 Rust 了&#xff0c;参考《Rust 程序设计语言》写了一下猜数游戏。差不多 40 行&#xff0c;感觉写起来真舒服。 use rand::Rng; use std::{cmp::Ordering, io};fn main() {let secret_number rand::thread_rng().gen_range(0..100);println!("[*] Guess the n…...

蓝桥杯物联网竞赛_STM32L071_16_EEPROM

仍然是没有考过的知识点 朴素的讲就是板子中一块不会因为断电重启而导致数值初始化的一片地址 要注意的是有时候容易把板子什么写错导致板子什么地址写坏了导致程序无法烧录&#xff0c;这个时候记得一直按flash键烧录&#xff0c;烧录时会报错&#xff0c;点击确定&#xff0…...

复习知识点整理

零碎语法 1.导入某个文件夹的index文件&#xff0c;index可以省略&#xff08;这里导入的是router和store文件下的index.js文件&#xff09; 2.路由懒加载 this 1.在vue文件中使用router\store对象时 this&#xff1a;普通函数的this指向vue实例对象(在没有明确指向的时候…...