Debian Codegraph @

test/handlers/loop_handler_timeout_init_h.erl

raw
1 %% This module implements a loop handler that reads
2 %% the request query for a timeout value, then sends
3 %% itself a message after 1000ms. It replies a 200 when
4 %% the message does not timeout and a 299 otherwise.
5
6 -module(loop_handler_timeout_init_h).
7
8 -export([init/2]).
9 -export([info/3]).
10 -export([terminate/3]).
11
12 init(Req, _) ->
13 #{timeout := Timeout} = cowboy_req:match_qs([{timeout, int}], Req),
14 erlang:send_after(500, self(), message),
15 {cowboy_loop, Req, undefined, Timeout}.
16
17 info(message, Req, State) ->
18 {stop, cowboy_req:reply(200, Req), State};
19 info(timeout, Req, State) ->
20 {stop, cowboy_req:reply(<<"299 OK!">>, Req), State}.
21
22 terminate(stop, _, _) ->
23 ok.