diff options
author | teor <teor2345@gmail.com> | 2017-07-05 01:32:06 +1000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2017-07-07 13:18:04 -0400 |
commit | 32f0cbc0f6fc7a53fc88a144e42fea9aca2cd073 (patch) | |
tree | 6964e9d4f8456a9bc1b45125caf707f7341820fd | |
parent | f30d355903343af3611ed2ec9828c4b6d1851168 (diff) | |
download | tor-32f0cbc0f6fc7a53fc88a144e42fea9aca2cd073.tar.gz tor-32f0cbc0f6fc7a53fc88a144e42fea9aca2cd073.zip |
Refactor exponential backoff multipliers into macros
There are only so many times you can type "4".
-rw-r--r-- | src/or/directory.c | 8 | ||||
-rw-r--r-- | src/or/directory.h | 10 | ||||
-rw-r--r-- | src/test/test_dir.c | 2 |
3 files changed, 16 insertions, 4 deletions
diff --git a/src/or/directory.c b/src/or/directory.c index ea9d69b158..ccec810bae 100644 --- a/src/or/directory.c +++ b/src/or/directory.c @@ -11,6 +11,7 @@ #include "connection.h" #include "connection_edge.h" #include "control.h" +#define DIRECTORY_PRIVATE #include "directory.h" #include "dirserv.h" #include "dirvote.h" @@ -3778,7 +3779,8 @@ find_dl_min_and_max_delay(download_status_t *dls, const or_options_t *options, /** Advance one delay step. The algorithm is to use the previous delay to * compute an increment, we construct a value uniformly at random between - * delay+1 and (delay*4)+1 (or *3 in test networks). + * delay+1 and (delay*(DIR_DEFAULT_RANDOM_MULTIPLIER+1))+1 (or + * DIR_TEST_NET_RANDOM_MULTIPLIER in test networks). * We then clamp that value to be no larger than max_delay, and return it. * * Requires that delay is less than INT_MAX, and delay is in [0,max_delay]. @@ -3798,11 +3800,11 @@ next_random_exponential_delay(int delay, int max_delay) /* How much are we willing to add to the delay? */ int max_increment; - int multiplier = 3; /* no more than quadruple the previous delay */ + int multiplier = DIR_DEFAULT_RANDOM_MULTIPLIER; if (get_options()->TestingTorNetwork) { /* Decrease the multiplier in testing networks. This reduces the variance, * so that bootstrap is more reliable. */ - multiplier = 2; /* no more than triple the previous delay */ + multiplier = DIR_TEST_NET_RANDOM_MULTIPLIER; } if (delay && delay < (INT_MAX-1) / multiplier) { diff --git a/src/or/directory.h b/src/or/directory.h index c473d8e4ca..0b54943b60 100644 --- a/src/or/directory.h +++ b/src/or/directory.h @@ -177,5 +177,15 @@ STATIC int next_random_exponential_delay(int delay, int max_delay); #endif +#if defined(TOR_UNIT_TESTS) || defined(DIRECTORY_PRIVATE) +/* Used only by directory.c and test_dir.c */ + +/* no more than quadruple the previous delay (multiplier + 1) */ +#define DIR_DEFAULT_RANDOM_MULTIPLIER (3) +/* no more than triple the previous delay */ +#define DIR_TEST_NET_RANDOM_MULTIPLIER (2) + +#endif + #endif diff --git a/src/test/test_dir.c b/src/test/test_dir.c index 4fddf7ab54..53911e8a26 100644 --- a/src/test/test_dir.c +++ b/src/test/test_dir.c @@ -3610,7 +3610,7 @@ download_status_random_backoff_helper(int min_delay, int max_delay) int increment = -1; int old_increment = -1; time_t current_time = time(NULL); - const int exponent = 4; + const int exponent = DIR_DEFAULT_RANDOM_MULTIPLIER + 1; /* Check the random backoff cases */ do { |