当前位置:网站首页>Using fieldmask to improve c grpc service performance yyds dry inventory
Using fieldmask to improve c grpc service performance yyds dry inventory
2022-06-10 06:11:00 【My space IO】
Preface
Imagine , There is a service that provides multiple client calls , But not all clients need all the return parameters :
For example, the product list service returns all information about the product , The order service calls the commodity list service , But it only needs the code and name of the product .
Of course , We can create a separate service for this requirement , But it's not very flexible , For example, what should I do when I need the coding and classification of commodities ?
however , Large and comprehensive service methods can lead to high computing and transmission costs , If we can understand which fields in the response do not need to be provided to the caller , So as to avoid unnecessary calculation and transmission , This is usually very useful for improving service performance .
In the realization of gRPC The service , We can use protobuf FieldMask Realize the above functions .
FieldMask
By default ,gRPC Use protobuf As its interface definition language and data serialization protocol .
FieldMask It's a protobuf news , Include a name paths Field of , Used to specify the fields to be modified by the read operation return or update operation :
message FieldMask {
repeated string paths = 1;
}
- 1.
- 2.
- 3.
below , Let's take an example , How to be in C# gRpc Use it in services .
Demo
1. Definition .proto file
stay .proto Define services and messages in the file :
syntax = "proto3";
option csharp_namespace = "GrpcService2";
import "google/protobuf/field_mask.proto";
package greet;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply);
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
google.protobuf.FieldMask field_mask = 2;
}
// The response message containing the greetings.
message HelloReply {
string message1 = 1;
string message2 = 2;
string message3 = 3;
string message4 = 4;
string message5 = 5;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
The key points are as follows 2 sentence :
// quote field_mask news
import "google/protobuf/field_mask.proto";
// Define request fields
google.protobuf.FieldMask field_mask = 2;
- 1.
- 2.
- 3.
- 4.
- 5.
2. Implement the server
The server code is as follows , Back to 5 A field :
public class GreeterService : Greeter.GreeterBase
{
private readonly ILogger<GreeterService> _logger;
public GreeterService(ILogger<GreeterService> logger)
{
_logger = logger;
}
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
var reply = new HelloReply
{
Message1 = "Hello " + request.Name + ", This is the first 1 Bar message ",
Message2 = "Hello " + request.Name + ", This is the first 2 Bar message ",
Message3 = "Hello " + request.Name + ", This is the first 3 Bar message ",
Message4 = "Hello " + request.Name + ", This is the first 4 Bar message ",
Message5 = "Hello " + request.Name + ", This is the first 5 Bar message "
};
return Task.FromResult(reply);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
3. Implement client
The client code is as follows :
using var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Greeter.GreeterClient(channel);
FieldMask fieldMask = new FieldMask();
fieldMask.Paths.AddRange(new string[] { "message2", "message4" });
var request = new HelloRequest { Name = "My IO" };
request.FieldMask = fieldMask;
var reply = await client.SayHelloAsync(request);
Console.WriteLine([email protected]"Greeting:
{reply.Message1}
{reply.Message2}
{reply.Message3}
{reply.Message4}
{reply.Message5}
" );
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
Into FieldMask, All we need here is message2、message4 Field .
Run the program , Find a problem , All fields are returned :

4. Modify the server
In fact, there is no judgment on the server side fieldMask, Modify the server code :
var mergedReply = new HelloReply();
request.FieldMask.Merge(reply, mergedReply);
return Task.FromResult(mergedReply);
- 1.
- 2.
- 3.
- 4.

Conclusion
In this paper , We saw how to use FieldMask , This is just a control that does not return fields , You can implement other logic yourself .
Want to know more about , Please pay attention to my official account ”My IO“

边栏推荐
猜你喜欢
随机推荐
N-gram language model
Improper use of BigDecimal caused P0 accident!
Flink 系例 之 SessionWindow
Cremb Pro backend sub administrator 403 problem analysis
SQL practice: SQL foundation and execution sequence
Machine learning notes - Interpretation of transformer/attention papers
[understanding of opportunity -20]: Guiguzi - reaction chapter - the art of movement and stillness, the combination of speaking and listening, silence is golden
NPM command Encyclopedia
Solve the problem of npcap installation failure in Wireshark
Will technology talk about alphacode affect programmers' jobs tonight?
词向量:GloVe 模型详解
Business topic: ab test experiment design and evaluation
Flink 系例 之 CountWindowAll
ArcGIS应用(十八)Arcgis 矢量图层属性表显示精度变化问题详解
Business topic: user growth analysis
SQL practice: SQL window functions and real problems
异构网络是什么?
Implementation of thread pool
ArcGIS application (XVIII) detailed explanation of display accuracy change of ArcGIS vector layer attribute table
What does network edge mean?









