loop_handler_timeout_init_h.erl 673 B

1234567891011121314151617181920212223
  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. -module(loop_handler_timeout_init_h).
  6. -export([init/2]).
  7. -export([info/3]).
  8. -export([terminate/3]).
  9. init(Req, _) ->
  10. #{timeout := Timeout} = cowboy_req:match_qs([{timeout, int}], Req),
  11. erlang:send_after(200, self(), message),
  12. {cowboy_loop, Req, undefined, Timeout}.
  13. info(message, Req, State) ->
  14. {stop, cowboy_req:reply(200, Req), State};
  15. info(timeout, Req, State) ->
  16. {stop, cowboy_req:reply(<<"299 OK!">>, Req), State}.
  17. terminate(stop, _, _) ->
  18. ok.