【问题标题】:YAWS responds with errors under light load. "Unhandled reply fr. do_recv() {error,econnaborted}"YAWS 在轻负载下响应错误。 “未处理的回复 fr.do_recv() {error,econnaborted}”
【发布时间】:2017-06-09 07:40:16
【问题描述】:

你好厄兰格人 :)

这里只是另一个使用该语言的 Erlang 爱好者。我有一个非常简单的 YAWS 应用程序模块,在单个客户端访问时可以正常工作。但是,如果我尝试跨越多个客户端来模拟轻量流量,这些客户端就会开始接收错误。知道是什么原因造成的吗?

生成客户端的代码:

-module(concurrent).
-export([measure/0, get/0]).

get() ->
  Result = httpc:request("http://localhost:8080/blah"),
  io:format("Result: ~p.\n", [Result]).

get_spawner(0) -> io:format("Done.\n");
get_spawner(Times) ->
  spawn(concurrent, get, []),
  get_spawner(Times - 1).

measure() ->
  io:fwrite("Starting inets...\n"),
  inets:start(),
  io:fwrite("Done\n"),

  io:fwrite("Creating blah...\n"),
  httpc:request(put, { "http://localhost:8080/blah", [], "", "" }, [], []),
  io:fwrite("Done\n"),

  get_spawner(9),

  io:fwrite("Stopping inets...\n"),
  inets:stop(),
  io:fwrite("Done\n"),

  init:stop().

我观察到这对于 8 个并发客户端来说一切正常,但是当我跨越 9 个或更多客户端时,它们开始从服务器接收以下消息:

=ERROR REPORT==== 24-Jan-2017::12:30:04 ===
** Generic server httpc_manager terminating
** Last message in was {request,
                           {request,undefined,<0.74.0>,0,http,
                               {"localhost",8080},
                               "/blah",[],get,
                               {http_request_h,undefined,"keep-alive",
                                   undefined,undefined,undefined,undefined,
                                   undefined,undefined,undefined,undefined,
                                   undefined,undefined,undefined,undefined,
                                   undefined,undefined,"localhost:8080",
                                   undefined,undefined,undefined,undefined,
                                   undefined,undefined,undefined,undefined,
                                   undefined,[],undefined,undefined,undefined,
                                   undefined,"0",undefined,undefined,
                                   undefined,undefined,undefined,undefined,[]},
                               {[],[]},
                               {http_options,"HTTP/1.1",infinity,true,
                                   {essl,[]},
                                   undefined,false,infinity,false},
                               "http://localhost:8080/blah",[],none,[],
                               1485261004189,undefined,undefined,false}}
** When Server state == {state,[],httpc_manager__handler_db,
                            {cookie_db,undefined,8209},
                            httpc_manager__session_db,httpc_manager,
                            {options,
                                {undefined,[]},
                                {undefined,[]},
                                0,2,5,120000,2,disabled,false,inet,default,
                                default,[]}}
** Reason for termination ==
** {'EXIT',
       {shutdown,
           {gen_server,call,
               [httpc_handler_sup,
                {start_child,
                    [<0.61.0>,
                     {request,#Ref<0.0.6.64>,<0.74.0>,0,http,
                         {"localhost",8080},
                         "/blah",[],get,
                         {http_request_h,undefined,"keep-alive",undefined,
                             undefined,undefined,undefined,undefined,
                             undefined,undefined,undefined,undefined,
                             undefined,undefined,undefined,undefined,
                             undefined,"localhost:8080",undefined,undefined,
                             undefined,undefined,undefined,undefined,
                             undefined,undefined,undefined,[],undefined,
                             undefined,undefined,undefined,"0",undefined,
                             undefined,undefined,undefined,undefined,
                             undefined,[]},
                         {[],[]},
                         {http_options,"HTTP/1.1",infinity,true,
                             {essl,[]},
                             undefined,false,infinity,false},
                         "http://localhost:8080/blah",[],none,[],
                         1485261004189,undefined,undefined,false},
                     {options,
                         {undefined,[]},
                         {undefined,[]},
                         0,2,5,120000,2,disabled,false,inet,default,default,
                         []},
                     httpc_manager]},
                infinity]}}}

=ERROR REPORT==== 24-Jan-2017::12:30:04 ===
Error in process <0.74.0> with exit value:
{{case_clause,
     {undefined,
         {error,
             {'EXIT',
                 {shutdown,
                     {gen_server,call,
                         [httpc_handler_sup,
                          {start_child,
                              [<0.61.0>,
                               {request,#Ref<0.0.6.64>,<0.74.0>,0,http,
                                   {"localhost",8080},
                                   "/blah",[],get,
                                   {http_request_h,undefined,"keep-alive",
                                       undefined,undefined,undefined,
                                       undefined,undefined,undefined,
                                       undefined,undefined,undefined,
                                       undefined,undefined,undefined,
                                       undefined,undefined,"localhost:8080",
                                       undefined,undefined,undefined,
                                       undefined,undefined,undefined,
                                       undefined,undefined,undefined,[],
                                       undefined,undefined,undefined,
                                       undefined,"0",undefined,undefined,
                                       undefined,undefined,undefined,
                                       undefined,[]},
                                   {[],[]},
                                   {http_options,"HTTP/1.1",infinity,true,
                                       {essl,[]},
                                       undefined,false,infinity,false},
                                   "http://localhost:8080/blah",[],none,[],
                                   1485261004189,undefined,undefined,false},
                               {options,
                                   {undefined,[]},
                                   {undefined,[]},
                                   0,2,5,120000,2,disabled,false,inet,default,
                                   default,[]},
                               httpc_manager]},
                          infinity]}}}}}},
 [{httpc,handle_request,9,[{file,"httpc.erl"},{line,574}]},
  {concurrent,get,0,
      [{file,
           "c:/Users/piotr_justyna/Documents/rets/performance_tests/concurrent.erl"},
       {line,5}]}]}

在服务器端我得到了这个:

=ERROR REPORT==== 24-Jan-2017::12:28:55 ===
Unhandled reply fr. do_recv() {error,econnaborted}

我确定这很简单,只是无法弄清楚它是什么。是导致它的并发连接数吗?你能帮忙解释一下吗?

问候, 彼得

【问题讨论】:

    标签: erlang yaws httpc


    【解决方案1】:

    您的代码产生了许多httpc:request/1 调用,但随后它立即停止inets

    ...
    get_spawner(9),
    io:fwrite("Stopping inets...\n"),
    inets:stop(),
    ...
    

    由于inets 已关闭,仍在进行中的请求会过早关闭,如您在错误报告中所见:

    ** Reason for termination ==
    ** {'EXIT',
           {shutdown,
               {gen_server,call,
                   [httpc_handler_sup,
    

    在调用inets:stop/0 之前,您需要让所有派生请求的父级等待这些请求完成。一种方法是将父 pid 传递给每个生成的子进程,一旦httpc:request/1 返回,让子进程向父进程发送消息。父级将生成所有N 子级,然后循环等待接收来自子级的N 完成消息,只有在收到所有完成消息后才会调用inets:stop/0

    它适用于 8 个请求但不适用于 9 个请求的原因是由于在调用 inets:stop/0 之前没有等待的竞争条件。 Yaws 请求处理接收器进程池的默认大小恰好是 8。当您的代码发送 8 个请求时,池处理它的速度足以击败您的代码对inets:stop/0 的调用,但是对于 9,有一点延迟接受器处理两个请求,并且微小的时间变化足以让您的inets:stop/0 调用破坏您的一个或多个客户端进程。您可以通过 acceptor_pool_size 配置变量修改 Yaws 接受器池大小。

    如果您对负载测试很认真,您可以考虑使用像 tsung 这样的工业级解决方案,而不是自己动手。

    【讨论】:

    • 太棒了。质量建议,非常感谢。让我试试,我会更新线程。
    • 就是这样,谢谢。感谢 tsung 的推荐——看来我很快就要开始使用它了。
    猜你喜欢
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多