gRPC +图片上传 [英] gRPC + Image Upload

查看:829
本文介绍了gRPC +图片上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的gRPC端点,用户可以上传其图片.协议缓冲区声明如下:

I want to create a simple gRPC endpoint which the user can upload his/her picture. The protocol buffer declaration is the following:

message UploadImageRequest {
    AuthToken auth = 1;
    // An enum with either JPG or PNG
    FileType image_format = 2;
    // Image file as bytes
    bytes image = 3;
}

不管gRPC文档中的警告如何,这种上传图片(和接收图片)的方法仍然可行吗?

Is this approach of uploading pictures (and recieving pictures) still ok regardless of the warning in the gRPC documentation?

如果不是,那么采用标准格式上传图片并存储图片文件位置的更好方法(标准)呢?

And if not, is the better approach (standard) to upload pictures using the standard form and storing the image file location instead?

推荐答案

对于大型二进制传输,标准方法是分块.分块可用于两个目的:

For large binary transfers, the standard approach is chunking. Chunking can serve two purposes:

  1. 减少处理每条消息所需的最大内存量
  2. 提供了恢复部分上传的边界.

对于您的用例#2可能不是很必要.

For your use-case #2 probably isn't very necessary.

在gRPC中,客户端流调用允许相当自然的分块,因为它具有流控制,流水线,并且易于在客户端和服务器代码中维护上下文.如果您关心恢复部分上载,则双向流会很好用,因为服务器可以响应客户端可以用来恢复的进度确认.

In gRPC, a client-streaming call allows for fairly natural chunking since it has flow control, pipelining, and is easy to maintain context in the client and server code. If you care about recovery of partial uploads, then bidirectional-streaming works well since the server can be responding with acknowledgements of progress that the client can use to resume.

也可以使用单个RPC进行块化,但是具有更多的复杂性.在进行负载平衡时,可能需要后端与每个块与其他后端进行协调.如果您串行上传块,则网络的延迟会降低上传速度,因为您花费了大部分时间等待从服务器接收响应.然后,您要么必须并行上传(但要并行上传几个?),要么增加块大小.但是增加块大小会增加处理每个块所需的内存,并会增加恢复失败的上载的粒度.并行上传还要求服务器处理乱序上传.

Chunking using individual RPCs is also possible, but has more complications. When load balancing, the backend may be required to coordinate with other backends each chunk. If you upload the chunks serially, then the latency of the network can slow upload speed as you spend most of the time waiting to receive responses from the server. You then either have to upload in parallel (but how many in parallel?) or increase the chunk size. But increasing the chunk size increases the memory required to process each chunk and increases the granularity for recovering failed uploads. Parallel upload also requires the server to handle out-of-order uploads.

这篇关于gRPC +图片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆