-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
313 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
%%%------------------------------------------------------------------- | ||
%% @doc Backoff Handling | ||
%% | ||
%% Based on `db_connection': | ||
%% [https://github.com/elixir-ecto/db_connection/blob/8ef1f2ea54922873590b8939f2dad6b031c5b49c/lib/db_connection/backoff.ex#L24] | ||
%% @end | ||
%% @since 3.2.0 | ||
%%%------------------------------------------------------------------- | ||
-module(oidcc_backoff). | ||
|
||
-export_type([type/0]). | ||
-export_type([min/0]). | ||
-export_type([max/0]). | ||
-export_type([state/0]). | ||
|
||
-export([handle_retry/4]). | ||
|
||
-type type() :: stop | exponential | random | random_exponential. | ||
|
||
-type min() :: pos_integer(). | ||
|
||
-type max() :: pos_integer(). | ||
|
||
-opaque state() :: pos_integer() | {pos_integer(), pos_integer()}. | ||
|
||
%% @private | ||
-spec handle_retry(Type, Min, Max, State) -> stop | {Wait, State} when | ||
Type :: type(), Min :: min(), Max :: max(), State :: undefined | state(), Wait :: pos_integer(). | ||
handle_retry(Type, Min, Max, State) when Min > 0, Max > 0, Max >= Min -> | ||
priv_handle_retry(Type, Min, Max, State). | ||
|
||
-spec priv_handle_retry(Type, Min, Max, State) -> stop | {Wait, State} when | ||
Type :: type(), Min :: min(), Max :: max(), State :: undefined | state(), Wait :: pos_integer(). | ||
priv_handle_retry(stop, _Min, _Max, undefined) -> | ||
stop; | ||
priv_handle_retry(random, Min, Max, State) -> | ||
{rand(Min, Max), State}; | ||
priv_handle_retry(exponential, Min, _Max, undefined) -> | ||
{Min, Min}; | ||
priv_handle_retry(exponential, _Min, Max, State) -> | ||
Wait = min(State * 2, Max), | ||
{Wait, Wait}; | ||
priv_handle_retry(random_exponential, Min, Max, undefined) -> | ||
Lower = max(Min, Max div 3), | ||
priv_handle_retry(random_exponential, Min, Max, {Lower, Lower}); | ||
priv_handle_retry(random_exponential, _Min, Max, {Prev, Lower}) -> | ||
NextMin = min(Prev, Lower), | ||
NextMax = min(Prev * 3, Max), | ||
Next = rand(NextMin, NextMax), | ||
priv_handle_retry(random, NextMin, NextMax, {Next, Lower}). | ||
|
||
rand(Min, Max) -> rand:uniform(Max - Min + 1) + Min - 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
%% Based on https://github.com/elixir-ecto/db_connection/blob/8ef1f2ea54922873590b8939f2dad6b031c5b49c/test/db_connection/backoff_test.exs | ||
|
||
-module(oidcc_backoff_test). | ||
|
||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
exp_backoff_in_min_max_test() -> | ||
Min = 1000, | ||
Max = 30000, | ||
lists:map( | ||
fun(Retry) -> | ||
?assertMatch({wait, _}, Retry), | ||
|
||
{wait, Wait} = Retry, | ||
|
||
?assert(Wait >= Min), | ||
?assert(Wait =< Max) | ||
end, | ||
calculate_backoffs(20, exponential, Min, Max) | ||
). | ||
|
||
exp_backoff_double_until_max_test() -> | ||
Min = 1000, | ||
Max = 30000, | ||
lists:foldl( | ||
fun | ||
({wait, Wait}, undefined) -> | ||
Wait; | ||
(Retry, Prev) -> | ||
?assertMatch({wait, _}, Retry), | ||
|
||
{wait, Wait} = Retry, | ||
|
||
?assert(((Wait div 2) =:= Prev) or (Wait =:= Max)), | ||
|
||
Wait | ||
end, | ||
undefined, | ||
calculate_backoffs(20, exponential, Min, Max) | ||
). | ||
|
||
rand_backoff_in_min_max_test() -> | ||
Min = 1000, | ||
Max = 30000, | ||
lists:map( | ||
fun(Retry) -> | ||
?assertMatch({wait, _}, Retry), | ||
|
||
{wait, Wait} = Retry, | ||
|
||
?assert(Wait >= Min), | ||
?assert(Wait =< Max) | ||
end, | ||
calculate_backoffs(20, random, Min, Max) | ||
). | ||
|
||
rand_backoff_different_every_time_test() -> | ||
Min = 1000, | ||
Max = 30000, | ||
Comparison = calculate_backoffs(20, random, Min, Max), | ||
lists:map( | ||
fun(_) -> | ||
?assertNotEqual(Comparison, calculate_backoffs(20, random, Min, Max)) | ||
end, | ||
lists:seq(1, 100) | ||
). | ||
|
||
rand_exp_backoff_in_min_max_test() -> | ||
Min = 1000, | ||
Max = 30000, | ||
lists:map( | ||
fun(Retry) -> | ||
?assertMatch({wait, _}, Retry), | ||
|
||
{wait, Wait} = Retry, | ||
|
||
?assert(Wait >= Min), | ||
?assert(Wait =< Max) | ||
end, | ||
calculate_backoffs(20, random_exponential, Min, Max) | ||
). | ||
|
||
rand_exp_backoff_increase_until_third_max_test() -> | ||
Min = 1000, | ||
Max = 30000, | ||
lists:foldl( | ||
fun | ||
({wait, Wait}, undefined) -> | ||
Wait; | ||
(Retry, Prev) -> | ||
?assertMatch({wait, _}, Retry), | ||
|
||
{wait, Wait} = Retry, | ||
|
||
?assert((Wait >= Prev) or (Wait >= (Max div 3))), | ||
|
||
Wait | ||
end, | ||
undefined, | ||
calculate_backoffs(20, random_exponential, Min, Max) | ||
). | ||
|
||
calculate_backoffs(N, Type, Min, Max) -> | ||
lists:reverse(calculate_backoffs(N, Type, Min, Max, undefined, [])). | ||
|
||
calculate_backoffs(0, _Type, _Min, _Max, _State, Acc) -> | ||
Acc; | ||
calculate_backoffs(N, Type, Min, Max, State, Acc) -> | ||
case oidcc_backoff:handle_retry(Type, Min, Max, State) of | ||
stop -> | ||
[stop | Acc]; | ||
{Wait, NewState} -> | ||
calculate_backoffs(N - 1, Type, Min, Max, NewState, [{wait, Wait} | Acc]) | ||
end. |
Oops, something went wrong.