Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 2 contributors
Commits on Apr 13, 2012
Stone Shi add group to manage connection pid 484b67f
@jbrisbin Merge pull request #3 from zmstone/conn_mgr
add group to manage connection pid
900f500
Showing with 23 additions and 22 deletions.
  1. +23 −22 src/lager_amqp_backend.erl
View
45 src/lager_amqp_backend.erl
@@ -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() ->

No commit comments for this range

Something went wrong with that request. Please try again.