Skip to content

Commit 25c5474

Browse files
committed
Add ability to specify retries and timeout for failover RADIUS servers
Previously we may specifiy the 'retries' and 'timeout' configuration options for the RADIUS relay servers. This commit adds ability to specify the same configuration options per RADIUS failover server. Backward compatibility is preserved. The proxy clients already had this options, they were not just used during the request but instead the RADIUS client used the timeout and retries values from the primary server configuration.
1 parent 5fc23c6 commit 25c5474

File tree

5 files changed

+17
-28
lines changed

5 files changed

+17
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ All pools are configured via:
305305
%%% ...
306306
{servers_pool, [
307307
{pool_name, [
308-
{{127, 0, 0, 2}, 1812, <<"secret">>, [{retries, 3}]},
308+
{{127, 0, 0, 2}, 1812, <<"secret">>, [{retries, 3}, {timeout, 3000}]},
309309
{{127, 0, 0, 3}, 1812, <<"secret">>}
310310
]}
311311
]}

src/eradius_client.erl

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
-module(eradius_client).
1414
-export([start_link/0, send_request/2, send_request/3, send_remote_request/3, send_remote_request/4]).
1515
%% internal
16-
-export([reconfigure/0, send_remote_request_loop/8, find_suitable_peer/1,
16+
-export([reconfigure/0, send_remote_request_loop/8,
1717
restore_upstream_server/1, store_radius_server_from_pool/3,
1818
init_server_status_metrics/0]).
1919

@@ -92,21 +92,14 @@ send_request({IP, Port, Secret}, Request, Options) when ?GOOD_CMD(Request) andal
9292
[] ->
9393
SendReqFn();
9494
UpstreamServers ->
95-
case find_suitable_peer([{IP, Port, Secret} | UpstreamServers]) of
95+
case find_suitable_peer([{IP, Port, Secret, Options} | UpstreamServers]) of
9696
[] ->
9797
no_active_servers;
98-
{{IP, Port, Secret}, _NewPool} ->
98+
{{IP, Port, Secret, Options}, _NewPool} ->
9999
SendReqFn();
100-
{NewPeer, []} ->
101-
% Special case, we don't have servers in the pool anymore, but we need
102-
% to preserve `failover` option to mark current server as inactive if
103-
% it will fail
104-
NewOptions = lists:keyreplace(failover, 1, Options, {failover, undefined}),
105-
send_request(NewPeer, Request, NewOptions);
106-
{NewPeer, NewPool} ->
100+
{{NewPeerAddr, NewPeerPort, NewPeerSecret, PeerOpts}, NewPool} ->
107101
% current server is not in list of active servers, so use another one
108-
NewOptions = lists:keyreplace(failover, 1, Options, {failover, NewPool}),
109-
send_request(NewPeer, Request, NewOptions)
102+
send_request({NewPeerAddr, NewPeerPort, NewPeerSecret}, Request, [{failover, NewPool} | PeerOpts])
110103
end
111104
end;
112105
send_request({_IP, _Port, _Secret}, _Request, _Options) ->
@@ -223,10 +216,8 @@ handle_failed_request(Request, {ServerIP, Port} = _FailedServer, UpstreamServers
223216
case find_suitable_peer(UpstreamServers) of
224217
[] ->
225218
Response;
226-
{NewPeer, NewPool} ->
227-
% leave only active upstream servers
228-
NewOptions = lists:keyreplace(failover, 1, Options, {failover, NewPool}),
229-
send_request(NewPeer, Request, NewOptions)
219+
{{NewPeerAddr, NewPeerPort, NewPeerSecret, PeerOpts}, NewPool} ->
220+
send_request({NewPeerAddr, NewPeerPort, NewPeerSecret}, Request, [{failover, NewPool} | PeerOpts])
230221
end.
231222

232223
% @private
@@ -632,27 +623,25 @@ client_response_counter_account_match_spec_compile() ->
632623
MatchSpecCompile
633624
end.
634625

635-
find_suitable_peer(undefined) ->
636-
[];
637626
find_suitable_peer([]) ->
638627
[];
639-
find_suitable_peer([{Host, Port, Secret} | Pool]) when is_list(Host) ->
628+
find_suitable_peer([{Host, Port, Secret, Opts} | Pool]) when is_list(Host) ->
640629
try
641630
IP = get_ip(Host),
642-
find_suitable_peer([{IP, Port, Secret} | Pool])
631+
find_suitable_peer([{IP, Port, Secret, Opts} | Pool])
643632
catch _:_ ->
644633
% can't resolve ip by some reasons, just ignore it
645634
find_suitable_peer(Pool)
646635
end;
647-
find_suitable_peer([{IP, Port, Secret} | Pool]) ->
636+
find_suitable_peer([{IP, Port, Secret, Opts} | Pool]) ->
648637
case ets:lookup(?MODULE, {IP, Port}) of
649638
[] ->
650639
find_suitable_peer(Pool);
651640
[{{IP, Port}, _Retries, _InitialRetries}] ->
652-
{{IP, Port, Secret}, Pool}
641+
{{IP, Port, Secret, Opts}, Pool}
653642
end;
654-
find_suitable_peer([{IP, Port, Secret, _Opts} | Pool]) ->
655-
find_suitable_peer([{IP, Port, Secret} | Pool]).
643+
find_suitable_peer([{IP, Port, Secret} | Pool]) ->
644+
find_suitable_peer([{IP, Port, Secret, []} | Pool]).
656645

657646
get_ip(Host) ->
658647
case inet:gethostbyname(Host) of

src/eradius_proxy.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
%%
2424
%% ```
2525
%% {servers_pool, [{pool_name, [
26-
%% {{127, 0, 0, 1}, 1815, <<"secret">>, [{retries, 3}]},
26+
%% {{127, 0, 0, 1}, 1815, <<"secret">>, [{retries, 3}, {timeout, 5000}]},
2727
%% {{127, 0, 0, 1}, 1816, <<"secret">>}]}]}
2828
%% '''
2929
%%

test/eradius_client_SUITE.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
-include("test/eradius_test.hrl").
2525

2626
-define(BAD_SERVER_IP, {eradius_test_handler:localhost(ip), 1820, "secret"}).
27-
-define(BAD_SERVER_INITIAL_RETRIES, 3).
27+
-define(BAD_SERVER_INITIAL_RETRIES, 2).
2828
-define(BAD_SERVER_TUPLE_INITIAL, {{eradius_test_handler:localhost(tuple), 1820},
2929
?BAD_SERVER_INITIAL_RETRIES,
3030
?BAD_SERVER_INITIAL_RETRIES}).

test/eradius_test_handler.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ start() ->
2020
application:set_env(eradius, unreachable_timeout, 2),
2121
application:set_env(eradius, servers_pool, [{test_pool, [{localhost(tuple), 1812, "secret"},
2222
% fake upstream server for fail-over
23-
{localhost(string), 1820, "secret"}]}]),
23+
{localhost(string), 1820, "secret", [{timeout, 1000}, {retries, 2}]}]}]),
2424
application:ensure_all_started(eradius),
2525
eradius:modules_ready([?MODULE]).
2626

0 commit comments

Comments
 (0)