Skip to content

Commit

Permalink
extract and turn into util function
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro M. Ramallo <[email protected]>
  • Loading branch information
aramallo committed Dec 15, 2023
1 parent cecf148 commit 0b6e634
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# v5.0.0-rc.12
## Fixes
* Fix a bug causing fast forward to be disabled in full-mesh topologies
* Merged [PR #254](https://github.com/lasp-lang/partisan/pull/254) - Thanks Massimo Cesaro!

# v5.0.0-rc.11
## Fixes
Expand Down
19 changes: 3 additions & 16 deletions src/partisan_plumtree_broadcast.erl
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,7 @@ handle_cast({broadcast, MessageId, Message, Mod, Round, Root, From}, State) ->
"received {broadcast, ~p, Msg, ~p, ~p, ~p, ~p}",
[MessageId, Mod, Round, Root, From]
),
Valid =
case catch Mod:merge(MessageId, Message) of
Resp when is_atom(Resp) andalso true == Resp orelse false == Resp -> Resp;
_ -> false
end,
Valid = partisan_util:apply(Mod, merge, [MessageId, Message], false),
State1 = handle_broadcast(Valid, MessageId, Message, Mod, Round, Root, From, State),
{noreply, State1};

Expand All @@ -589,12 +585,7 @@ handle_cast({prune, Root, From}, State) ->

handle_cast({i_have, MessageId, Mod, Round, Root, From}, State) ->
?LOG_DEBUG("received ~p", [{i_have, MessageId, Mod, Round, Root, From}]),
Stale = % Mod:is_stale(MessageId),
case catch Mod:is_stale(MessageId) of
Resp when is_atom(Resp) andalso true == Resp orelse false == Resp -> Resp;
_ -> false
end,

Stale = partisan_util:apply(Mod, is_stale, [MessageId], false),
State1 = handle_ihave(Stale, MessageId, Mod, Round, Root, From, State),
{noreply, State1};

Expand All @@ -608,11 +599,7 @@ handle_cast({ignored_i_have, MessageId, Mod, Round, Root, From}, State) ->

handle_cast({graft, MessageId, Mod, Round, Root, From}, State) ->
?LOG_DEBUG("received ~p", [{graft, MessageId, Mod, Round, Root, From}]),
Result =
case catch Mod:graft(MessageId) of
Resp when is_atom(Resp) andalso true == Resp orelse false == Resp -> Resp;
_ -> false
end,
Result = partisan_util:apply(Mod, graft, [MessageId], false),
?LOG_DEBUG("graft(~p): ~p", [MessageId, Result]),
State1 = handle_graft(Result, MessageId, Mod, Round, Root, From, State),
{noreply, State1};
Expand Down
32 changes: 27 additions & 5 deletions src/partisan_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@
-include_lib("eunit/include/eunit.hrl").
-endif.

-export([maps_append/3]).
-export([apply/4]).
-export([encode/1]).
-export([encode/2]).
-export([maybe_connect_disterl/1]).
-export([maybe_pad_term/1]).
-export([format_posix_error/1]).
-export([get/2]).
-export([get/3]).
-export([format_posix_error/1]).
-export([maps_append/3]).
-export([maybe_connect_disterl/1]).
-export([maybe_pad_term/1]).
-export([parse_ip_address/1]).
-export([parse_port_nbr/1]).
-export([parse_listen_address/1]).
-export([parse_port_nbr/1]).



Expand Down Expand Up @@ -142,6 +143,27 @@ maps_append(Key, Value, Map) ->
).


%% -----------------------------------------------------------------------------
%% @doc
%% @end
%% -----------------------------------------------------------------------------
-spec apply(
Mod :: module(), Fun :: atom(), Args :: list(), Default :: any()) -> any().

apply(Mod, Fun, Args, Default) ->
erlang:function_exported(Mod, module_info, 0)
orelse code:ensure_loaded(Mod),

Arity = length(Args),

case erlang:function_exported(Mod, Fun, Arity) of
true ->
erlang:apply(Mod, Fun, Args);
false ->
Default
end.


%% -----------------------------------------------------------------------------
%% @doc
%% @end
Expand Down

0 comments on commit 0b6e634

Please sign in to comment.