简体   繁体   English

块传输 vs grpc 流

[英]Chunk transfer vs grpc streaming

I am working on a project, where I want to expose an API which will read a large file send that in response.我正在做一个项目,我想在其中公开一个 API,它将读取一个大文件作为响应发送。

Since file can be large in size, it is better to send the file in chunks, so not much memory pressure on system.由于文件可能很大,最好分块发送文件,所以 memory 对系统的压力不大。

I evaluated 2 options: chunked transfer ( https://en.m.wikipedia.org/wiki/Chunked_transfer_encoding ) which is supported by HTTP-1.1, and a server side streaming by grpc.我评估了 2 个选项:HTTP-1.1 支持的分块传输 ( https://en.m.wikipedia.org/wiki/Chunked_transfer_encoding ) 和 grpc 的服务器端流式传输。

In grpc approach, client request through a rpc, while server will stream bytes over grpc channel and close the same once done.在 grpc 方法中,客户端通过 rpc 请求,而服务器将通过 grpc 通道发送 stream 字节,并在完成后关闭。

My ecosystem can support both, and client also supports grpc.我的生态可以同时支持,客户端也支持grpc。

Can you please suggest which option is better, what are pros and cons of both approaches.您能否建议哪种选择更好,两种方法的优缺点是什么。

Both approaches will yield similar results.这两种方法都会产生相似的结果。 Just choose the stack you are most comfortable with.只需选择您最喜欢的堆栈即可。

HTTP chunking uses length-prefixed batches. HTTP 分块使用长度前缀的批次。 gRPC streaming uses length-prefixed messages. gRPC 流式处理使用长度前缀的消息。 The main difference is the batch size is chosen for you with chunking.主要区别在于批量大小是通过分块为您选择的。

gRPC uses chunking to provide its streaming, although chunking is implemented differently between HTTP/1.1 and HTTP/2. gRPC 使用分块来提供其流,尽管分块在 HTTP/1.1 和 HTTP/2 之间的实现方式不同。 When using gRPC the main thing is to use a "reasonable" message size;使用 gRPC 时,最主要的是使用“合理”的消息大小; there's an overhead of ~5-10 bytes per message.每条消息的开销约为 5-10 字节。 Overheads exist in chunking as well, just you didn't have to choose the "reasonable" message size.分块中也存在开销,只是您不必选择“合理”的消息大小。

Both chunking and gRPC streaming use pipelining, where you send a chunk before the previous chunk has been fully received and processed.分块和 gRPC 流都使用流水线,在前一个块被完全接收和处理之前发送一个块。 This reduces the influence of the chunk size on.network performance.这减少了块大小对网络性能的影响。 As long as you choose a chunk size of at least 1 KB, the chunk size shouldn't matter too much and fine-tuning will rapidly see diminishing returns.只要您选择至少 1 KB 的块大小,块大小就不会太重要,微调会很快看到收益递减。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM