From fe1fe6b2ca2ebefd0b7e82b6651e35f6388a06d0 Mon Sep 17 00:00:00 2001 From: Zandra Date: Fri, 22 Apr 2016 15:37:50 +0200 Subject: let the spawned processes handle rpc replies This decreases the load on the rex server. --- lib/kernel/src/rpc.erl | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) (limited to 'lib/kernel/src/rpc.erl') diff --git a/lib/kernel/src/rpc.erl b/lib/kernel/src/rpc.erl index 6f9ce17c0c..ee71d39253 100644 --- a/lib/kernel/src/rpc.erl +++ b/lib/kernel/src/rpc.erl @@ -130,30 +130,16 @@ handle_cast(_, S) -> -spec handle_info(term(), state()) -> {'noreply', state()}. +handle_info({'DOWN', _, process, Caller, normal}, S) -> + {noreply, gb_trees:delete(Caller, S)}; handle_info({'DOWN', _, process, Caller, Reason}, S) -> case gb_trees:lookup(Caller, S) of - {value, To} -> - receive - {Caller, {reply, Reply}} -> - gen_server:reply(To, Reply) - after 0 -> - gen_server:reply(To, {badrpc, {'EXIT', Reason}}) - end, + {value, To} -> + gen_server:reply(To, {badrpc, {'EXIT', Reason}}), {noreply, gb_trees:delete(Caller, S)}; none -> {noreply, S} end; -handle_info({Caller, {reply, Reply}}, S) -> - case gb_trees:lookup(Caller, S) of - {value, To} -> - receive - {'DOWN', _, process, Caller, _} -> - gen_server:reply(To, Reply), - {noreply, gb_trees:delete(Caller, S)} - end; - none -> - {noreply, S} - end; handle_info({From, {sbcast, Name, Msg}}, S) -> _ = case catch Name ! Msg of %% use catch to get the printout {'EXIT', _} -> @@ -190,7 +176,6 @@ code_change(_, S, _) -> %% Auxiliary function to avoid a false dialyzer warning -- do not inline %% handle_call_call(Mod, Fun, Args, Gleader, To, S) -> - RpcServer = self(), %% Spawn not to block the rpc server. {Caller,_} = erlang:spawn_monitor( @@ -205,7 +190,7 @@ handle_call_call(Mod, Fun, Args, Gleader, To, S) -> Result -> Result end, - RpcServer ! {self(), {reply, Reply}} + gen_server:reply(To, Reply) end), {noreply, gb_trees:insert(Caller, To, S)}. -- cgit v1.2.3 From 0c2954dd7fa1923a26d7da05e366bca63d5fe53e Mon Sep 17 00:00:00 2001 From: Zandra Date: Mon, 25 Apr 2016 09:05:30 +0200 Subject: use maps instead of gb_trees to optimize the rpc server --- lib/kernel/src/rpc.erl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'lib/kernel/src/rpc.erl') diff --git a/lib/kernel/src/rpc.erl b/lib/kernel/src/rpc.erl index ee71d39253..21bff02214 100644 --- a/lib/kernel/src/rpc.erl +++ b/lib/kernel/src/rpc.erl @@ -63,7 +63,7 @@ %%------------------------------------------------------------------------ --type state() :: gb_trees:tree(pid(), {pid(), reference()}). +-type state() :: map(). %%------------------------------------------------------------------------ @@ -91,7 +91,7 @@ stop(Rpc) -> init([]) -> process_flag(trap_exit, true), - {ok, gb_trees:empty()}. + {ok, maps:new()}. -spec handle_call(term(), term(), state()) -> {'noreply', state()} | @@ -131,14 +131,14 @@ handle_cast(_, S) -> -spec handle_info(term(), state()) -> {'noreply', state()}. handle_info({'DOWN', _, process, Caller, normal}, S) -> - {noreply, gb_trees:delete(Caller, S)}; + {noreply, maps:remove(Caller, S)}; handle_info({'DOWN', _, process, Caller, Reason}, S) -> - case gb_trees:lookup(Caller, S) of - {value, To} -> + case maps:get(Caller, S, undefined) of + undefined -> + {noreply, S}; + {_, _} = To -> gen_server:reply(To, {badrpc, {'EXIT', Reason}}), - {noreply, gb_trees:delete(Caller, S)}; - none -> - {noreply, S} + {noreply, maps:remove(Caller, S)} end; handle_info({From, {sbcast, Name, Msg}}, S) -> _ = case catch Name ! Msg of %% use catch to get the printout @@ -192,7 +192,7 @@ handle_call_call(Mod, Fun, Args, Gleader, To, S) -> end, gen_server:reply(To, Reply) end), - {noreply, gb_trees:insert(Caller, To, S)}. + {noreply, maps:put(Caller, To, S)}. %% RPC aid functions .... -- cgit v1.2.3