Browse Source

Refactor exponential backoff multipliers into macros

There are only so many times you can type "4".
teor 6 years ago
parent
commit
32f0cbc0f6
3 changed files with 16 additions and 4 deletions
  1. 5 3
      src/or/directory.c
  2. 10 0
      src/or/directory.h
  3. 1 1
      src/test/test_dir.c

+ 5 - 3
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) {

+ 10 - 0
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
 

+ 1 - 1
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 {