Skip to content

Commit 1669658

Browse files
authored
Move some functions to grisp_connect_utils (#59)
1 parent fdeef91 commit 1669658

File tree

2 files changed

+67
-49
lines changed

2 files changed

+67
-49
lines changed

src/grisp_connect_client.erl

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ waiting_ip(enter, _OldState, _Data) ->
163163
% First IP check do not have any delay
164164
{keep_state_and_data, [{state_timeout, 0, check_ip}]};
165165
waiting_ip(state_timeout, check_ip, Data) ->
166-
case check_inet_ipv4() of
166+
case grisp_connect_utils:check_inet_ipv4() of
167167
{ok, IP} ->
168168
?LOG_INFO(#{event => checked_ip, ip => IP}),
169169
{next_state, connecting, Data};
@@ -174,17 +174,8 @@ waiting_ip(state_timeout, check_ip, Data) ->
174174
?HANDLE_COMMON.
175175

176176
% @doc State connecting is used to establish a connection to grisp.io.
177-
connecting(enter, _OldState, #data{retry_count = 0}) ->
178-
{keep_state_and_data, [{state_timeout, 0, connect}]};
179177
connecting(enter, _OldState, #data{retry_count = RetryCount}) ->
180-
%% Calculate the connection delay in milliseconds with exponential backoff.
181-
%% The delay is selected randomly between `1000' and
182-
%% `2 ^ RETRY_COUNT - 1000' with a maximum value of `64000'.
183-
%% Loosely inspired by https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
184-
MinDelay = 1000,
185-
MaxDelay = 64000,
186-
MaxRandomDelay = min(MaxDelay, (1 bsl RetryCount) * 1000) - MinDelay,
187-
Delay = MinDelay + rand:uniform(MaxRandomDelay),
178+
Delay = grisp_connect_utils:retry_delay(RetryCount),
188179
?LOG_DEBUG("Scheduling connection attempt in ~w ms", [Delay]),
189180
{keep_state_and_data, [{state_timeout, Delay, connect}]};
190181
connecting(state_timeout, connect, Data = #data{conn = undefined}) ->
@@ -405,41 +396,3 @@ conn_error(#data{conn = Conn}, Code, Message, ErData, ReqRef)
405396
conn_error(Data, Code, Message, ErData, ReqRef) ->
406397
BinErData = iolist_to_binary(io_lib:format("~p", [ErData])),
407398
conn_error(Data, Code, Message, BinErData, ReqRef).
408-
409-
% IP check functions
410-
411-
check_inet_ipv4() ->
412-
case get_ip_of_valid_interfaces() of
413-
{IP1,_,_,_} = IP when IP1 =/= 127 -> {ok, IP};
414-
_ -> invalid
415-
end.
416-
417-
get_ipv4_from_opts([]) ->
418-
undefined;
419-
get_ipv4_from_opts([{addr, {_1, _2, _3, _4}} | _]) ->
420-
{_1, _2, _3, _4};
421-
get_ipv4_from_opts([_ | TL]) ->
422-
get_ipv4_from_opts(TL).
423-
424-
has_ipv4(Opts) ->
425-
get_ipv4_from_opts(Opts) =/= undefined.
426-
427-
flags_are_ok(Flags) ->
428-
lists:member(up, Flags) and
429-
lists:member(running, Flags) and
430-
not lists:member(loopback, Flags).
431-
432-
get_valid_interfaces() ->
433-
{ok, Interfaces} = inet:getifaddrs(),
434-
[
435-
Opts
436-
|| {_Name, [{flags, Flags} | Opts]} <- Interfaces,
437-
flags_are_ok(Flags),
438-
has_ipv4(Opts)
439-
].
440-
441-
get_ip_of_valid_interfaces() ->
442-
case get_valid_interfaces() of
443-
[Opts | _] -> get_ipv4_from_opts(Opts);
444-
_ -> undefined
445-
end.

src/grisp_connect_utils.erl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-module(grisp_connect_utils).
2+
3+
4+
%--- Exports -------------------------------------------------------------------
5+
6+
% API functions
7+
-export([retry_delay/1]).
8+
-export([check_inet_ipv4/0]).
9+
10+
11+
%--- API Functions -------------------------------------------------------------
12+
13+
check_inet_ipv4() ->
14+
case get_ip_of_valid_interfaces() of
15+
{ok, {IP1, _, _, _} = IP} when IP1 =/= 127 -> {ok, IP};
16+
_ -> invalid
17+
end.
18+
19+
20+
%--- Internal Functions --------------------------------------------------------
21+
22+
retry_delay(0) ->
23+
0;
24+
retry_delay(RetryCount) ->
25+
%% Calculate the connection delay in milliseconds with exponential backoff.
26+
%% The delay is selected randomly between `1000' and
27+
%% `2 ^ RETRY_COUNT - 1000' with a maximum value of `64000'.
28+
%% Loosely inspired by https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
29+
MinDelay = 1000,
30+
MaxDelay = 64000,
31+
MaxRandomDelay = min(MaxDelay, (1 bsl RetryCount) * 1000) - MinDelay,
32+
MinDelay + rand:uniform(MaxRandomDelay).
33+
34+
get_ipv4_from_opts([]) ->
35+
undefined;
36+
get_ipv4_from_opts([{addr, {_1, _2, _3, _4}} | _]) ->
37+
{ok, {_1, _2, _3, _4}};
38+
get_ipv4_from_opts([_ | TL]) ->
39+
case get_ipv4_from_opts(TL) of
40+
{ok, _IP} = Result -> Result;
41+
Other -> Other
42+
end.
43+
44+
has_ipv4(Opts) ->
45+
get_ipv4_from_opts(Opts) =/= undefined.
46+
47+
flags_are_ok(Flags) ->
48+
lists:member(up, Flags) and
49+
lists:member(running, Flags) and
50+
not lists:member(loopback, Flags).
51+
52+
get_valid_interfaces() ->
53+
case inet:getifaddrs() of
54+
{error, _Reason} = Error -> Error;
55+
{ok, Interfaces} ->
56+
{ok, [Opts || {_Name, [{flags, Flags} | Opts]} <- Interfaces,
57+
flags_are_ok(Flags), has_ipv4(Opts)]}
58+
end.
59+
60+
get_ip_of_valid_interfaces() ->
61+
case get_valid_interfaces() of
62+
{error, _Reason} = Error -> Error;
63+
{ok, [Opts | _]} -> get_ipv4_from_opts(Opts);
64+
_ -> undefined
65+
end.

0 commit comments

Comments
 (0)