Permalink
Browse files

Make web socket handlers more robust to bad code by registering them

when the worker starts, so that if they crash, they get restarted and
reregistered.
  • Loading branch information...
1 parent 39a23fe commit 1d515dde7de81324a7293be4a47e4f09277363ed @davidw committed Jan 31, 2013
Showing with 6 additions and 6 deletions.
  1. +1 −2 src/boss/boss_service_sup.erl
  2. +5 −4 src/boss/boss_service_worker.erl
@@ -64,9 +64,8 @@ start_services(SupPid, Services) ->
({ServiceUrl, Service}, Acc) ->
{ok, ServicePid} =
supervisor:start_child(SupPid,
- {Service, {boss_service_worker, start_link, [Service]},
+ {Service, {boss_service_worker, start_link, [Service, ServiceUrl]},
permanent, 5000, worker, [Service]}),
- boss_websocket_router:register(ServiceUrl, Service),
Acc ++ [{ok, ServicePid}]
end,
[],
@@ -11,7 +11,7 @@
-behaviour(gen_server).
%% API
--export([start_link/1]).
+-export([start_link/2]).
-export([incoming/5, join/4, close/4, websockets/1, websockets_eval/2]).
%% gen_server callbacks
@@ -33,8 +33,8 @@
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
-start_link(Handler) when is_atom(Handler)->
- gen_server:start_link({global, Handler}, ?MODULE, [Handler], []).
+start_link(Handler, ServiceUrl) when is_atom(Handler)->
+ gen_server:start_link({global, Handler}, ?MODULE, [Handler, ServiceUrl], []).
incoming(Service, ServiceUrl, WebSocketId, SessionId, Msg) ->
gen_server:cast({global, Service}, {incoming_msg, ServiceUrl, WebSocketId, SessionId, Msg}).
@@ -67,10 +67,11 @@ websockets_eval(Service, Fun) ->
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-init([Handler]) when is_atom(Handler) ->
+init([Handler, ServiceUrl]) when is_atom(Handler) ->
% todo option to init
try Handler:init() of
{ok, Internal} ->
+ boss_websocket_router:register(ServiceUrl, Handler),
{ok, #state{handler=Handler, internal=Internal, websockets=sets:new()}}
catch Class:Reason ->
error_logger:error_msg(

0 comments on commit 1d515dd

Please sign in to comment.