loop_handler_timeout_hibernate_h.erl 838 B

123456789101112131415161718192021222324252627282930
  1. %% This module implements a loop handler that first
  2. %% sets a timeout, then hibernates, then ensures
  3. %% that the timeout initially set no longer triggers.
  4. %% If everything goes fine a 200 is returned. If the
  5. %% timeout triggers again a 299 is.
  6. -module(loop_handler_timeout_hibernate_h).
  7. -export([init/2]).
  8. -export([info/3]).
  9. -export([terminate/3]).
  10. init(Req, _) ->
  11. self() ! message1,
  12. {cowboy_loop, Req, undefined, 100}.
  13. info(message1, Req, State) ->
  14. erlang:send_after(200, self(), message2),
  15. {ok, Req, State, hibernate};
  16. info(message2, Req, State) ->
  17. erlang:send_after(200, self(), message3),
  18. %% Don't set a timeout now.
  19. {ok, Req, State};
  20. info(message3, Req, State) ->
  21. {stop, cowboy_req:reply(200, Req), State};
  22. info(timeout, Req, State) ->
  23. {stop, cowboy_req:reply(<<"299 OK!">>, Req), State}.
  24. terminate(stop, _, _) ->
  25. ok.