几个小时后,我的 grpc 服务器响应速度较慢

My grpc server is having slower response after a few hours

提问人:Qing Feng 提问时间:11/8/2023 最后编辑:Cem PolatQing Feng 更新时间:11/8/2023 访问量:48

问:

在此处输入图像描述我添加了一行日志来监视执行功能,似乎运行时间总是在 1 毫秒左右。但是,几个小时后,grpc 记录器显示响应时间非常长。它将打印出如下内容:

Request finished HTTP/2 POST http://127.0.0.1:5060/Instrument.InstrumentControl/Perform application/grpc - - 200 - application/grpc 9.9647ms

如果我将服务打开几天,这个数字将上升到 170 毫秒,但我的日志仍然显示我的函数仍然需要大约 1 毫秒。此时客户端也处于滞后状态。
谁能告诉我开销在哪里?提前致谢!

来自 grpc 日志记录的一致运行时。

我的代码:

public class Program
{
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.UseKestrel(options =>
                    {
                        options.ConfigureEndpointDefaults(opt =>
                        {
                            opt.Protocols = HttpProtocols.Http2;
                        });
                    });
                    webBuilder.UseUrls($"http://[::]:{ServicePort.InstrumentService}");
                });

        public static void Main(string[] args)
        {
             CreateHostBuilder(args).Build().Run();
        }
}

public class Startup
{            
        public void ConfigureServices(IServiceCollection services)
        {          
            services.AddGrpc().AddServiceOptions<InstrumentService>(options =>
            {
                options.MaxReceiveMessageSize = Data.Constants.GrpcMaxLength.MaxMsgRecLen;
                options.MaxSendMessageSize = Data.Constants.GrpcMaxLength.MaxMsgRecLen;
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
               
                endpoints.MapGrpcService<InstrumentService>();
              
                endpoints.MapGrpcService<SubscribeService>();

                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
                });
            });
        }
    }

InstrumentService 都是一元 RPC。 SubscribeService 是一个单服务器流式 rpc,只要客户端连接,它就会处于活动状态,并将数据/状态发布回客户端。我目前的使用情况是该服务的单个客户端。

客户端:

public static GrpcChannel CreateChannel(string ip, int port)
        {
            return GrpcChannel.ForAddress($"http://{ip}:{port}",
                new GrpcChannelOptions
                {
                    HttpHandler = new SocketsHttpHandler
                    {
                        PooledConnectionIdleTimeout = Timeout.InfiniteTimeSpan,
                        EnableMultipleHttp2Connections = true,
                    },
                    DisposeHttpClient = true,
                    MaxReceiveMessageSize = 
                    Constants.GrpcMaxLength.MaxMsgRecLen,
                    MaxSendMessageSize = 
                    Constants.GrpcMaxLength.MaxMsgSndLen
                }
            );
        }
_channel = CreateChannel(config.Ip, config.port);

_instrumentClient = new InstrumentControl.InstrumentControlClient(_channel);

_subscribeClient = new SubscribeControl.SubscribeControlClient(_channel);

谢谢!

C# 性能 服务 gRPC 响应

评论

0赞 Tragamota 11/8/2023
您能否添加端点的代码及其其他相关部分?这样可以更轻松地分析您的问题。

答: 暂无答案