From f1136f789743f8f5cd941e3bda72fec74064cd4a Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Fri, 2 Oct 2020 03:18:50 -0400 Subject: [PATCH] [core] short-circuit connection_write_throttle() short-circuit connection_write_throttle() when throttling not enabled --- src/connections.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/connections.c b/src/connections.c index 560a1e08..a142c6ba 100644 --- a/src/connections.c +++ b/src/connections.c @@ -244,34 +244,39 @@ static void connection_handle_response_end_state(request_st * const r, connectio } } + +__attribute_pure__ static off_t -connection_write_throttle (connection * const con, off_t max_bytes) +connection_write_throttled (const connection * const con, off_t max_bytes) { - request_st * const r = &con->request; - if (r->conf.global_bytes_per_second) { - off_t limit = (off_t)r->conf.global_bytes_per_second - - *(r->conf.global_bytes_per_second_cnt_ptr); - if (limit <= 0) { - /* we reached the global traffic limit */ - con->traffic_limit_reached = 1; - return 0; - } - else if (max_bytes > limit) + const request_config * const restrict rconf = &con->request.conf; + if (0 == rconf->global_bytes_per_second && 0 == rconf->bytes_per_second) + return max_bytes; + + if (rconf->global_bytes_per_second) { + off_t limit = (off_t)rconf->global_bytes_per_second + - *(rconf->global_bytes_per_second_cnt_ptr); + if (max_bytes > limit) max_bytes = limit; } - if (r->conf.bytes_per_second) { - off_t limit = (off_t)r->conf.bytes_per_second + if (rconf->bytes_per_second) { + off_t limit = (off_t)rconf->bytes_per_second - con->bytes_written_cur_second; - if (limit <= 0) { - /* we reached the traffic limit */ - con->traffic_limit_reached = 1; - return 0; - } - else if (max_bytes > limit) + if (max_bytes > limit) max_bytes = limit; } + return max_bytes > 0 ? max_bytes : 0; /*(0 == reached traffic limit)*/ +} + + +static off_t +connection_write_throttle (connection * const con, off_t max_bytes) +{ + /*assert(max_bytes > 0);*/ + max_bytes = connection_write_throttled(con, max_bytes); + if (0 == max_bytes) con->traffic_limit_reached = 1; return max_bytes; }