Browse Source

Merge branch 'bug12205_take2_squashed'

Nick Mathewson 9 years ago
parent
commit
916fba2243
4 changed files with 113 additions and 11 deletions
  1. 4 0
      changes/bug12205
  2. 30 11
      src/or/entrynodes.c
  3. 3 0
      src/or/entrynodes.h
  4. 76 0
      src/test/test_entrynodes.c

+ 4 - 0
changes/bug12205

@@ -0,0 +1,4 @@
+  o Minor refactoring:
+    - Refactoring and unit-testing entry_is_time_to_retry() in 
+      entrynodes.c. Resolves ticket 12205.
+

+ 30 - 11
src/or/entrynodes.c

@@ -156,21 +156,40 @@ entry_guard_set_status(entry_guard_t *e, const node_t *node,
 
 /** Return true iff enough time has passed since we last tried to connect
  * to the unreachable guard <b>e</b> that we're willing to try again. */
-static int
+STATIC int
 entry_is_time_to_retry(const entry_guard_t *e, time_t now)
 {
-  long diff;
+  struct guard_retry_period_s {
+    time_t period_duration;
+    time_t interval_during_period;
+  };
+
+  struct guard_retry_period_s periods[] = {
+     {    6*60*60,    60*60 }, /* For first 6 hrs., retry hourly; */
+     { 3*24*60*60,  4*60*60 }, /* Then retry every 4 hrs. until the
+                                  3-day mark; */
+     { 7*24*60*60, 18*60*60 }, /* After 3 days, retry every 18 hours until
+                                  1 week mark. */
+     {   TIME_MAX, 36*60*60 }  /* After 1 week, retry every 36 hours. */
+  };
+
+  time_t ith_deadline_for_retry;
+  time_t unreachable_for;
+  int i;
+
   if (e->last_attempted < e->unreachable_since)
     return 1;
-  diff = now - e->unreachable_since;
-  if (diff < 6*60*60)
-    return now > (e->last_attempted + 60*60);
-  else if (diff < 3*24*60*60)
-    return now > (e->last_attempted + 4*60*60);
-  else if (diff < 7*24*60*60)
-    return now > (e->last_attempted + 18*60*60);
-  else
-    return now > (e->last_attempted + 36*60*60);
+
+  unreachable_for = now - e->unreachable_since;
+
+  for (i = 0; ; i++) {
+    if (unreachable_for <= periods[i].period_duration) {
+      ith_deadline_for_retry = e->last_attempted +
+                               periods[i].interval_during_period;
+
+      return (now > ith_deadline_for_retry);
+    }
+  }
 }
 
 /** Return the node corresponding to <b>e</b>, if <b>e</b> is

+ 3 - 0
src/or/entrynodes.h

@@ -104,6 +104,9 @@ typedef enum {
 STATIC const node_t *entry_is_live(const entry_guard_t *e,
                                    entry_is_live_flags_t flags,
                                    const char **msg);
+
+STATIC int entry_is_time_to_retry(const entry_guard_t *e, time_t now);
+
 #endif
 
 void remove_all_entry_guards(void);

+ 76 - 0
src/test/test_entrynodes.c

@@ -551,6 +551,80 @@ test_entry_guards_set_from_config(void *arg)
   routerset_free(options->EntryNodes);
 }
 
+static void
+test_entry_is_time_to_retry(void *arg)
+{
+  entry_guard_t *test_guard;
+  time_t now;
+  int retval;
+  (void)arg;
+
+  now = time(NULL);
+
+  test_guard = tor_malloc_zero(sizeof(entry_guard_t));
+
+  test_guard->last_attempted = now - 10;
+  test_guard->unreachable_since = now - 1;
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,1);
+
+  test_guard->unreachable_since = now - (6*60*60 - 1);
+  test_guard->last_attempted = now - (60*60 + 1);
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,1);
+
+  test_guard->last_attempted = now - (60*60 - 1);
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,0);
+
+  test_guard->unreachable_since = now - (6*60*60 + 1);
+  test_guard->last_attempted = now - (4*60*60 + 1);
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,1);
+
+  test_guard->unreachable_since = now - (3*24*60*60 - 1);
+  test_guard->last_attempted = now - (4*60*60 + 1);
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,1);
+
+  test_guard->unreachable_since = now - (3*24*60*60 + 1);
+  test_guard->last_attempted = now - (18*60*60 + 1);
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,1);
+
+  test_guard->unreachable_since = now - (7*24*60*60 - 1);
+  test_guard->last_attempted = now - (18*60*60 + 1);
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,1);
+
+  test_guard->last_attempted = now - (18*60*60 - 1);
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,0);
+
+  test_guard->unreachable_since = now - (7*24*60*60 + 1);
+  test_guard->last_attempted = now - (36*60*60 + 1);
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,1);
+
+  test_guard->unreachable_since = now - (7*24*60*60 + 1);
+  test_guard->last_attempted = now - (36*60*60 + 1);
+
+  retval = entry_is_time_to_retry(test_guard,now);
+  tt_int_op(retval,==,1);
+
+ done:
+  tor_free(test_guard);
+}
+
 /** XXX Do some tests that entry_is_live() */
 static void
 test_entry_is_live(void *arg)
@@ -621,6 +695,8 @@ static const struct testcase_setup_t fake_network = {
 };
 
 struct testcase_t entrynodes_tests[] = {
+  { "entry_is_time_to_retry", test_entry_is_time_to_retry,
+    TT_FORK, NULL, NULL },
   { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
     TT_FORK, &fake_network, NULL },
   { "choose_random_entry_one_possibleguard",