Commit 399518da authored by David Goulet's avatar David Goulet 🐼 Committed by Alexander Hansen Færøy
Browse files

relay: Reduce streaming compression ratio from HIGH to LOW



Fixes #40301

Signed-off-by: David Goulet's avatarDavid Goulet <dgoulet@torproject.org>
parent 87492154
Loading
Loading
Loading
Loading

changes/ticket40301

0 → 100644
+4 −0
Original line number Original line Diff line number Diff line
  o Minor bugfixes (relay):
    - Reduce the compression level for data streaming from HIGH to LOW. Fixes
      bug 40301; bugfix on 0.3.5.1-alpha.
+19 −16
Original line number Original line Diff line number Diff line
@@ -294,19 +294,22 @@ client_likes_consensus(const struct consensus_cache_entry_t *ent,
/** Return the compression level we should use for sending a compressed
/** Return the compression level we should use for sending a compressed
 * response of size <b>n_bytes</b>. */
 * response of size <b>n_bytes</b>. */
STATIC compression_level_t
STATIC compression_level_t
choose_compression_level(ssize_t n_bytes)
choose_compression_level(void)
{
{
  if (! have_been_under_memory_pressure()) {
  /* This is the compression level choice for a stream.
    return HIGH_COMPRESSION; /* we have plenty of RAM. */
   *
  } else if (n_bytes < 0) {
   * We always return LOW because this compression is done in the main thread
    return HIGH_COMPRESSION; /* unknown; might be big. */
   * thus we save CPU time as much as possible, and it is also done more than
  } else if (n_bytes < 1024) {
   * background compression for document we serve pre-compressed.
   *
   * GZip highest compression level (9) gives us a ratio of 49.72%
   * Zstd lowest compression level (1) gives us a ratio of 47.38%
   *
   * Thus, as the network moves more and more to use Zstd when requesting
   * directory documents that are not pre-cached, even at the
   * lowest level, we still gain over GZip and thus help with load and CPU
   * time on the network. */
  return LOW_COMPRESSION;
  return LOW_COMPRESSION;
  } else if (n_bytes < 2048) {
    return MEDIUM_COMPRESSION;
  } else {
    return HIGH_COMPRESSION;
  }
}
}


/** Information passed to handle a GET request. */
/** Information passed to handle a GET request. */
@@ -1044,7 +1047,7 @@ handle_get_status_vote(dir_connection_t *conn, const get_handler_args_t *args)
    if (smartlist_len(items)) {
    if (smartlist_len(items)) {
      if (compress_method != NO_METHOD) {
      if (compress_method != NO_METHOD) {
        conn->compress_state = tor_compress_new(1, compress_method,
        conn->compress_state = tor_compress_new(1, compress_method,
                           choose_compression_level(estimated_len));
                           choose_compression_level());
        SMARTLIST_FOREACH(items, const char *, c,
        SMARTLIST_FOREACH(items, const char *, c,
                 connection_buf_add_compress(c, strlen(c), conn, 0));
                 connection_buf_add_compress(c, strlen(c), conn, 0));
        connection_buf_add_compress("", 0, conn, 1);
        connection_buf_add_compress("", 0, conn, 1);
@@ -1109,7 +1112,7 @@ handle_get_microdesc(dir_connection_t *conn, const get_handler_args_t *args)


    if (compress_method != NO_METHOD)
    if (compress_method != NO_METHOD)
      conn->compress_state = tor_compress_new(1, compress_method,
      conn->compress_state = tor_compress_new(1, compress_method,
                                      choose_compression_level(size_guess));
                                      choose_compression_level());


    const int initial_flush_result = connection_dirserv_flushed_some(conn);
    const int initial_flush_result = connection_dirserv_flushed_some(conn);
    tor_assert_nonfatal(initial_flush_result == 0);
    tor_assert_nonfatal(initial_flush_result == 0);
@@ -1204,7 +1207,7 @@ handle_get_descriptor(dir_connection_t *conn, const get_handler_args_t *args)
      write_http_response_header(conn, -1, compress_method, cache_lifetime);
      write_http_response_header(conn, -1, compress_method, cache_lifetime);
      if (compress_method != NO_METHOD)
      if (compress_method != NO_METHOD)
        conn->compress_state = tor_compress_new(1, compress_method,
        conn->compress_state = tor_compress_new(1, compress_method,
                                        choose_compression_level(size_guess));
                                        choose_compression_level());
      clear_spool = 0;
      clear_spool = 0;
      /* Prime the connection with some data. */
      /* Prime the connection with some data. */
      int initial_flush_result = connection_dirserv_flushed_some(conn);
      int initial_flush_result = connection_dirserv_flushed_some(conn);
@@ -1301,7 +1304,7 @@ handle_get_keys(dir_connection_t *conn, const get_handler_args_t *args)
                               60*60);
                               60*60);
    if (compress_method != NO_METHOD) {
    if (compress_method != NO_METHOD) {
      conn->compress_state = tor_compress_new(1, compress_method,
      conn->compress_state = tor_compress_new(1, compress_method,
                                              choose_compression_level(len));
                                              choose_compression_level());
      SMARTLIST_FOREACH(certs, authority_cert_t *, c,
      SMARTLIST_FOREACH(certs, authority_cert_t *, c,
            connection_buf_add_compress(
            connection_buf_add_compress(
                c->cache_info.signed_descriptor_body,
                c->cache_info.signed_descriptor_body,
+1 −1
Original line number Original line Diff line number Diff line
@@ -26,7 +26,7 @@ MOCK_DECL(STATIC int, directory_handle_command_post,(dir_connection_t *conn,


STATIC int handle_post_hs_descriptor(const char *url, const char *body);
STATIC int handle_post_hs_descriptor(const char *url, const char *body);
enum compression_level_t;
enum compression_level_t;
STATIC enum compression_level_t choose_compression_level(ssize_t n_bytes);
STATIC enum compression_level_t choose_compression_level(void);


struct get_handler_args_t;
struct get_handler_args_t;
STATIC int handle_get_hs_descriptor_v3(dir_connection_t *conn,
STATIC int handle_get_hs_descriptor_v3(dir_connection_t *conn,
+2 −2
Original line number Original line Diff line number Diff line
@@ -49,8 +49,8 @@ memory_level(compression_level_t level)
    default:
    default:
    case BEST_COMPRESSION:
    case BEST_COMPRESSION:
    case HIGH_COMPRESSION: return 9;
    case HIGH_COMPRESSION: return 9;
    case MEDIUM_COMPRESSION: return 8;
    case MEDIUM_COMPRESSION: return 3;
    case LOW_COMPRESSION: return 7;
    case LOW_COMPRESSION: return 1;
  }
  }
}
}
#endif /* defined(HAVE_ZSTD) */
#endif /* defined(HAVE_ZSTD) */
+0 −26
Original line number Original line Diff line number Diff line
@@ -4859,31 +4859,6 @@ NS(directory_initiate_request)(directory_request_t *req)
  CALLED(directory_initiate_request)++;
  CALLED(directory_initiate_request)++;
}
}


static void
test_dir_choose_compression_level(void* data)
{
  (void)data;

  /* It starts under_memory_pressure */
  tt_int_op(have_been_under_memory_pressure(), OP_EQ, 1);

  tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
  tt_assert(LOW_COMPRESSION == choose_compression_level(1024-1));
  tt_assert(MEDIUM_COMPRESSION == choose_compression_level(2048-1));
  tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));

  /* Reset under_memory_pressure timer */
  cell_queues_check_size();
  tt_int_op(have_been_under_memory_pressure(), OP_EQ, 0);

  tt_assert(HIGH_COMPRESSION == choose_compression_level(-1));
  tt_assert(HIGH_COMPRESSION == choose_compression_level(1024-1));
  tt_assert(HIGH_COMPRESSION == choose_compression_level(2048-1));
  tt_assert(HIGH_COMPRESSION == choose_compression_level(2048));

  done: ;
}

/*
/*
 * Mock check_private_dir(), and always succeed - no need to actually
 * Mock check_private_dir(), and always succeed - no need to actually
 * look at or create anything on the filesystem.
 * look at or create anything on the filesystem.
@@ -6397,7 +6372,6 @@ struct testcase_t dir_tests[] = {
  DIR(should_not_init_request_to_ourselves, TT_FORK),
  DIR(should_not_init_request_to_ourselves, TT_FORK),
  DIR(should_not_init_request_to_dir_auths_without_v3_info, 0),
  DIR(should_not_init_request_to_dir_auths_without_v3_info, 0),
  DIR(should_init_request_to_dir_auths, 0),
  DIR(should_init_request_to_dir_auths, 0),
  DIR(choose_compression_level, 0),
  DIR(dump_unparseable_descriptors, 0),
  DIR(dump_unparseable_descriptors, 0),
  DIR(populate_dump_desc_fifo, 0),
  DIR(populate_dump_desc_fifo, 0),
  DIR(populate_dump_desc_fifo_2, 0),
  DIR(populate_dump_desc_fifo_2, 0),