loop_handler_timeout_h.erl 631 B

1234567891011121314151617181920212223
  1. %% This module implements a loop handler that sends
  2. %% itself a timeout that will intentionally arrive
  3. %% too late, as it configures itself to only wait
  4. %% 200ms before closing the connection in init/3.
  5. %% This results in a 204 reply being sent back by Cowboy.
  6. -module(loop_handler_timeout_h).
  7. -behaviour(cowboy_loop_handler).
  8. -export([init/3]).
  9. -export([info/3]).
  10. -export([terminate/3]).
  11. init(_, Req, _) ->
  12. erlang:send_after(1000, self(), timeout),
  13. {loop, Req, undefined, 200, hibernate}.
  14. info(timeout, Req, State) ->
  15. {ok, Req2} = cowboy_req:reply(500, Req),
  16. {ok, Req2, State}.
  17. terminate({normal, timeout}, _, _) ->
  18. ok.