[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Remove unused open circuits when there are too many, not wh...



Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv23381/src/or

Modified Files:
	circuit.c 
Log Message:
Remove unused open circuits when there are too many, not when they are too old.

Index: circuit.c
===================================================================
RCS file: /home/or/cvsroot/src/or/circuit.c,v
retrieving revision 1.157
retrieving revision 1.158
diff -u -d -r1.157 -r1.158
--- circuit.c	21 Mar 2004 05:01:06 -0000	1.157
+++ circuit.c	21 Mar 2004 06:33:57 -0000	1.158
@@ -18,6 +18,7 @@
 
 /********* START VARIABLES **********/
 
+static int circuitlist_len=0;
 static circuit_t *global_circuitlist=NULL;
 char *circuit_state_to_string[] = {
   "doing handshakes",        /* 0 */
@@ -36,6 +37,7 @@
     circ->next = global_circuitlist;
     global_circuitlist = circ;
   }
+  ++circuitlist_len;
 }
 
 void circuit_remove(circuit_t *circ) {
@@ -45,12 +47,14 @@
 
   if(global_circuitlist == circ) {
     global_circuitlist = global_circuitlist->next;
+    --circuitlist_len;
     return;
   }
 
   for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
     if(tmpcirc->next == circ) {
       tmpcirc->next = circ->next;
+      --circuitlist_len;
       return;
     }
   }
@@ -59,9 +63,8 @@
 void circuit_close_all_marked()
 {
   circuit_t *tmp,*m;
-  
+
   while (global_circuitlist && global_circuitlist->marked_for_close) {
-    
     tmp = global_circuitlist->next;
     circuit_free(global_circuitlist);
     global_circuitlist = tmp;
@@ -325,8 +328,8 @@
   int num=0;
 
   for(circ=global_circuitlist;circ;circ = circ->next) {
-    if(circ->cpath 
-       && circ->state != CIRCUIT_STATE_OPEN 
+    if(circ->cpath
+       && circ->state != CIRCUIT_STATE_OPEN
        && !circ->marked_for_close)
       num++;
   }
@@ -343,7 +346,7 @@
   int num=0;
 
   for(circ=global_circuitlist;circ;circ = circ->next) {
-    if(circ->cpath && circ->state != CIRCUIT_STATE_OPEN && 
+    if(circ->cpath && circ->state != CIRCUIT_STATE_OPEN &&
        !circ->marked_for_close) {
       exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
       if(exitrouter && connection_ap_can_use_exit(conn, exitrouter) != ADDR_POLICY_REJECTED)
@@ -907,34 +910,44 @@
   }
 }
 
-/* Expire unused testing circuits after 10 minutes. */
-#define TESTING_CIRCUIT_MAX_AGE 600
+/* Don't keep more than 10 unused open circuits around. */
+#define MAX_UNUSED_OPEN_CIRCUITS 10
 
 void circuit_expire_unused_circuits(void) {
-  circuit_t *circ, *tmpcirc;
+  circuit_t *circ;
   time_t now = time(NULL);
+  smartlist_t *unused_open_circs;
+  int i;
 
-  circ = global_circuitlist;
-  while(circ) {
-    tmpcirc = circ;
-    circ = circ->next;
+  unused_open_circs = smartlist_create(circuitlist_len);
+
+  for (circ = global_circuitlist; circ; circ = circ->next) {
+    if (circ->marked_for_close)
+      continue;
     /* If the circuit has been dirty for too long, and there are no streams
      * on it, mark it for close.
-     * If we are creating test circuits, and the circuit is old, and has
-     * no streams, shut it down even if it isn't dirty.
      */
-    if(((tmpcirc->timestamp_dirty &&
-         tmpcirc->timestamp_dirty + options.NewCircuitPeriod < now) ||
-        (options.RunTesting &&
-         tmpcirc->cpath &&
-         tmpcirc->timestamp_created + TESTING_CIRCUIT_MAX_AGE < now))
-       && !tmpcirc->p_conn
-       && !tmpcirc->p_streams
-       && !tmpcirc->marked_for_close) {
-      log_fn(LOG_DEBUG,"Closing n_circ_id %d",tmpcirc->n_circ_id);
-      circuit_mark_for_close(tmpcirc);
+    if (circ->timestamp_dirty &&
+        circ->timestamp_dirty + options.NewCircuitPeriod < now &&
+        !circ->p_conn &&
+        !circ->p_streams) {
+      log_fn(LOG_DEBUG,"Closing n_circ_id %d",circ->n_circ_id);
+      circuit_mark_for_close(circ);
+    } else if (!circ->timestamp_dirty && circ->cpath &&
+               circ->state == CIRCUIT_STATE_OPEN) {
+      /* Also, gather a list of open unused circuits that we created.
+       * Because we add elements to the front of global_circuitlist,
+       * the last elements of unused_open_circs will be the oldest
+       * ones.
+       */
+      smartlist_add(unused_open_circs, circ);
     }
   }
+  for (i = MAX_UNUSED_OPEN_CIRCUITS; i < unused_open_circs->num_used; ++i) {
+    circuit_t *circ=(circuit_t*)(unused_open_circs->list[i]);
+    circuit_mark_for_close(circ);
+  }
+  smartlist_free(unused_open_circs);
 }
 
 /* Number of consecutive failures so far; should only be touched by
@@ -1351,7 +1364,7 @@
     if (c->cpath) {
       assert(!c->n_crypto);
       assert(!c->p_crypto);
-      assert(!c->n_digest); 
+      assert(!c->n_digest);
       assert(!c->p_digest);
     } else {
       assert(c->n_crypto);