Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Make Federation connections (links) specify a name
Browse files Browse the repository at this point in the history
Fixes #39
  • Loading branch information
acogoluegnes committed Oct 13, 2016
1 parent b034bf7 commit 2539871
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
37 changes: 29 additions & 8 deletions src/rabbit_federation_link_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
-export([start_conn_ch/5, disposable_channel_call/2, disposable_channel_call/3,
disposable_connection_call/3, ensure_connection_closed/1,
log_terminate/4, unacked_new/0, ack/3, nack/3, forward/9,
handle_down/6]).
handle_down/6, get_connection_name/2]).

%% temp
-export([connection_error/6]).
Expand All @@ -36,7 +36,7 @@

start_conn_ch(Fun, Upstream, UParams,
XorQName = #resource{virtual_host = DownVHost}, State) ->
case open_monitor(#amqp_params_direct{virtual_host = DownVHost}) of
case open_monitor(#amqp_params_direct{virtual_host = DownVHost}, get_connection_name(Upstream, UParams)) of
{ok, DConn, DCh} ->
case Upstream#upstream.ack_mode of
'on-confirm' ->
Expand All @@ -46,7 +46,7 @@ start_conn_ch(Fun, Upstream, UParams,
_ ->
ok
end,
case open_monitor(UParams#upstream_params.params) of
case open_monitor(UParams#upstream_params.params, get_connection_name(Upstream, UParams)) of
{ok, Conn, Ch} ->
%% Don't trap exits until we have established
%% connections so that if we try to delete
Expand Down Expand Up @@ -82,15 +82,36 @@ start_conn_ch(Fun, Upstream, UParams,
Upstream, UParams, XorQName, State)
end.

open_monitor(Params) ->
case open(Params) of
get_connection_name(#upstream{name = UpstreamName},
#upstream_params{x_or_q = Resource}) when is_record(Resource, exchange)->
Policy = Resource#exchange.policy,
PolicyName = proplists:get_value(name, Policy),
connection_name(UpstreamName, PolicyName);

get_connection_name(#upstream{name = UpstreamName},
#upstream_params{x_or_q = Resource}) when is_record(Resource, amqqueue)->
Policy = Resource#amqqueue.policy,
PolicyName = proplists:get_value(name, Policy),
connection_name(UpstreamName, PolicyName);

get_connection_name(_, _) ->
connection_name(undefined, undefined).

connection_name(Upstream, Policy) when is_binary(Upstream), is_binary(Policy) ->
<<<<"Federation ">>/binary, Upstream/binary, <<" ">>/binary, Policy/binary>>;

connection_name(_, _) ->
<<"Federation">>.

open_monitor(Params, Name) ->
case open(Params, Name) of
{ok, Conn, Ch} -> erlang:monitor(process, Ch),
{ok, Conn, Ch};
E -> E
end.

open(Params) ->
case amqp_connection:start(Params) of
open(Params, Source) ->
case amqp_connection:start(Params, Source) of
{ok, Conn} -> case amqp_connection:open_channel(Conn) of
{ok, Ch} -> {ok, Conn, Ch};
E -> catch amqp_connection:close(Conn),
Expand Down Expand Up @@ -273,7 +294,7 @@ disposable_channel_call(Conn, Method, ErrFun) ->
end.

disposable_connection_call(Params, Method, ErrFun) ->
case open(Params) of
case open(Params, undefined) of
{ok, Conn, Ch} ->
try
amqp_channel:call(Ch, Method)
Expand Down
26 changes: 25 additions & 1 deletion test/unit_inbroker_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ groups() ->
{non_parallel_tests, [], [
serialisation,
scratch_space,
remove_credentials
remove_credentials,
get_connection_name
]}
].

Expand Down Expand Up @@ -139,6 +140,29 @@ remove_credentials(Config) ->
Test(<<"amqps://">>, <<"localhost:5672/%2f">>),
ok.

get_connection_name(_Config) ->
<<"Federation my.upstream my.federation.policy">> = rabbit_federation_link_util:get_connection_name(
#upstream{name = <<"my.upstream">>},
#upstream_params{x_or_q = #amqqueue{policy = [{name, <<"my.federation.policy">>}]}}
),
<<"Federation my.upstream my.federation.policy">> = rabbit_federation_link_util:get_connection_name(
#upstream{name = <<"my.upstream">>},
#upstream_params{x_or_q = #exchange{policy = [{name, <<"my.federation.policy">>}]}}
),
<<"Federation">> = rabbit_federation_link_util:get_connection_name(
#upstream{},
#upstream_params{x_or_q = #amqqueue{policy = []}}
),
<<"Federation">> = rabbit_federation_link_util:get_connection_name(
#upstream{},
#upstream_params{x_or_q = #exchange{policy = []}}
),
<<"Federation">> = rabbit_federation_link_util:get_connection_name(
whatever,
whatever
),
ok.

with_exchanges(Fun) ->
rabbit_exchange:declare(r(?US_NAME), fanout, false, false, false, []),
X = rabbit_exchange:declare(r(?DS_NAME), fanout, false, false, false, []),
Expand Down

0 comments on commit 2539871

Please sign in to comment.