[erlang-questions] Need parallel calls to gen_server:call, similar but different to gen_server:multi_call
Cameron Kerr
ckerr@REDACTED
Fri Oct 2 07:54:40 CEST 2009
Thanks for your feedback everyone. This is how I ended up doing it. I'd
appreciate any criticisms. If doing this long-term, I think I'd prefer
to extend the gen_server API. Thanks to David Mercer for his guidance.
%%%============================================================================
%%% Utility functions for dealing with gen_servers
%%%
%%% Author: Cameron Kerr <ckerr@REDACTED>
%%% Date: 1 October 2009
%%%============================================================================
-module(gen_server_util).
-include_lib("eunit/include/eunit.hrl").
-export([parallel_call/2]).
%% @spec parallel_call([Server_ref()], Request) -> [Response]
%%
%% Makes multiple gen_server calls, in parallel. Responses are
%% returned in the same order as they are found in the Servers list.
%%
%% FIXME: this is not robust if a call causes a process to fail.
%% In this case, we may end up getting a badrpc on subsequent
%% messages if the Servers list is not maintained.
%%
parallel_call(Servers, Request) ->
rpc:pmap({gen_server, call}, [Request], Servers).
parallel_call_test() ->
Servers_params = [{one,400}, {two,100}, {three,500}, {four,200}],
Servers_pids = [launch_responder(Response,Delay) || {Response,Delay}
<- Servers_params],
[one,two,three,four] = parallel_call(Servers_pids, ping),
parallel_call(Servers_pids, shutdown).
launch_responder(Response, Delay) ->
{ok, Pid} = parallel_call_test_server:start_link(Response, Delay),
Pid.
%%%-------------------------------------------------------------------
%%% File : parallel_call_test_server.erl
%%% Author : <Cameron Kerr@REDACTED>
%%% Description : Implements a simple server used for testing
%%% gen_server_util:parallel_call/2
%%%
%%% Created : 1 Oct 2009 by <Cameron Kerr@REDACTED>
%%%-------------------------------------------------------------------
-module(parallel_call_test_server).
-include_lib("eunit/include/eunit.hrl").
-behaviour(gen_server).
%% API
-export([start_link/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, {response, delay}).
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link(Response, Delay) ->
gen_server:start_link(?MODULE, [Response, Delay], []).
%%====================================================================
%% gen_server callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([Response, Delay]) ->
{ok, #state{response = Response, delay = Delay}}.
%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(ping, _From, State) ->
timer:sleep(State#state.delay),
?debugFmt("Server ~p responding to ping with response of ~p~n",
[self(), State#state.response]),
{reply, State#state.response, State};
handle_call(shutdown, _From, State) ->
?debugFmt("Server ~p (which responds with ~p) responding to shutdown
request~n", [self(), State#state.response]),
{stop, normal, ok, State};
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
More information about the erlang-questions
mailing list