Erlang TCP套接字关闭 [英] Erlang TCP sockets get closed
问题描述
要学习Erlang,我正在尝试基于 gen_tcp
实现一个小型的Web服务器。不幸的是,我的代码似乎触发了一些有线的行为。为了演示这个问题,我已经附加了我的实现的最小化版本,足以重现问题。只要提供静态200 OK,无论HTTP请求是什么。
To learn Erlang I am trying to implement a tiny web server based on gen_tcp
. Unfortunately, my code seems to trigger some wired behaviour. To demonstrate the problem I have attached a minimised version of my implementation which is sufficient to reproduce the problem. It is just delivering a static 200 OK, no matter what the HTTP request was.
当我尝试运行 ab $时出现问题c $ c>(Apache HTTP服务器基准测试)对我的Web服务器(使用回送接口)。没有任何并发请求(
-c
)一切都正常运行。但是,如果我使用 -c 8
或 -c 16
,则调用 gen_tcp:接受/ 1
似乎在某些套接字上失败,因为我在shell中看到一些请求:关闭
行。
The problem arises when I try to run ab
(Apache HTTP server benchmarking) against my web server (using loopback interface). Without any concurrent requests (-c
) everything is running just fine. However, if I use -c 8
or -c 16
, the call to gen_tcp:accept/1
seems to fail on some sockets as I see a number of request: closed
lines in the shell.
什么使得整个故事甚至是weirder是,我看到不同的行为在不同的操作系统:
What makes the whole story even weirder is, that I see different behaviours on different operating systems:
- 操作系统X + Erlang / OTP 18:
ab
报告启动后几乎立即重新启动连接。 - Debian + Erlang R15B01:全部但是有两个HTTP请求似乎正在运行。但是,然后,
ab
挂起几秒钟,并报告指定的超时已过期,总共4998个请求完成,当我运行ab
与-n 5000
。同样地,当我运行15000次测试时,报告了14998。
- OS X+Erlang/OTP 18:
ab
reports "Connection reset by peer" almost immediately after starting. - Debian+Erlang R15B01: All but two of the HTTP requests seem to run through. But then,
ab
hangs for a few seconds and reports "The timeout specified has expired, Total of 4998 requests completed", when i runab
with-n 5000
. Similarly, 14998 is reported when I run 15000 tests.
这个似乎不是问题。我真的很失落,因此感谢任何帮助! :)谢谢!
This one does not seem to be the problem. I am honestly quite lost and therefore appreciate any help! :) Thanks!
server(Port) ->
Opt = [list, {active, false}, {reuseaddr, true}],
case gen_tcp:listen(Port, Opt) of
{ok, Listen} ->
handler(Listen),
gen_tcp:close(Listen),
ok;
{error, Error} ->
io:format("init: ~w~n", [Error])
end.
handler(Listen) ->
case gen_tcp:accept(Listen) of
{ok, Client} ->
request(Client),
handler(Listen);
{error, Error} ->
io:format("request: ~w~n", [Error])
end.
request(Client) ->
Recv = gen_tcp:recv(Client, 0),
case Recv of
{ok, _} ->
Response = reply(),
gen_tcp:send(Client, Response);
{error, Error} ->
io:format("request: ~w~n", [Error])
end,
gen_tcp:close(Client).
reply() ->
"HTTP/1.0 200 OK\r\n" ++
"Content-Length: 7\r\n\r\n"
"static\n".
推荐答案
当您增加与 ab -c N
它将立即打开多个TCP套接字到服务器。
When you increase the number of concurrent requests sent with ab -c N
it will immediately open multiple TCP sockets to the server.
默认情况下,使用gen_tcp打开的套接字: listen / 2将仅支持五个未完成的连接请求。使用{backlog,N}选项来增加未完成的连接请求数量到gen_tcp:listen / 2。
By default a socket opened with gen_tcp:listen/2 will support only five outstanding connection requests. Increase the number of connection requests outstanding with the {backlog, N} option to gen_tcp:listen/2.
我用ab测试了在OS X上的代码,看到这个解决具有由对等体重新连接的文件。
I tested your code on OS X with ab and saw this resolve the prolem with "Connection reset by peer".
这篇关于Erlang TCP套接字关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!