| 1 |
%% This module implements a loop handler that streams endless data. |
| 2 |
|
| 3 |
-module(loop_handler_endless_h). |
| 4 |
|
| 5 |
-export([init/2]). |
| 6 |
-export([info/3]). |
| 7 |
|
| 8 |
init(Req0, #{delay := Delay} = Opts) -> |
| 9 |
case cowboy_req:header(<<"x-test-pid">>, Req0) of |
| 10 |
BinPid when is_binary(BinPid) -> |
| 11 |
Pid = list_to_pid(binary_to_list(BinPid)), |
| 12 |
Pid ! {Pid, self(), init}, |
| 13 |
ok; |
| 14 |
_ -> |
| 15 |
ok |
| 16 |
end, |
| 17 |
erlang:send_after(Delay, self(), timeout), |
| 18 |
Req = cowboy_req:stream_reply(200, Req0), |
| 19 |
{cowboy_loop, Req, Opts}. |
| 20 |
|
| 21 |
info(timeout, Req, State) -> |
| 22 |
cowboy_req:stream_body(<<0:10000/unit:8>>, nofin, Req), |
| 23 |
%% Equivalent to a 0 timeout. |
| 24 |
self() ! timeout, |
| 25 |
{ok, Req, State}. |