简体   繁体   English

如何实现零拷贝形式的gRPC C++

[英]How to implement zero-copy form gRPC c++

I want to write a gRPC server(c++) which serves as a FTP server in some case.我想编写一个 gRPC 服务器(c++),在某些情况下用作 FTP 服务器。 In detail, client requests a file by its name, while server returns the file content if it exists.具体来说,客户端通过文件名请求文件,而服务器返回文件内容(如果存在)。 In traditional socket programming, I can implement zero-copy utilizing sendfile for linux system.在传统的socket编程中,我可以利用linux系统的sendfile实现零拷贝。 It means that the file content would go directly to socket buffer from disk without transferred in user space.这意味着文件内容将直接从磁盘进入套接字缓冲区,而无需在用户空间中传输。 But in gRPC world, the socket detail is hidden, which means you might not access the file descriptor behind the socket that is necessary for sendfile :但是在 gRPC 世界中,套接字详细信息是隐藏的,这意味着您可能无法访问sendfile所需的套接字后面的文件描述符:

ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

Therefore, I must read the file bytes into memory first and return it in gRPC response afterwards.因此,我必须首先将文件字节读入内存,然后在 gRPC 响应中返回它。 That's an unnecessary performance loss.这是不必要的性能损失。 I'm wondering if there's any APIs or workaround for it.我想知道是否有任何 API 或解决方法。

Doing a sendfile straight over a low-level grpc socket wouldn't make a lot of sense, as the grpc protocol will itself have a lot of framing around the data being sent.直接通过低级 grpc 套接字执行sendfile没有多大意义,因为 grpc 协议本身将围绕正在发送的数据进行大量帧处理。 Your data will be naturally chunked and potentially multiplexed with other control data.您的数据将自然分块并可能与其他控制数据多路复用。 Therefore, there is no API to do what you want.因此,没有 API 可以做你想做的事。

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

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