Permalink
Browse files

add group to manage connection pid

  • Loading branch information...
1 parent 51cad2b commit 484b67fb63afe25f7552d18af802e034b4282916 Stone Shi committed Apr 13, 2012
Showing with 23 additions and 22 deletions.
  1. +23 −22 src/lager_amqp_backend.erl
View
@@ -127,29 +127,30 @@ config_val(C, Params, Default) ->
end.
amqp_channel(AmqpParams) ->
- % io:format("amqp params: ~p~n", [AmqpParams]),
- case pg2:get_closest_pid(AmqpParams) of
- {error, {no_such_group, _}} ->
- pg2:create(AmqpParams),
- amqp_channel(AmqpParams);
- {error, {no_process, _}} ->
- % io:format("no client running~n"),
- case amqp_connection:start(AmqpParams) of
- {ok, Client} ->
- % io:format("started new client: ~p~n", [Client]),
- case amqp_connection:open_channel(Client) of
- {ok, Channel} ->
- pg2:join(AmqpParams, Channel),
- {ok, Channel};
- {error, Reason} -> {error, Reason}
- end;
- {error, Reason} ->
- % io:format("encountered an error: ~p~n", [Reason]),
- {error, Reason}
+ case maybe_new_pid({AmqpParams, connection},
+ fun() -> amqp_connection:start(AmqpParams) end) of
+ {ok, Client} ->
+ maybe_new_pid({AmqpParams, channel},
+ fun() -> amqp_connection:open_channel(Client) end);
+ Error ->
+ Error
+ end.
+
+maybe_new_pid(Group, StartFun) ->
+ case pg2:get_closest_pid(Group) of
+ {error, {no_such_group, _}} ->
+ pg2:create(Group),
+ maybe_new_pid(Group, StartFun);
+ {error, {no_process, _}} ->
+ case StartFun() of
+ {ok, Pid} ->
+ pg2:join(Group, Pid),
+ {ok, Pid};
+ Error ->
+ Error
end;
- Channel ->
- % io:format("using existing channel: ~p~n", [Channel]),
- {ok, Channel}
+ Pid ->
+ {ok, Pid}
end.
test() ->

0 comments on commit 484b67f

Please sign in to comment.