0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Ensure we can stream the response body from any process

This commit is contained in:
Loïc Hoguin 2019-10-02 20:30:32 +02:00
parent 20660d7566
commit eaa052616f
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
4 changed files with 38 additions and 9 deletions

View file

@ -827,19 +827,19 @@ stream_body(_, _, #{method := <<"HEAD">>, has_sent_resp := headers}) ->
stream_body({sendfile, _, 0, _}, nofin, _) -> stream_body({sendfile, _, 0, _}, nofin, _) ->
ok; ok;
stream_body({sendfile, _, 0, _}, IsFin=fin, Req=#{has_sent_resp := headers}) -> stream_body({sendfile, _, 0, _}, IsFin=fin, Req=#{has_sent_resp := headers}) ->
stream_body({data, IsFin, <<>>}, Req); stream_body({data, self(), IsFin, <<>>}, Req);
stream_body({sendfile, O, B, P}, IsFin, Req=#{has_sent_resp := headers}) stream_body({sendfile, O, B, P}, IsFin, Req=#{has_sent_resp := headers})
when is_integer(O), O >= 0, is_integer(B), B > 0 -> when is_integer(O), O >= 0, is_integer(B), B > 0 ->
stream_body({data, IsFin, {sendfile, O, B, P}}, Req); stream_body({data, self(), IsFin, {sendfile, O, B, P}}, Req);
stream_body(Data, IsFin=nofin, Req=#{has_sent_resp := headers}) stream_body(Data, IsFin=nofin, Req=#{has_sent_resp := headers})
when not is_tuple(Data) -> when not is_tuple(Data) ->
case iolist_size(Data) of case iolist_size(Data) of
0 -> ok; 0 -> ok;
_ -> stream_body({data, IsFin, Data}, Req) _ -> stream_body({data, self(), IsFin, Data}, Req)
end; end;
stream_body(Data, IsFin, Req=#{has_sent_resp := headers}) stream_body(Data, IsFin, Req=#{has_sent_resp := headers})
when not is_tuple(Data) -> when not is_tuple(Data) ->
stream_body({data, IsFin, Data}, Req). stream_body({data, self(), IsFin, Data}, Req).
%% @todo Do we need a timeout? %% @todo Do we need a timeout?
stream_body(Msg, #{pid := Pid, streamid := StreamID}) -> stream_body(Msg, #{pid := Pid, streamid := StreamID}) ->
@ -850,7 +850,7 @@ stream_body(Msg, #{pid := Pid, streamid := StreamID}) ->
stream_events(Event, IsFin, Req) when is_map(Event) -> stream_events(Event, IsFin, Req) when is_map(Event) ->
stream_events([Event], IsFin, Req); stream_events([Event], IsFin, Req);
stream_events(Events, IsFin, Req=#{has_sent_resp := headers}) -> stream_events(Events, IsFin, Req=#{has_sent_resp := headers}) ->
stream_body({data, IsFin, cow_sse:events(Events)}, Req). stream_body({data, self(), IsFin, cow_sse:events(Events)}, Req).
-spec stream_trailers(cowboy:http_headers(), req()) -> ok. -spec stream_trailers(cowboy:http_headers(), req()) -> ok.
stream_trailers(Trailers, #{pid := Pid, streamid := StreamID, has_sent_resp := headers}) -> stream_trailers(Trailers, #{pid := Pid, streamid := StreamID, has_sent_resp := headers}) ->

View file

@ -41,6 +41,7 @@
read_body_is_fin = nofin :: nofin | {fin, non_neg_integer()}, read_body_is_fin = nofin :: nofin | {fin, non_neg_integer()},
read_body_buffer = <<>> :: binary(), read_body_buffer = <<>> :: binary(),
body_length = 0 :: non_neg_integer(), body_length = 0 :: non_neg_integer(),
stream_body_pid = undefined :: pid() | undefined,
stream_body_status = normal :: normal | blocking | blocked stream_body_status = normal :: normal | blocking | blocked
}). }).
@ -225,28 +226,36 @@ info(StreamID, Headers={headers, _, _}, State) ->
do_info(StreamID, Headers, [Headers], State#state{expect=undefined}); do_info(StreamID, Headers, [Headers], State#state{expect=undefined});
%% Sending data involves the data message, the stream_buffer_full alarm %% Sending data involves the data message, the stream_buffer_full alarm
%% and the connection_buffer_full alarm. We stop sending acks when an alarm is on. %% and the connection_buffer_full alarm. We stop sending acks when an alarm is on.
info(StreamID, Data={data, _, _}, State0=#state{pid=Pid, stream_body_status=Status}) -> %%
%% We only apply backpressure when the message includes a pid. Otherwise
%% it is a message from Cowboy, or the user circumventing the backpressure.
%%
%% We currently do not support sending data from multiple processes concurrently.
info(StreamID, Data={data, _, _}, State) ->
do_info(StreamID, Data, [Data], State);
info(StreamID, Data0={data, Pid, _, _}, State0=#state{stream_body_status=Status}) ->
State = case Status of State = case Status of
normal -> normal ->
Pid ! {data_ack, self()}, Pid ! {data_ack, self()},
State0; State0;
blocking -> blocking ->
State0#state{stream_body_status=blocked}; State0#state{stream_body_pid=Pid, stream_body_status=blocked};
blocked -> blocked ->
State0 State0
end, end,
Data = erlang:delete_element(2, Data0),
do_info(StreamID, Data, [Data], State); do_info(StreamID, Data, [Data], State);
info(StreamID, Alarm={alarm, Name, on}, State) info(StreamID, Alarm={alarm, Name, on}, State)
when Name =:= connection_buffer_full; Name =:= stream_buffer_full -> when Name =:= connection_buffer_full; Name =:= stream_buffer_full ->
do_info(StreamID, Alarm, [], State#state{stream_body_status=blocking}); do_info(StreamID, Alarm, [], State#state{stream_body_status=blocking});
info(StreamID, Alarm={alarm, Name, off}, State=#state{pid=Pid, stream_body_status=Status}) info(StreamID, Alarm={alarm, Name, off}, State=#state{stream_body_pid=Pid, stream_body_status=Status})
when Name =:= connection_buffer_full; Name =:= stream_buffer_full -> when Name =:= connection_buffer_full; Name =:= stream_buffer_full ->
_ = case Status of _ = case Status of
normal -> ok; normal -> ok;
blocking -> ok; blocking -> ok;
blocked -> Pid ! {data_ack, self()} blocked -> Pid ! {data_ack, self()}
end, end,
do_info(StreamID, Alarm, [], State#state{stream_body_status=normal}); do_info(StreamID, Alarm, [], State#state{stream_body_pid=undefined, stream_body_status=normal});
info(StreamID, Trailers={trailers, _}, State) -> info(StreamID, Trailers={trailers, _}, State) ->
do_info(StreamID, Trailers, [Trailers], State); do_info(StreamID, Trailers, [Trailers], State);
info(StreamID, Push={push, _, _, _, _, _, _, _}, State) -> info(StreamID, Push={push, _, _, _, _, _, _, _}, State) ->

View file

@ -247,6 +247,21 @@ do(<<"stream_body">>, Req0, Opts) ->
cowboy_req:stream_body(<<"Hello! ">>, nofin, Req), cowboy_req:stream_body(<<"Hello! ">>, nofin, Req),
cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, fin, Req), cowboy_req:stream_body({sendfile, 0, AppSize, AppFile}, fin, Req),
{ok, Req, Opts}; {ok, Req, Opts};
<<"spawn">> ->
Req = cowboy_req:stream_reply(200, Req0),
Parent = self(),
Pid = spawn(fun() ->
cowboy_req:stream_body(<<"Hello ">>, nofin, Req),
cowboy_req:stream_body(<<"world">>, nofin, Req),
cowboy_req:stream_body(<<"!">>, fin, Req),
Parent ! {self(), ok}
end),
receive
{Pid, ok} -> ok
after 5000 ->
error(timeout)
end,
{ok, Req, Opts};
_ -> _ ->
%% Call stream_body without initiating streaming. %% Call stream_body without initiating streaming.
cowboy_req:stream_body(<<0:800000>>, fin, Req0), cowboy_req:stream_body(<<0:800000>>, fin, Req0),

View file

@ -942,6 +942,11 @@ stream_body_sendfile_fin(Config) ->
{200, _, ExpectedBody} = do_get("/resp/stream_body/sendfile_fin", Config), {200, _, ExpectedBody} = do_get("/resp/stream_body/sendfile_fin", Config),
ok. ok.
stream_body_spawn(Config) ->
doc("Confirm we can use cowboy_req:stream_body/3 from another process."),
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body/spawn", Config),
ok.
stream_body_content_length_multiple(Config) -> stream_body_content_length_multiple(Config) ->
doc("Streamed body via multiple calls."), doc("Streamed body via multiple calls."),
{200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/multiple", Config), {200, _, <<"Hello world!">>} = do_get("/resp/stream_body_content_length/multiple", Config),