当前位置:网站首页>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 :

 Use FieldMask Improve C# gRpc Service performance #yyds Dry inventory #_ client


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.

 Use FieldMask Improve C# gRpc Service performance #yyds Dry inventory #_ Server side _02

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“

 Use FieldMask Improve C# gRpc Service performance #yyds Dry inventory #_ client _03


原网站

版权声明
本文为[My space IO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021256050652.html