Skip to content

Commit c6e5a10

Browse files
committed
Replace infinite timeouts with sensible defaults.
Change gen_server:call timeouts to use a configurable default that is cached inside each process' process dictionary. Also make supervisor shutdown timeouts use the SUPERVISOR_WAIT value. [#147178169]
1 parent af13d84 commit c6e5a10

14 files changed

+54
-32
lines changed

deps/amqp_client/include/amqp_client.hrl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
channel_max = 0,
3131
frame_max = 0,
3232
heartbeat = 10,
33-
connection_timeout = infinity,
33+
connection_timeout = 30000,
3434
ssl_options = none,
3535
auth_mechanisms =
3636
[fun amqp_auth_mechanisms:plain/3,

deps/amqp_client/include/amqp_client_internal.hrl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@
3535
{<<"consumer_cancel_notify">>, bool, true},
3636
{<<"connection.blocked">>, bool, true},
3737
{<<"authentication_failure_close">>, bool, true}]).
38+
39+
-define(CALL_TIMEOUT, rabbit_misc:get_env(amqp_client, gen_server_call_timeout,
40+
30000)).
41+

deps/amqp_client/src/amqp_channel.erl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
%% @spec (Channel, Method) -> Result
141141
%% @doc This is equivalent to amqp_channel:call(Channel, Method, none).
142142
call(Channel, Method) ->
143-
gen_server:call(Channel, {call, Method, none, self()}, infinity).
143+
gen_server:call(Channel, {call, Method, none, self()}, amqp_util:call_timeout()).
144144

145145
%% @spec (Channel, Method, Content) -> Result
146146
%% where
@@ -163,7 +163,7 @@ call(Channel, Method) ->
163163
%% the broker. It does not necessarily imply that the broker has
164164
%% accepted responsibility for the message.
165165
call(Channel, Method, Content) ->
166-
gen_server:call(Channel, {call, Method, Content, self()}, infinity).
166+
gen_server:call(Channel, {call, Method, Content, self()}, amqp_util:call_timeout()).
167167

168168
%% @spec (Channel, Method) -> ok
169169
%% @doc This is equivalent to amqp_channel:cast(Channel, Method, none).
@@ -208,15 +208,15 @@ close(Channel) ->
208208
%% @doc Closes the channel, allowing the caller to supply a reply code and
209209
%% text. If the channel is already closing, the atom 'closing' is returned.
210210
close(Channel, Code, Text) ->
211-
gen_server:call(Channel, {close, Code, Text}, infinity).
211+
gen_server:call(Channel, {close, Code, Text}, amqp_util:call_timeout()).
212212

213213
%% @spec (Channel) -> integer()
214214
%% where
215215
%% Channel = pid()
216216
%% @doc When in confirm mode, returns the sequence number of the next
217217
%% message to be published.
218218
next_publish_seqno(Channel) ->
219-
gen_server:call(Channel, next_publish_seqno, infinity).
219+
gen_server:call(Channel, next_publish_seqno, amqp_util:call_timeout()).
220220

221221
%% @spec (Channel) -> boolean() | 'timeout'
222222
%% where
@@ -225,7 +225,7 @@ next_publish_seqno(Channel) ->
225225
%% been either ack'd or nack'd by the broker. Note, when called on a
226226
%% non-Confirm channel, waitForConfirms returns an error.
227227
wait_for_confirms(Channel) ->
228-
wait_for_confirms(Channel, infinity).
228+
wait_for_confirms(Channel, amqp_util:call_timeout()).
229229

230230
%% @spec (Channel, Timeout) -> boolean() | 'timeout'
231231
%% where
@@ -236,7 +236,7 @@ wait_for_confirms(Channel) ->
236236
%% Note, when called on a non-Confirm channel, waitForConfirms throws
237237
%% an exception.
238238
wait_for_confirms(Channel, Timeout) ->
239-
case gen_server:call(Channel, {wait_for_confirms, Timeout}, infinity) of
239+
case gen_server:call(Channel, {wait_for_confirms, Timeout}, amqp_util:call_timeout()) of
240240
{error, Reason} -> throw(Reason);
241241
Other -> Other
242242
end.
@@ -248,7 +248,7 @@ wait_for_confirms(Channel, Timeout) ->
248248
%% received, the calling process is immediately sent an
249249
%% exit(nack_received).
250250
wait_for_confirms_or_die(Channel) ->
251-
wait_for_confirms_or_die(Channel, infinity).
251+
wait_for_confirms_or_die(Channel, amqp_util:call_timeout()).
252252

253253
%% @spec (Channel, Timeout) -> true
254254
%% where
@@ -328,7 +328,7 @@ unregister_flow_handler(Channel) ->
328328
%% where Consumer is the amqp_gen_consumer implementation registered with
329329
%% the channel.
330330
call_consumer(Channel, Msg) ->
331-
gen_server:call(Channel, {call_consumer, Msg}, infinity).
331+
gen_server:call(Channel, {call_consumer, Msg}, amqp_util:call_timeout()).
332332

333333
%% @spec (Channel, BasicConsume, Subscriber) -> ok
334334
%% where
@@ -338,7 +338,7 @@ call_consumer(Channel, Msg) ->
338338
%% @doc Subscribe the given pid to a queue using the specified
339339
%% basic.consume method.
340340
subscribe(Channel, BasicConsume = #'basic.consume'{}, Subscriber) ->
341-
gen_server:call(Channel, {subscribe, BasicConsume, Subscriber}, infinity).
341+
gen_server:call(Channel, {subscribe, BasicConsume, Subscriber}, amqp_util:call_timeout()).
342342

343343
%%---------------------------------------------------------------------------
344344
%% Internal interface
@@ -364,7 +364,7 @@ connection_closing(Pid, ChannelCloseType, Reason) ->
364364

365365
%% @private
366366
open(Pid) ->
367-
gen_server:call(Pid, open, infinity).
367+
gen_server:call(Pid, open, amqp_util:call_timeout()).
368368

369369
%%---------------------------------------------------------------------------
370370
%% gen_server callbacks
@@ -993,3 +993,4 @@ call_to_consumer(Method, Args, DeliveryCtx, #state{consumer = Consumer}) ->
993993

994994
safe_cancel_timer(undefined) -> ok;
995995
safe_cancel_timer(TRef) -> erlang:cancel_timer(TRef).
996+

deps/amqp_client/src/amqp_channels_manager.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ start_link(Connection, ConnName, ChSupSup) ->
4343

4444
open_channel(ChMgr, ProposedNumber, Consumer, InfraArgs) ->
4545
gen_server:call(ChMgr, {open_channel, ProposedNumber, Consumer, InfraArgs},
46-
infinity).
46+
amqp_util:call_timeout()).
4747

4848
set_channel_max(ChMgr, ChannelMax) ->
4949
gen_server:cast(ChMgr, {set_channel_max, ChannelMax}).
5050

5151
is_empty(ChMgr) ->
52-
gen_server:call(ChMgr, is_empty, infinity).
52+
gen_server:call(ChMgr, is_empty, amqp_util:call_timeout()).
5353

5454
num_channels(ChMgr) ->
55-
gen_server:call(ChMgr, num_channels, infinity).
55+
gen_server:call(ChMgr, num_channels, amqp_util:call_timeout()).
5656

5757
pass_frame(ChMgr, ChNumber, Frame) ->
5858
gen_server:cast(ChMgr, {pass_frame, ChNumber, Frame}).

deps/amqp_client/src/amqp_connection.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
%% defaults to 0 (turned off) (network only)</li>
122122
%% <li>connection_timeout :: non_neg_integer() | 'infinity'
123123
%% - The connection timeout in milliseconds,
124-
%% defaults to 'infinity' (network only)</li>
124+
%% defaults to 30000 (network only)</li>
125125
%% <li>ssl_options :: term() - The second parameter to be used with the
126126
%% ssl:connect/2 function, defaults to 'none' (network only)</li>
127127
%% <li>client_properties :: [{binary(), atom(), binary()}] - A list of extra
@@ -278,7 +278,7 @@ close(ConnectionPid, Timeout) ->
278278
%% @doc Closes the AMQP connection, allowing the caller to set the reply
279279
%% code and text.
280280
close(ConnectionPid, Code, Text) ->
281-
close(ConnectionPid, Code, Text, infinity).
281+
close(ConnectionPid, Code, Text, amqp_util:call_timeout()).
282282

283283
%% @spec (ConnectionPid, Code, Text, Timeout) -> ok | closing
284284
%% where

deps/amqp_client/src/amqp_connection_sup.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ start_link(AMQPParams) ->
3333
{ok, TypeSup} = supervisor2:start_child(
3434
Sup, {connection_type_sup,
3535
{amqp_connection_type_sup, start_link, []},
36-
transient, infinity, supervisor,
36+
transient, ?SUPERVISOR_WAIT, supervisor,
3737
[amqp_connection_type_sup]}),
3838
{ok, Connection} = supervisor2:start_child(
3939
Sup, {connection, {amqp_gen_connection, start_link,

deps/amqp_client/src/amqp_connection_type_sup.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ start_channels_manager(Sup, Conn, ConnName, Type) ->
4242
Sup,
4343
{channel_sup_sup, {amqp_channel_sup_sup, start_link,
4444
[Type, Conn, ConnName]},
45-
intrinsic, infinity, supervisor,
45+
intrinsic, ?SUPERVISOR_WAIT, supervisor,
4646
[amqp_channel_sup_sup]}),
4747
{ok, _} = supervisor2:start_child(
4848
Sup,

deps/amqp_client/src/amqp_gen_connection.erl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ start_link(TypeSup, AMQPParams) ->
5555
gen_server:start_link(?MODULE, {TypeSup, AMQPParams}, []).
5656

5757
connect(Pid) ->
58-
gen_server:call(Pid, connect, infinity).
58+
gen_server:call(Pid, connect, amqp_util:call_timeout()).
5959

6060
open_channel(Pid, ProposedNumber, Consumer) ->
6161
case gen_server:call(Pid,
6262
{command, {open_channel, ProposedNumber, Consumer}},
63-
infinity) of
63+
amqp_util:call_timeout()) of
6464
{ok, ChannelPid} -> ok = amqp_channel:open(ChannelPid),
6565
{ok, ChannelPid};
6666
Error -> Error
@@ -79,19 +79,19 @@ channels_terminated(Pid) ->
7979
gen_server:cast(Pid, channels_terminated).
8080

8181
close(Pid, Close, Timeout) ->
82-
gen_server:call(Pid, {command, {close, Close, Timeout}}, infinity).
82+
gen_server:call(Pid, {command, {close, Close, Timeout}}, amqp_util:call_timeout()).
8383

8484
server_close(Pid, Close) ->
8585
gen_server:cast(Pid, {server_close, Close}).
8686

8787
info(Pid, Items) ->
88-
gen_server:call(Pid, {info, Items}, infinity).
88+
gen_server:call(Pid, {info, Items}, amqp_util:call_timeout()).
8989

9090
info_keys() ->
9191
?INFO_KEYS.
9292

9393
info_keys(Pid) ->
94-
gen_server:call(Pid, info_keys, infinity).
94+
gen_server:call(Pid, info_keys, amqp_util:call_timeout()).
9595

9696
%%---------------------------------------------------------------------------
9797
%% Behaviour

deps/amqp_client/src/amqp_gen_consumer.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ start_link(ConsumerModule, ExtraParams, Identity) ->
5858
%% @doc This function is used to perform arbitrary calls into the
5959
%% consumer module.
6060
call_consumer(Pid, Msg) ->
61-
gen_server2:call(Pid, {consumer_call, Msg}, infinity).
61+
gen_server2:call(Pid, {consumer_call, Msg}, amqp_util:call_timeout()).
6262

6363
%% @spec (Consumer, Method, Args) -> ok
6464
%% where
@@ -69,10 +69,10 @@ call_consumer(Pid, Msg) ->
6969
%% @doc This function is used by amqp_channel to forward received
7070
%% methods and deliveries to the consumer module.
7171
call_consumer(Pid, Method, Args) ->
72-
gen_server2:call(Pid, {consumer_call, Method, Args}, infinity).
72+
gen_server2:call(Pid, {consumer_call, Method, Args}, amqp_util:call_timeout()).
7373

7474
call_consumer(Pid, Method, Args, DeliveryCtx) ->
75-
gen_server2:call(Pid, {consumer_call, Method, Args, DeliveryCtx}, infinity).
75+
gen_server2:call(Pid, {consumer_call, Method, Args, DeliveryCtx}, amqp_util:call_timeout()).
7676

7777
%%---------------------------------------------------------------------------
7878
%% Behaviour

deps/amqp_client/src/amqp_main_reader.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ init([Sock, Connection, ConnName, ChMgr, AState]) ->
5151
channels_manager = ChMgr,
5252
astate = AState,
5353
message = none},
54-
case rabbit_net:async_recv(Sock, 0, infinity) of
54+
case rabbit_net:async_recv(Sock, 0, amqp_util:call_timeout()) of
5555
{ok, _} -> {ok, State};
5656
{error, Reason} -> {stop, Reason, _} = handle_error(Reason, State),
5757
{stop, Reason}
@@ -72,7 +72,7 @@ handle_cast(Cast, State) ->
7272
handle_info({inet_async, Sock, _, {ok, Data}},
7373
State = #state {sock = Sock}) ->
7474
%% Latency hiding: Request next packet first, then process data
75-
case rabbit_net:async_recv(Sock, 0, infinity) of
75+
case rabbit_net:async_recv(Sock, 0, amqp_util:call_timeout()) of
7676
{ok, _} -> handle_data(Data, State);
7777
{error, Reason} -> handle_error(Reason, State)
7878
end;

deps/amqp_client/src/amqp_rpc_client.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ start_link(Connection, Queue) ->
7070
%% RpcClient = pid()
7171
%% @doc Stops an exisiting RPC client.
7272
stop(Pid) ->
73-
gen_server:call(Pid, stop, infinity).
73+
gen_server:call(Pid, stop, amqp_util:call_timeout()).
7474

7575
%% @spec (RpcClient, Payload) -> ok
7676
%% where
@@ -79,7 +79,7 @@ stop(Pid) ->
7979
%% @doc Invokes an RPC. Note the caller of this function is responsible for
8080
%% encoding the request and decoding the response.
8181
call(RpcClient, Payload) ->
82-
gen_server:call(RpcClient, {call, Payload}, infinity).
82+
gen_server:call(RpcClient, {call, Payload}, amqp_util:call_timeout()).
8383

8484
%%--------------------------------------------------------------------------
8585
%% Plumbing

deps/amqp_client/src/amqp_rpc_server.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ start_link(Connection, Queue, Fun) ->
7070
%% RpcServer = pid()
7171
%% @doc Stops an exisiting RPC server.
7272
stop(Pid) ->
73-
gen_server:call(Pid, stop, infinity).
73+
gen_server:call(Pid, stop, amqp_util:call_timeout()).
7474

7575
%%--------------------------------------------------------------------------
7676
%% gen_server callbacks

deps/amqp_client/src/amqp_sup.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ start_connection_sup(AmqpParams) ->
4444
init([]) ->
4545
{ok, {{simple_one_for_one, 0, 1},
4646
[{connection_sup, {amqp_connection_sup, start_link, []},
47-
temporary, infinity, supervisor, [amqp_connection_sup]}]}}.
47+
temporary, ?SUPERVISOR_WAIT, supervisor, [amqp_connection_sup]}]}}.

deps/amqp_client/src/amqp_util.erl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-module(amqp_util).
2+
3+
-include("amqp_client_internal.hrl").
4+
5+
-export([call_timeout/0]).
6+
7+
call_timeout() ->
8+
case get(gen_server_call_timeout) of
9+
undefined ->
10+
Timeout = rabbit_misc:get_env(amqp_client,
11+
gen_server_call_timeout,
12+
?CALL_TIMEOUT),
13+
put(gen_server_call_timeout, Timeout),
14+
Timeout;
15+
Timeout ->
16+
Timeout
17+
end.

0 commit comments

Comments
 (0)