Skip to content

Commit

Permalink
consul experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbbell committed Apr 26, 2024
1 parent 93077fc commit ff507a5
Showing 1 changed file with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

-define(CONSUL_CHECK_NOTES, "RabbitMQ Consul-based peer discovery plugin TTL check").

-define(REGISTRATION_VERSION, <<"2">>).

%%
%% API
%%
Expand Down Expand Up @@ -76,6 +78,7 @@ list_nodes() ->
maybe_add_acl([]),
HttpOpts) of
{ok, Nodes} ->
?LOG_DEBUG("Consul discovery: ~p", [Nodes]),
IncludeWithWarnings = get_config_key(consul_include_nodes_with_warnings, M),
Result = extract_nodes(
filter_nodes(Nodes, IncludeWithWarnings)),
Expand Down Expand Up @@ -260,18 +263,32 @@ extract_nodes(Data) -> extract_nodes(Data, []).
-> list().
extract_nodes([], Nodes) -> Nodes;
extract_nodes([H | T], Nodes) ->
Service = maps:get(<<"Service">>, H),
Value = maps:get(<<"Address">>, Service),
NodeName = case ?UTIL_MODULE:as_string(Value) of
"" ->
NodeData = maps:get(<<"Node">>, H),
Node = maps:get(<<"Node">>, NodeData),
maybe_add_domain(?UTIL_MODULE:node_name(Node));
Address ->
?UTIL_MODULE:node_name(Address)
Service = maps:get(<<"Service">>, H),
Key = registration_version_key(),
Meta = maps:get(<<"Meta">>, Service, #{}),
RegistrationVersion = maps:get(Key, Meta, 1),
NodeName = case RegistrationVersion of
<<"2">> ->
ID = maps:get(<<"ID">>, Service),
maybe_add_domain(?UTIL_MODULE:node_name(ID));
<<"1">> ->
Value = maps:get(<<"Address">>, Service),
case ?UTIL_MODULE:as_string(Value) of
"" ->
NodeData = maps:get(<<"Node">>, H),
Node = maps:get(<<"Node">>, NodeData),
maybe_add_domain(?UTIL_MODULE:node_name(Node));
Address ->
?UTIL_MODULE:node_name(Address)
end
end,
extract_nodes(T, lists:merge(Nodes, [NodeName])).

registration_version_key() ->
list_to_binary(
rabbit_misc:format(
"~s-registration-version", [?MODULE])).

-spec maybe_add_acl(QArgs :: list()) -> list().
maybe_add_acl(List) ->
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
Expand Down Expand Up @@ -420,8 +437,10 @@ registration_body_maybe_add_tag(Payload, Cluster, Tags) ->
registration_body_maybe_add_meta(Payload) ->
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
ClusterName = get_config_key(cluster_name, M),
Meta = ?UTIL_MODULE:as_list(get_config_key(consul_svc_meta, M)),
registration_body_maybe_add_meta(Payload, ClusterName, Meta).
Meta0 = ?UTIL_MODULE:as_list(get_config_key(consul_svc_meta, M)),
Key = registration_version_key(),
Meta1 = lists:keystore(Key, 1, Meta0, {Key, ?REGISTRATION_VERSION}),
registration_body_maybe_add_meta(Payload, ClusterName, Meta1).

-spec registration_body_maybe_add_meta(Payload :: list(),
ClusterName :: string(),
Expand Down Expand Up @@ -483,14 +502,16 @@ service_address(_, false, NIC, _) ->

-spec service_id() -> string().
service_id() ->
M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
service_id(get_config_key(consul_svc, M),
service_address()).

-spec service_id(Name :: string(), Address :: string()) -> string().
service_id(Service, "undefined") -> Service;
service_id(Service, Address) ->
string:join([Service, Address], ":").
ThisNode = node(),
atom_to_list(ThisNode).
%M = ?CONFIG_MODULE:config_map(?BACKEND_CONFIG_KEY),
%service_id(get_config_key(consul_svc, M),
%service_address()).

%-spec service_id(Name :: string(), Address :: string()) -> string().
%service_id(Service, "undefined") -> Service;
%service_id(Service, Address) ->
%string:join([Service, Address], ":").

-spec service_ttl(TTL :: integer()) -> string().
service_ttl(Value) ->
Expand Down

0 comments on commit ff507a5

Please sign in to comment.