Skip to content

Commit

Permalink
Merge branch 'cancel-timer' of https://github.com/etrepum/cowboy
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Hoguin committed May 3, 2012
2 parents dab6648 + 4a8ba14 commit 1524ba4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/cowboy_http_protocol.erl
Expand Up @@ -324,16 +324,15 @@ handler_loop_timeout(State=#state{loop_timeout=Timeout,
loop_timeout_ref=PrevRef}) ->
_ = case PrevRef of undefined -> ignore; PrevRef ->
erlang:cancel_timer(PrevRef) end,
TRef = make_ref(),
erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}),
TRef = erlang:start_timer(Timeout, self(), ?MODULE),
State#state{loop_timeout_ref=TRef}.

-spec handler_loop(any(), #http_req{}, #state{}) -> ok.
handler_loop(HandlerState, Req, State=#state{loop_timeout_ref=TRef}) ->
receive
{?MODULE, timeout, TRef} ->
{timeout, TRef, ?MODULE} ->
terminate_request(HandlerState, Req, State);
{?MODULE, timeout, OlderTRef} when is_reference(OlderTRef) ->
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
handler_loop(HandlerState, Req, State);
Message ->
handler_call(HandlerState, Req, State, Message)
Expand Down
7 changes: 3 additions & 4 deletions src/cowboy_http_websocket.erl
Expand Up @@ -232,8 +232,7 @@ handler_loop_timeout(State=#state{timeout=infinity}) ->
handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) ->
_ = case PrevRef of undefined -> ignore; PrevRef ->
erlang:cancel_timer(PrevRef) end,
TRef = make_ref(),
erlang:send_after(Timeout, self(), {?MODULE, timeout, TRef}),
TRef = erlang:start_timer(Timeout, self(), ?MODULE),
State#state{timeout_ref=TRef}.

%% @private
Expand All @@ -248,9 +247,9 @@ handler_loop(State=#state{messages={OK, Closed, Error}, timeout_ref=TRef},
handler_terminate(State, Req, HandlerState, {error, closed});
{Error, Socket, Reason} ->
handler_terminate(State, Req, HandlerState, {error, Reason});
{?MODULE, timeout, TRef} ->
{timeout, TRef, ?MODULE} ->
websocket_close(State, Req, HandlerState, {normal, timeout});
{?MODULE, timeout, OlderTRef} when is_reference(OlderTRef) ->
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
handler_loop(State, Req, HandlerState, SoFar);
Message ->
handler_call(State, Req, HandlerState,
Expand Down

0 comments on commit 1524ba4

Please sign in to comment.