简体   繁体   English

如何使用异步 GRPC 调用有效地管理 memory

[英]How to manage memory efficiently with Async GRPC calls

We are running into a memory issue when using GRPC to "fire and forget" messages.在使用 GRPC 来“触发并忘记”消息时,我们遇到了 memory 问题。

We have a service A which manages database requests and a service B which publishes event data to an external data source.我们有一个管理数据库请求的服务 A 和一个将事件数据发布到外部数据源的服务 B。

Both services implement GRPC service to communicate with them.这两个服务都实现了 GRPC 服务来与它们通信。 For example service B has a definition of:例如,服务 B 的定义为:

service MessageBusService {
    rpc PublishRevision (RevisionRequest) returns (EmptyResponse) {}
}
message EmptyResponse{
}

In service A we make potentially thousands of calls to "fire and forget" the async service call.在服务 A 中,我们可能会进行数千次调用以“触发并忘记”异步服务调用。

_ = grpcClient.PublishRevisionAsync(revision);

Where the "revision" object can be ~1MB in size. “修订版” object 的大小约为 1MB。 Performing memory profiling we have noticed that the service can spike to 600MB+ of memory consumption.执行 memory 分析,我们注意到该服务可以达到 600MB+ 的 memory 消耗。 This seems to be due to the fact that the Task we spin off will hold onto object in memory until the GRPC service responds with the "EmptyResponse" object.这似乎是因为我们分拆的任务将保留 memory 中的 object 直到 GRPC 服务以“EmptyResponse” object 响应。

Does anyone know if GRPC supports any way to truly "fire and forget" to a service, or if there is a way to host a service that explicitly sends no response?有谁知道 GRPC 是否支持任何方式来真正“触发并忘记”服务,或者是否有办法托管明确不发送响应的服务?

Is there a better way to perform this task?有没有更好的方法来执行此任务?

Any help would be greatly appreciated.任何帮助将不胜感激。

Incase someone else ever runs into this issue and needs a solution.以防其他人遇到此问题并需要解决方案。 Ok so after some research I figured out a sort of hacky way to do what I wanted.好的,经过一番研究,我想出了一种 hacky 方法来做我想做的事。

service MessageBusService {
    rpc PublishRevision (stream RevisionRequest) returns (EmptyResponse) {}
}
message EmptyResponse{
}

You can "fire and forget" to the GRPC end point by writing each element to the stream, as you are not expected to get a response for each write to the stream.您可以通过将每个元素写入 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 来“触发并忘记”到 GRPC 端点,因为每次写入 stream 时您不会收到响应。

There is some outdated examples on microsofts page https://docs.microsoft.com/en-us/dotnet/architecture/grpc-for-wcf-developers/rpc-types微软页面https://docs.microsoft.com/en-us/dotnet/architecture/grpc-for-wcf-developers/rpc-types上有一些过时的示例

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

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