mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Add timeout to cowboy_loop
LH: I have added a test that does both hibernate and timeout and fixed a related issue. I also tweaked the docs and tests.
This commit is contained in:
parent
a72bf4105f
commit
a81dc8af9d
8 changed files with 165 additions and 25 deletions
23
test/handlers/loop_handler_timeout_init_h.erl
Normal file
23
test/handlers/loop_handler_timeout_init_h.erl
Normal file
|
@ -0,0 +1,23 @@
|
|||
%% This module implements a loop handler that reads
|
||||
%% the request query for a timeout value, then sends
|
||||
%% itself a message after 1000ms. It replies a 200 when
|
||||
%% the message does not timeout and a 299 otherwise.
|
||||
|
||||
-module(loop_handler_timeout_init_h).
|
||||
|
||||
-export([init/2]).
|
||||
-export([info/3]).
|
||||
-export([terminate/3]).
|
||||
|
||||
init(Req, _) ->
|
||||
#{timeout := Timeout} = cowboy_req:match_qs([{timeout, int}], Req),
|
||||
erlang:send_after(200, self(), message),
|
||||
{cowboy_loop, Req, undefined, Timeout}.
|
||||
|
||||
info(message, Req, State) ->
|
||||
{stop, cowboy_req:reply(200, Req), State};
|
||||
info(timeout, Req, State) ->
|
||||
{stop, cowboy_req:reply(<<"299 OK!">>, Req), State}.
|
||||
|
||||
terminate(stop, _, _) ->
|
||||
ok.
|
Loading…
Add table
Add a link
Reference in a new issue