0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00
cowboy/test/handlers/streamed_result_h.erl
Robert J. Macomber f74b69c3ed
Optionally reset the idle timeout when sending data
A new option reset_idle_timeout_on_send has been added.
When set to 'true', the idle timeout is reset not only
when data is received, but also when data is sent.

This allows sending large responses without having to
worry about timeouts triggering.

The default is currently unchanged but might change in
a future release.

LH: Greatly reworked the implementation so that the
    timeout gets reset on almost all socket writes.
	This essentially completely supersets the original
	work. Tests are mostly the same although I
	refactored a bit to avoid test code duplication.

This commit also changes HTTP/2 behavior a little when
data is received: Cowboy will not attempt to update the
window before running stream handler commands to avoid
sending WINDOW_UPDATE frames twice. Now it has some
small heuristic to ensure they can only be sent once
at most.
2023-12-21 14:03:07 +01:00

20 lines
623 B
Erlang

-module(streamed_result_h).
-export([init/2]).
init(Req, Opts) ->
N = list_to_integer(binary_to_list(cowboy_req:binding(n, Req))),
Interval = list_to_integer(binary_to_list(cowboy_req:binding(interval, Req))),
chunked(N, Interval, Req, Opts).
chunked(N, Interval, Req0, Opts) ->
Req = cowboy_req:stream_reply(200, Req0),
{ok, loop(N, Interval, Req), Opts}.
loop(0, _Interval, Req) ->
ok = cowboy_req:stream_body("Finished!\n", fin, Req),
Req;
loop(N, Interval, Req) ->
ok = cowboy_req:stream_body(iolist_to_binary([integer_to_list(N), <<"\n">>]), nofin, Req),
timer:sleep(Interval),
loop(N-1, Interval, Req).