Browse Source

Implement hold_open_until_flushed. I may have missed something important.

svn:r1209
Nick Mathewson 20 years ago
parent
commit
f4e4dac801
3 changed files with 34 additions and 1 deletions
  1. 21 0
      src/or/connection.c
  2. 9 1
      src/or/main.c
  3. 4 0
      src/or/or.h

+ 21 - 0
src/or/connection.c

@@ -209,6 +209,27 @@ _connection_mark_for_close(connection_t *conn, char reason)
   return retval;
 }
 
+void connection_expire_held_open(void)
+{
+  connection_t **carray, *conn;
+  int n, i;
+  time_t now;
+
+  now = time(NULL);
+
+  get_connection_array(&carray, &n);
+  for (i = 0; i < n; ++i) {
+    conn = carray[i];
+    /* If we've been holding the connection open, but we haven't written
+     * for 15 seconds...
+     */
+    if (conn->marked_for_close && conn->hold_open_until_flushed &&
+        now - conn->timestamp_lastwritten >= 15) {
+      conn->hold_open_until_flushed = 0;
+    }
+  }
+}
+
 int connection_create_listener(char *bindaddress, uint16_t bindport, int type) {
   struct sockaddr_in bindaddr; /* where to bind */
   struct hostent *rent;

+ 9 - 1
src/or/main.c

@@ -229,6 +229,10 @@ static void conn_close_if_marked(int i) {
   conn = connection_array[i];
   assert_connection_ok(conn, time(NULL));
   if(conn->marked_for_close) {
+    if (conn->hold_open_until_flushed && conn->s >= 0 &&
+        connection_wants_to_flush(conn))
+      return;
+
     log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
     if(conn->s >= 0 && connection_wants_to_flush(conn)) {
       /* -1 means it's an incomplete edge connection, or that the socket
@@ -237,7 +241,6 @@ static void conn_close_if_marked(int i) {
       log_fn(LOG_WARN,
              "Conn (fd %d, type %d, state %d) marked for close, but wants to flush.",
              conn->s, conn->type, conn->state);
-
       if(connection_speaks_cells(conn)) {
         if(conn->state == OR_CONN_STATE_OPEN) {
           flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
@@ -348,6 +351,11 @@ static void run_scheduled_events(time_t now) {
    */
   connection_ap_expire_beginning();
 
+
+  /* 2c. And expire connections that we've holding open for too long.
+   */
+  connection_expire_held_open();
+
   /* 3. Every second, we try a new circuit if there are no valid
    *    circuits. Every NewCircuitPeriod seconds, we expire circuits
    *    that became dirty more than NewCircuitPeriod seconds ago,

+ 4 - 0
src/or/or.h

@@ -318,6 +318,7 @@ struct connection_t {
                          */
   char *marked_for_close_file; /* for debugging: in which file were we marked
                                 * for close? */
+  int hold_open_until_flushed;
 
   buf_t *inbuf;
   int inbuf_reached_eof; /* did read() return 0 on this conn? */
@@ -668,6 +669,8 @@ int _connection_mark_for_close(connection_t *conn, char reason);
     }                                                                   \
   } while (0)
 
+void connection_expire_held_open(void);
+
 int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
 
 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
@@ -787,6 +790,7 @@ void connection_watch_events(connection_t *conn, short events);
 int connection_is_reading(connection_t *conn);
 void connection_stop_reading(connection_t *conn);
 void connection_start_reading(connection_t *conn);
+
 int connection_is_writing(connection_t *conn);
 void connection_stop_writing(connection_t *conn);
 void connection_start_writing(connection_t *conn);