Skip to content

Commit

Permalink
Allow websocket handlers to hibernate from the websocket_init/3 function
Browse files Browse the repository at this point in the history
Also improve the documentation about hibernate.
  • Loading branch information
Loïc Hoguin committed Sep 15, 2011
1 parent 89ae3c8 commit 04f55eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/cowboy_http_websocket.erl
Expand Up @@ -116,9 +116,15 @@ handler_init(State=#state{handler=Handler, opts=Opts},
try Handler:websocket_init(Transport:name(), Req, Opts) of
{ok, Req2, HandlerState} ->
websocket_handshake(State, Req2, HandlerState);
{ok, Req2, HandlerState, hibernate} ->
websocket_handshake(State#state{hibernate=true},
Req2, HandlerState);
{ok, Req2, HandlerState, Timeout} ->
websocket_handshake(State#state{timeout=Timeout},
Req2, HandlerState)
Req2, HandlerState);
{ok, Req2, HandlerState, Timeout, hibernate} ->
websocket_handshake(State#state{timeout=Timeout,
hibernate=true}, Req2, HandlerState)
catch Class:Reason ->
upgrade_error(Req),
error_logger:error_msg(
Expand Down
8 changes: 6 additions & 2 deletions src/cowboy_http_websocket_handler.erl
Expand Up @@ -29,8 +29,7 @@
%% here.
%%
%% <em>websocket_handle/3</em> receives the data from the socket. It can reply
%% something, do nothing or close the connection. You can choose to hibernate
%% the process by returning <em>hibernate</em> to save memory and CPU.
%% something, do nothing or close the connection.
%%
%% <em>websocket_info/3</em> receives messages sent to the process. It has
%% the same reply format as <em>websocket_handle/3</em> described above. Note
Expand All @@ -41,6 +40,11 @@
%% <em>websocket_terminate/3</em> is meant for cleaning up. It also receives
%% the request and the state previously defined, along with a reason for
%% termination.
%%
%% All of <em>websocket_init/3</em>, <em>websocket_handle/3</em> and
%% <em>websocket_info/3</em> can decide to hibernate the process by adding
%% an extra element to the returned tuple, containing the atom
%% <em>hibernate</em>. Doing so helps save memory and improve CPU usage.
-module(cowboy_http_websocket_handler).

-export([behaviour_info/1]).
Expand Down

0 comments on commit 04f55eb

Please sign in to comment.