Permalink
Browse files

Switched from using process dictionary to mnesia

  • Loading branch information...
1 parent 287216e commit 0f509ee066613ec30398e2e9b5cbf345c51ca034 @0x6e6562 0x6e6562 committed Jul 24, 2009
Showing with 112 additions and 17 deletions.
  1. +4 −2 ebin/rfc4627_jsonrpc.app
  2. +36 −0 src/rfc4627_jsonrpc_app.erl
  3. +31 −15 src/rfc4627_jsonrpc_registry.erl
  4. +41 −0 src/rfc4627_jsonrpc_sup.erl
View
@@ -4,12 +4,14 @@
{modules, [
rfc4627,
rfc4627_jsonrpc,
+ rfc4627_jsonrpc_app,
+ rfc4627_jsonrpc_sup,
rfc4627_jsonrpc_http,
rfc4627_jsonrpc_inets,
rfc4627_jsonrpc_mochiweb,
rfc4627_jsonrpc_registry
]},
{registered, []},
- {mod, {rfc4627_jsonrpc, []}},
+ {mod, {rfc4627_jsonrpc_app, []}},
{env, []},
- {applications, [kernel, stdlib]}]}.
+ {applications, [kernel, stdlib]}]}.
@@ -0,0 +1,36 @@
+% The contents of this file are subject to the Mozilla Public License
+%% Version 1.1 (the "License"); you may not use this file except in
+%% compliance with the License. You may obtain a copy of the License at
+%% http://www.mozilla.org/MPL/
+%%
+%% Software distributed under the License is distributed on an "AS IS"
+%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+%% License for the specific language governing rights and limitations
+%% under the License.
+%%
+%% The Original Code is the RabbitMQ BQL Module
+%%
+%% The Initial Developers of the Original Code are LShift Ltd.,
+%% Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.
+%%
+%% Portions created by LShift Ltd., Cohesive Financial
+%% Technologies LLC., and Rabbit Technologies Ltd. are Copyright (C)
+%% 2009 LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit
+%% Technologies Ltd.;
+%%
+%% All Rights Reserved.
+%%
+%% Contributor(s): ___________________________
+%%
+-module(rfc4627_jsonrpc_app).
+
+-export([start/0, stop/0, start/2, stop/1]).
+
+start() -> application:start(?MODULE).
+stop() -> application:stop(?MODULE).
+
+start(normal, []) ->
+ rfc4627_jsonrpc_sup:start_link().
+
+stop(_State) ->
+ ok.
@@ -39,8 +39,16 @@
-export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2]).
+-define(TABLE_NAME, rfc4627_jsonrpc_registry).
+
%% @doc gen_server behaviour callback.
init(_Args) ->
+ case mnesia:create_table(?TABLE_NAME, [{attributes,[key, value]}]) of
+ {atomic,ok} ->
+ error_logger:error_msg("Creating new ETS table for ~p~n", [?MODULE]),
+ ok;
+ {aborted, {already_exists, ?TABLE_NAME}} -> ok
+ end,
{ok, no_jsonrpc_state}.
%% @doc gen_server behaviour callback.
@@ -54,18 +62,22 @@ code_change(_OldVsn, State, _Extra) ->
%% @doc gen_server behaviour callback.
handle_call({lookup_service, Service}, _From, State) ->
- case get({service, Service}) of
- undefined ->
- {reply, not_found, State};
- ServiceRec ->
- {reply, ServiceRec, State}
+ Fun = fun() -> mnesia:read(?TABLE_NAME, {service, Service}) end,
+ case mnesia:transaction(Fun) of
+ {atomic, []} ->
+ {reply, not_found, State};
+ {atomic, [{?TABLE_NAME, _, ServiceRec}]} ->
+ {reply, ServiceRec, State}
end;
handle_call({register_service, Pid, ServiceDescription}, _From, State) ->
SD = ServiceDescription#service{handler = {pid, Pid}},
erlang:monitor(process, Pid),
- put({service_pid, Pid}, SD#service.name),
- put({service, SD#service.name}, SD),
+ mnesia:transaction(
+ fun() ->
+ mnesia:write({?TABLE_NAME,{service_pid, Pid}, SD#service.name}),
+ mnesia:write({?TABLE_NAME,{service, SD#service.name}, SD})
+ end),
{reply, ok, State}.
%% @doc gen_server behaviour callback.
@@ -75,12 +87,16 @@ handle_cast(Request, State) ->
%% @doc gen_server behaviour callback.
handle_info({'DOWN', _MonitorRef, process, DownPid, _Reason}, State) ->
- case get({service_pid, DownPid}) of
- undefined ->
- %% How strange.
- {noreply, State};
- ServiceName ->
- erase({service_pid, DownPid}),
- erase({service, ServiceName}),
- {noreply, State}
+ Fun = fun() -> mnesia:read(?TABLE_NAME, {service_pid, DownPid}) end,
+ case mnesia:transaction(Fun) of
+ {atomic, []} ->
+ {noreply, State};
+ {atomic, [{?TABLE_NAME, _, ServiceName}]} ->
+ mnesia:transaction(
+ fun() ->
+ mnesia:delete({?TABLE_NAME,{service_pid, DownPid}}),
+ mnesia:write({?TABLE_NAME,{service, ServiceName}})
+ end),
+ {noreply, State}
end.
+
@@ -0,0 +1,41 @@
+% The contents of this file are subject to the Mozilla Public License
+%% Version 1.1 (the "License"); you may not use this file except in
+%% compliance with the License. You may obtain a copy of the License at
+%% http://www.mozilla.org/MPL/
+%%
+%% Software distributed under the License is distributed on an "AS IS"
+%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+%% License for the specific language governing rights and limitations
+%% under the License.
+%%
+%% The Original Code is the RabbitMQ BQL Module
+%%
+%% The Initial Developers of the Original Code are LShift Ltd.,
+%% Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.
+%%
+%% Portions created by LShift Ltd., Cohesive Financial
+%% Technologies LLC., and Rabbit Technologies Ltd. are Copyright (C)
+%% 2009 LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit
+%% Technologies Ltd.;
+%%
+%% All Rights Reserved.
+%%
+%% Contributor(s): ___________________________
+%%
+-module(rfc4627_jsonrpc_sup).
+-behaviour(supervisor).
+
+-export([start_link/0, init/1]).
+
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, _Arg = []).
+
+init([]) ->
+ {ok, {{one_for_one, 3, 10},
+ [{rfc4627_jsonrpc,
+ {rfc4627_jsonrpc, start_link, []},
+ permanent,
+ 10000,
+ worker,
+ [rfc4627_jsonrpc]}
+ ]}}.

0 comments on commit 0f509ee

Please sign in to comment.