代码之家  ›  专栏  ›  技术社区  ›  alehro

红隼把同时收到的请求堆起来,直到所有请求都堆起来才读取它们

  •  0
  • alehro  · 技术社区  · 6 年前

    我需要在10秒内处理5000个。问题是红隼的某个地方似乎有瓶颈。同时处理的客户端请求越多,读取第一个请求正文的延迟就越大。极简测试控制台应用程序如下:

    public interface IMyHandler
    {
        Func<HttpContext, string> Handler { get; set; }
    }
    
    public class MyHandler : IMyHandler
    {
        public Func<HttpContext, string> Handler { get; set; }
    }
    
    public class Startup : IStartup
    {
        IMyHandler MyHandler;
        public Startup(IMyHandler h)
        {
            MyHandler = h;
        }
    
        public void Configure(IApplicationBuilder app)
        {
            var this_ = this;
            app.Run(context =>
            {
                MyHandler.Handler(context);
                var response = String.Format("Hello, Universe! It is {0}", DateTime.Now);
                return context.Response.WriteAsync(response);
            });
        }
    
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // Add framework services
            return services.BuildServiceProvider();
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            EventWaitHandle success = new AutoResetEvent(false);
            var sentRequests = 0;
            var serverTask = Task.Run(() =>
            {
    
                MyHandler h1 = new MyHandler();
                h1.Handler = (context) =>
                {
                    using (BufferedStream buffer = new BufferedStream(context.Request.Body))
                    {
                        using (var reader = new StreamReader(buffer))
                        {
                            var body = reader.ReadToEnd();
                            var _sent = sentRequests;
                            //success.Set();
                        }
                    }
                    return "OK";
                };
    
                var host = new WebHostBuilder()
                    .UseKestrel((context, options) =>
                    {
                        //options.ApplicationSchedulingMode = SchedulingMode.Inline;
                        options.ListenAnyIP(21122, listenOptions =>
                        {
                        });
                    })
                    .UseContentRoot(Directory.GetCurrentDirectory())
    
                     .ConfigureServices(services =>
                     {
                         services.Add(Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Singleton(typeof(IMyHandler), h1));
                     })
                    //.UseIISIntegration()
                    .UseStartup<Startup>()
                    //.UseApplicationInsights()
                    .Build();
    
                host.Run();
            });
            Enumerable.Range(0, 100).ForEach((n) => {
                var clientTask = Task.Run(async () =>
                {
                    var handler = new HttpClientHandler();
                    HttpClient client = new HttpClient(handler);
                    sentRequests++;
                    var p1 = await client.PostAsync("http://localhost:21122", new StringContent("{test:1}", Encoding.UTF8, "application/json"));
                });
            });
    
            Assert.IsTrue(success.WaitOne(new TimeSpan(0, 0, 50)));
        }
    }
    

    对于给定的100个请求,我已经在第一个body read上有大约12秒的延迟。要查看这个,只需在行上设置断点

    var _sent = sentRequests;
    

    sent requests也等于100,这意味着(这实际上是一个猜测,但似乎是合理的)此时所有的请求都被发送了。在我看来,这两件事似乎是由一个线程完成的,虽然它接受请求,但并没有开始读取它们。

    0 回复  |  直到 6 年前