@@ -163,7 +163,7 @@ waiting_ip(enter, _OldState, _Data) ->
163
163
% First IP check do not have any delay
164
164
{keep_state_and_data , [{state_timeout , 0 , check_ip }]};
165
165
waiting_ip (state_timeout , check_ip , Data ) ->
166
- case check_inet_ipv4 () of
166
+ case grisp_connect_utils : check_inet_ipv4 () of
167
167
{ok , IP } ->
168
168
? LOG_INFO (#{event => checked_ip , ip => IP }),
169
169
{next_state , connecting , Data };
@@ -174,17 +174,8 @@ waiting_ip(state_timeout, check_ip, Data) ->
174
174
? HANDLE_COMMON .
175
175
176
176
% @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 }]};
179
177
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 ),
188
179
? LOG_DEBUG (" Scheduling connection attempt in ~w ms" , [Delay ]),
189
180
{keep_state_and_data , [{state_timeout , Delay , connect }]};
190
181
connecting (state_timeout , connect , Data = # data {conn = undefined }) ->
@@ -405,41 +396,3 @@ conn_error(#data{conn = Conn}, Code, Message, ErData, ReqRef)
405
396
conn_error (Data , Code , Message , ErData , ReqRef ) ->
406
397
BinErData = iolist_to_binary (io_lib :format (" ~p " , [ErData ])),
407
398
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 .
0 commit comments