From 04f55eb3c925705da28b3509b2f7e6b203632391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 15 Sep 2011 23:13:27 +0200 Subject: Allow websocket handlers to hibernate from the websocket_init/3 function Also improve the documentation about hibernate. --- src/cowboy_http_websocket.erl | 8 +++++++- src/cowboy_http_websocket_handler.erl | 8 ++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl index 8e951a5..74c5d7a 100644 --- a/src/cowboy_http_websocket.erl +++ b/src/cowboy_http_websocket.erl @@ -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( diff --git a/src/cowboy_http_websocket_handler.erl b/src/cowboy_http_websocket_handler.erl index 90cf7ac..2ea0a46 100644 --- a/src/cowboy_http_websocket_handler.erl +++ b/src/cowboy_http_websocket_handler.erl @@ -29,8 +29,7 @@ %% here. %% %% websocket_handle/3 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 hibernate to save memory and CPU. +%% something, do nothing or close the connection. %% %% websocket_info/3 receives messages sent to the process. It has %% the same reply format as websocket_handle/3 described above. Note @@ -41,6 +40,11 @@ %% websocket_terminate/3 is meant for cleaning up. It also receives %% the request and the state previously defined, along with a reason for %% termination. +%% +%% All of websocket_init/3, websocket_handle/3 and +%% websocket_info/3 can decide to hibernate the process by adding +%% an extra element to the returned tuple, containing the atom +%% hibernate. Doing so helps save memory and improve CPU usage. -module(cowboy_http_websocket_handler). -export([behaviour_info/1]). -- cgit v1.2.3