美文网首页
深入理解Tokio——多路复用协议

深入理解Tokio——多路复用协议

作者: 新高_Butland | 来源:发表于2017-11-06 10:11 被阅读0次

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

    Amultiplexedsocket connection is one that allows many concurrent requests to be issued, with responses coming back in the order they are ready, rather than in the order requested. Multiplexing allows a server to begin processing requests as soon as they are received and to respond to the client as soon as the request is processed. Generally, multiplexed protocols will make better usage of available resources like TCP sockets.

    多路复用socket链接允许同时并发多个请求,按照处理完成顺序响应,而不是按照请求顺序响应。多路利用允许一个服务只要请求一到达就开始处理这些请求,同时只要请求一处理完成应当响应。一般来说,多路复用协议更好的利用了像TCP socket资源。

    Since responses arrive out of order, arequest IDis used to match responses with their associated requests. When the client issues a request, the request will be paired with an identifier. The server processes the request, and sends a response to the client paired with the same request identifier. This allows the client to match the response with the request that it issued.

    因为响应到是无序的,所以使用request ID来匹配request和他的响应。当client发起一个请求时,这个请求和一个标识组合。这服务处理这个请求,发现响应的时候带上相同的请求标识。客户端就能匹配到发起请求的响应结果。

    Flow of multiplexed requests and responses

    多路复用请求和响应流程图

    Tokio makes implementing multiplexed clients and servers easy. This guide will show how.

    Tokio使多路复用client和server实现变得更加简单。具体见下面介绍:

    Overview

    Just as with theecho serverguide, implementing a client or server for a multiplexed protocol is done in three parts:

    实现一个client和server多路复用分为三个步骤:

    Atransport, which manages serialization of Rust request and response types to the underlying socket. In this guide, we will implement this using theframedhelper, just as before.

    Transport,负责将Rust 请求和响应类型序列化到低层socket.这里,我们将使用framer helper实现

    Aprotocol specification, which puts together a codec and some basic information about the protocol.

    Protocol规范,将编码器与一些协议基本信息组合到一起。

    Aservice, which says how to produce a response given a request. A service is basically an asynchronous function.

    Service,负责处理请求的响应逻辑。一个service一般是一个异步function。

    Each part can vary independently, so once you’ve implemented a protocol (like HTTP), you can pair it with a number of different services.

    每一部分都是独立的,你一旦实现了一个协议(像http),你可以与其它的服务组合。

    This guide specifically covers implementing asimplemultiplexed protocol. Thenext sectionwill show how to implement a streaming protocol.

    本文包含实现一个简单的多路复用协议。下一章展示如何实现一个stream协议。

    The full implementation can be found in thetokio-linerepository. Let’s take it step by step.

    Step 1: Implement a transport 

    实现一个transport

    In Tokio, atransportis any type implementingStream+Sinkwhere the yielded items are frames.

    在Tokio,一个Transport 是一个实现了Stream+Sink trait的类型,其生产的items都是frames

    同样,我们将实现line-based协议,不过这次是全双工,是由frames组成的stream,每一个frame包含:

                1、在网络字节序中,头4字节表示request ID。

                 2、Frame payload,一个UTF-8编码的不定长的字符串,以字符"\n"结束

    注意,这些Line不支持包含转义后的换行符。

    使用tokio-proto全双工后,我们的transport会变得复杂,frame将是由request Id和payload组件的元组构成的。

    use tokio_proto::multiplex::RequestId;

    type MyMultiplexedFrame = (RequestId, T);

    TheT here represents the request or response type used for theService.tokio-protowill use theRequestIdin the frame to match outstanding requests with responses.

    T 代表的是请求或响应的类型。tokio-ptoto将使用request id去匹配请求和响应。

    Step 2: 实现Protocol

    下一步是定义协议细节。

    Step 3:实现服务

    An interesting thing to note is thatRequestIddoes not show up as part of theService’s request and response types. TheRequestIdis only needed for tokio-proto to manage the internal state of keeping requests and responses matched when operating on the transport.

    一个比较有意思的事情是Request ID 并没有作为Service的Request和Response类型的一部分,Request Id只是被tokio-proto用来在transport上管理内部状态和匹配request和response。

    Again, you can find the full working example here.

    相关文章

      网友评论

          本文标题:深入理解Tokio——多路复用协议

          本文链接:https://www.haomeiwen.com/subject/tnjopxtx.html