Erlang http 服务器牛仔在 60 秒后停止传输数据

问题描述 投票:0回答:1

我正在使用牛仔测试数据流,但 60 秒后牛仔停止流,我尝试设置不同的配置选项,但没有运气。

牛仔是这样开始的:

`start_http() ->
    Port = application:get_env(test_app, http_port, 8080),
    Routes = [
        {'_', [
            {"/[...]", test_app_http_handler, []}
        ]}
    ],
    Dispatch = cowboy_router:compile(Routes),
    TransportOpst = [
        {port, Port},
        {num_acceptors, 100}
    ],
    HttpOpts = #{
        % idle_timeout     => 300000,
        % request_timeout  => 300000,
        % shutdown_timeout => 300000,
        % linger_timeout   => 300000,
        % inactivity_timeout => 300000,
        stream_handlers => [cowboy_compress_h, cowboy_stream_h],
        compress_threshold => 1024, % 1kb
        env => #{dispatch => Dispatch}
    },
    case cowboy:start_clear(test_http, TransportOpst, HttpOpts) of
        {ok, _} ->
            ok;
        E ->
            error_logger:error_msg("Cant start listnener on port:~p, reason: ~p", [Port, E]),
            ok
    end.`

我在牛仔http处理程序回调中创建流的代码如下所示:

init(Req, State) ->
    Length = 10, % each chunk is only 10 bytes
    Code = 200,
    Headers = #{},
    Body = iolist_to_binary([io_lib:format("~9.10.=B~n", [X]) || X <- lists:seq(1,200)]),
    Req1 = cowboy_req:stream_reply(Code, Headers, Req),
    stream_body(Body, Req1, Length),
    {ok, Req1, State}.

stream_body(Data, Req, Length) when byte_size(Data) > Length ->
    <<Chunk:Length/binary, Rest/binary>> = Data,
    % Make a pause before streaming each chunk 1 second
    timer:sleep(1000),
    ok = cowboy_req:stream_body(Chunk, nofin, Req),
    io:format("cowboy_req:stream_body(~p, nofin, Req)~n", [Chunk]),
    stream_body(Rest, Req, Length);
stream_body(Data, Req, _Length) ->
    ok = cowboy_req:stream_body(Data, fin, Req),
    io:format("cowboy_req:stream_body(~p, fin, Req)~n", [Data]),
    {ok, Req}.

curl 命令给了我:

curl -v http://localhost:8080/test_chunked
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /test_chunked HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< date: Tue, 06 Feb 2024 19:35:14 GMT
< server: Cowboy
< transfer-encoding: chunked
< 
* transfer closed with outstanding read data remaining
* Closing connection 0
curl: (18) transfer closed with outstanding read data remaining
=========1=========2=========3=========4=========5=========6=========7=========8=========9========10
========11========12========13========14========15========16========17========18========19========20
========21========22========23========24========25========26========27========28========29========30
========31========32========33========34========35========36========37========38========39========40
========41========42========43========44========45========46========47========48========49========50
========51========52========53========54========55========56========57========58========59


如您所见,响应在 60 的步长处中断。

如何解决这个问题,让牛仔发送所有数据,即使需要超过 60 秒?

我尝试为牛仔设置不同的设置,例如:

        % idle_timeout     => 300000,
        % request_timeout  => 300000,
        % shutdown_timeout => 300000,
        % linger_timeout   => 300000,
        % inactivity_timeout => 300000,

但这并没有帮助。

curl timeout erlang streaming cowboy
1个回答
0
投票

哎呀,它确实有效,请不要介意这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.