| 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 |
|
| 7 |
-module(loop_handler_timeout_hibernate_h). |
| 8 |
|
| 9 |
-export([init/2]). |
| 10 |
-export([info/3]). |
| 11 |
-export([terminate/3]). |
| 12 |
|
| 13 |
init(Req, _) -> |
| 14 |
self() ! message1, |
| 15 |
{cowboy_loop, Req, undefined, 100}. |
| 16 |
|
| 17 |
info(message1, Req, State) -> |
| 18 |
erlang:send_after(200, self(), message2), |
| 19 |
{ok, Req, State, hibernate}; |
| 20 |
info(message2, Req, State) -> |
| 21 |
erlang:send_after(200, self(), message3), |
| 22 |
%% Don't set a timeout now. |
| 23 |
{ok, Req, State}; |
| 24 |
info(message3, Req, State) -> |
| 25 |
{stop, cowboy_req:reply(200, Req), State}; |
| 26 |
info(timeout, Req, State) -> |
| 27 |
{stop, cowboy_req:reply(<<"299 OK!">>, Req), State}. |
| 28 |
|
| 29 |
terminate(stop, _, _) -> |
| 30 |
ok. |