深入理解Tokio——架构设计篇

本文原版本为官方英文文档,详情见官方http://tokio.rs

网络程序一般分为以下几个层次:

Byte streams 层,处在最低层。这一层一般是提供TCP或者UDP Socket.这一层,一般操作byte buffer.TLS使用也是处在这一层。

Framing is taking a raw stream of bytes and breaking it up into meaningful units. For example, HTTP naturally has frames consisting of request headers, response headers, or body chunks. A line-based protocol consists of String frames that are delineated by new line tokens. At this point, instead of dealing with a stream of raw bytes, we are dealing with a stream of frame values. In Tokio, we sometimes refer to a full duplex stream of frames as a transport, which implements both the Stream and Sink traits.

Framing将一个原始的字节流,并分解成有意义的单位。例如,HTTP由请求头,响应头或主体块组成的帧。基于行的协议由换行标记描述的字符串frame组成。在这一点上,我们不是处理一个原始字节流,而是处理一系列的frame值。在Tokio中,我们有时将帧的全双工流称为transport,它实现了Stream和Sink特性。

request / response exchange generally is where application logic starts appearing. For a client, at this layer, a request is issued and a response for the request is returned. When the request is issued, it is turned into one or more frames and written to a transport. Then, at some point in the future, a response to the request will be read from the transport, and matched with the original request.

Request / Response应答层,一般是程序逻辑开始的地方。对于一个客户端,这一层,一个请求发出时,他会转换成一个或多个Frames,写入Transport。然后,从这个Transport中读取匹配原来请求的响应

At the application layer, the details of how requests and responses are mapped onto a transport don’t matter. A single application may be receiving and issuing requests for many different protocols. An HTTP server application will be receiving HTTP requests, and then in turn, issuing database requests or other HTTP requests.

应用层,不关心请求与响应如何映射到一个Transport中。一个应用程序也许通过许多不同的协议接收和发起请求。一个Http server应用程序会接收Http请求,然后发起数据库请求或其它http请求。

Each of these layers tend to be implemented in different libraries, and the end application will pull in the protocol implementations and just interact with them at the request / response exchange layer.

每一层都是在不同的库中实现的,最后,应用会将他们整合到协议实现中去,并在request / response应答中使用他们。

Tokio’s abstractions map onto these different layers.

Tokio对上述这些层进行一一抽象。

Byte streams

tokio-coreprovides the lowest level building blocks for writing asynchronous I/O code: anevent loopand theconcrete I/O types, such as TCP and UDP sockets. These primitives work on the byte level much like thestd::iotypes, except the Tokio types are non-blocking. Other sections describe bothhigh-levelandlow-levelAPIs for working with byte streams.

tokio-core提供最低层异步I/O代码:event loop和具体的I/O类型,如TCP和UDP。主要工作中byte层,类似于std::IO类型,但是Tokio类型都是非阻塞的。其它部分都是描述上层和低层的处理byte Stream的api。

Framing

Framing is done with Tokio by first defining a frame type, usually an enum, then implementing a transport as aStream + Sinkthat works with that frame type. The transport handles encoding and decoding the frame values to the raw stream of bytes. This can either be donemanuallyor using a helper likeframed.

Framing Tokio首先定义一个Frame类型,通常是一个枚举,然后实现了一个transport,实现Stream和Sink trait来处理这个Frame类型。Transport解码和编码这Frame值为byte stream.可以动手实现也可以使用framed helper.

Later sections coverworking with transportsandhandshakes in particular.

后面的章节包含处理transporthandshake

Request / Response

The request / response exchange layer is handled by Tokio’sServicetrait. TheServicetrait is a simplified interface making it easy to write network applications in a modular and reusable way, decoupled from the underlying protocol. It is one of Tokio’s fundamental abstractions. It is a similar abstraction to Finagle’sService, Ruby’s Rack, or Java’s servlet; however, Tokio’sServicetrait is abstract over the underlying protocol.

请求和应答层在Service trait中被处理。Service trait是一个简单的接口,非常容易以模块化和可重用的方式实现网络应用层,与底层的协议解耦。这是Tokio基础抽象之一,处在低层协议之上,类似于Finagle Service, Ruby Rack, Java servlet。

There are generally two ways to map request / responses to a stream of frames: pipelined ormultiplexing.tokio-proto’s goal is to take a transport and handle the required logic to map that to an implementation ofService.

通常有两种方式将request / responses映射到一个帧流:pipelined或者multiplexing。tokio-proto的目标是将transport和处理所需的逻辑映射到一个服务实现。

A big advantage of having a standardizedServiceinterface is that it makes it possible to write reusable middleware components that add useful functionality.

标准的Service接口最大的优点是,可以写可重用的中间层组件。

Application

Generally, all the previously listed layers will be implemented in libraries. For example, an HTTP server implementation would implement an HTTP transport, then usetokio-prototo map that to aService. TheServiceis what the HTTP library would expose.

一般来说,所有之前列出的层都是在库中实现的。例如一个http服务实现在一个http transport中,然后使用tokio-proto将其转换成一个服务。

An application would depend on many different libraries, providing various protocol implementations exposed as services, and using thefutureslibrary to hook everything together.

一个应用依赖很多不同的库,提供各种协议。作为服务提供出来,使用futures库将所有东西结合在一起。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容