Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use FieldMask to improve the gRpc Service performance of C#

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use FieldMask to improve the gRpc service performance of C#". In the daily operation, I believe many people have doubts about how to use FieldMask to improve the gRpc service performance of C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to use FieldMask to improve the gRpc service performance of C#"! Next, please follow the editor to study!

Foreword:

Imagine that there is a service that provides multiple client calls, but not all clients need all the return parameters:

For example, the goods list service returns all the information about the goods, while the order service calls the goods list service, but it only needs the code and name of the goods.

Of course, we can create a separate service for this requirement, but this is not very flexible, such as when we need the coding and classification of goods.

However, a large and comprehensive service approach can lead to high computing and transmission costs, and 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 beneficial to improve service performance.

When implementing gRPC services, we can use protobuf FieldMask to implement the above functions.

I. FieldMask

By default, gRPC uses protobuf as its interface definition and data serialization protocol.

FieldMask is a protobuf message that contains a field named paths that specifies the field to be modified by the read operation or update operation:

Message FieldMask {repeated string paths = 1;}

Next, let's look at an example of how to use it in a C # gRpc service.

2. Demo1. Define .proto file

Define services and messages in the .proto 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;}

The key points are the following two sentences:

/ / reference the field_mask message import "google/protobuf/field_mask.proto"; / / define the request field google.protobuf.FieldMask field_mask = 2 position 2. Implement the server side

The server code is as follows, and five fields are returned:

Public class GreeterService: Greeter.GreeterBase {private readonly ILogger _ logger; public GreeterService (ILogger logger) {_ logger = logger } public override Task SayHello (HelloRequest request, ServerCallContext context) {var reply = new HelloReply {Message1 = "Hello" + request.Name + ", this is the first message", Message2 = "Hello" + request.Name + ", this is the second message", Message3 = "Hello" + request.Name + ", this is the third message", Message4 = "Hello" + request.Name + " This is the fourth message ", Message5 =" Hello "+ request.Name +", this is the fifth message "} Return Task.FromResult (reply);} 3. Implement the 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 ($@ "Greeting: {reply.Message1} {reply.Message2} {reply.Message3} {reply.Message4} {reply.Message5}")

FieldMask is passed, and only message2 and message4 fields are needed.

Run the program, find a problem, or return all fields:

4. Modify the server

In fact, the server side does not judge the fieldMask and modifies the server code:

Var mergedReply = new HelloReply (); request.FieldMask.Merge (reply, mergedReply); return Task.FromResult (mergedReply)

At this point, the study on "how to use FieldMask to improve the gRpc service performance of C#" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report