From: Stephan Bosch Date: Tue, 20 Mar 2018 19:59:21 +0000 (+0100) Subject: lib-http: Reformat http-client-queue.c. X-Git-Tag: 2.3.13~282 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b0f648c58caffda5dfbbddc1018e99822880a96;p=thirdparty%2Fdovecot%2Fcore.git lib-http: Reformat http-client-queue.c. --- diff --git a/src/lib-http/http-client-queue.c b/src/lib-http/http-client-queue.c index c7b5a9abf2..ab82270d04 100644 --- a/src/lib-http/http-client-queue.c +++ b/src/lib-http/http-client-queue.c @@ -24,10 +24,10 @@ http_client_queue_fail_full(struct http_client_queue *queue, unsigned int status, const char *error, bool all); static void http_client_queue_set_delay_timer(struct http_client_queue *queue, - struct timeval time); + struct timeval time); static void http_client_queue_set_request_timer(struct http_client_queue *queue, - const struct timeval *time); + const struct timeval *time); /* * Queue object @@ -35,7 +35,7 @@ http_client_queue_set_request_timer(struct http_client_queue *queue, static struct http_client_queue * http_client_queue_find(struct http_client_host *host, - const struct http_client_peer_addr *addr) + const struct http_client_peer_addr *addr) { struct http_client_queue *const *queue_idx; @@ -51,7 +51,7 @@ http_client_queue_find(struct http_client_host *host, static struct http_client_queue * http_client_queue_create(struct http_client_host *host, - const struct http_client_peer_addr *addr) + const struct http_client_peer_addr *addr) { const char *hostname = host->shared->name; struct http_client_queue *queue; @@ -63,20 +63,20 @@ http_client_queue_create(struct http_client_host *host, switch (addr->type) { case HTTP_CLIENT_PEER_ADDR_RAW: - queue->name = - i_strdup_printf("raw://%s:%u", hostname, addr->a.tcp.port); + queue->name = i_strdup_printf("raw://%s:%u", + hostname, addr->a.tcp.port); queue->addr.a.tcp.https_name = NULL; break; case HTTP_CLIENT_PEER_ADDR_HTTPS_TUNNEL: case HTTP_CLIENT_PEER_ADDR_HTTPS: - queue->name = - i_strdup_printf("https://%s:%u", hostname, addr->a.tcp.port); + queue->name = i_strdup_printf("https://%s:%u", + hostname, addr->a.tcp.port); queue->addr_name = i_strdup(addr->a.tcp.https_name); queue->addr.a.tcp.https_name = queue->addr_name; break; case HTTP_CLIENT_PEER_ADDR_HTTP: - queue->name = - i_strdup_printf("http://%s:%u", hostname, addr->a.tcp.port); + queue->name = i_strdup_printf("http://%s:%u", + hostname, addr->a.tcp.port); queue->addr.a.tcp.https_name = NULL; break; case HTTP_CLIENT_PEER_ADDR_UNIX: @@ -104,7 +104,7 @@ http_client_queue_create(struct http_client_host *host, struct http_client_queue * http_client_queue_get(struct http_client_host *host, - const struct http_client_peer_addr *addr) + const struct http_client_peer_addr *addr) { struct http_client_queue *queue; @@ -122,23 +122,24 @@ void http_client_queue_free(struct http_client_queue *queue) e_debug(queue->event, "Destroy"); - /* currently only called when peer is freed, so there is no need to + /* Currently only called when peer is freed, so there is no need to unlink from the peer */ - /* unlink all peers */ + /* Unlink all peers */ if (queue->cur_peer != NULL) { struct http_client_peer *peer = queue->cur_peer; + queue->cur_peer = NULL; http_client_peer_unlink_queue(peer, queue); } t_array_init(&peers, array_count(&queue->pending_peers)); array_copy(&peers.arr, 0, &queue->pending_peers.arr, 0, - array_count(&queue->pending_peers)); + array_count(&queue->pending_peers)); array_foreach(&peers, peer_idx) http_client_peer_unlink_queue(*peer_idx, queue); array_free(&queue->pending_peers); - /* abort all requests */ + /* Abort all requests */ http_client_queue_fail_full(queue, HTTP_CLIENT_REQUEST_ERROR_ABORTED, "Aborted", TRUE); array_free(&queue->requests); @@ -146,11 +147,11 @@ void http_client_queue_free(struct http_client_queue *queue) array_free(&queue->queued_urgent_requests); array_free(&queue->delayed_requests); - /* cancel timeouts */ + /* Cancel timeouts */ timeout_remove(&queue->to_connect); timeout_remove(&queue->to_delayed); - /* free */ + /* Free */ event_unref(&queue->event); i_free(queue->addr_name); i_free(queue->name); @@ -169,7 +170,7 @@ http_client_queue_fail_full(struct http_client_queue *queue, struct http_client_request **req_idx; unsigned int retained = 0; - /* abort requests */ + /* Abort requests */ req_arr = &queue->requests; t_array_init(&treqs, array_count(req_arr)); array_copy(&treqs.arr, 0, &req_arr->arr, 0, array_count(req_arr)); @@ -184,18 +185,18 @@ http_client_queue_fail_full(struct http_client_queue *queue, http_client_request_error(&req, status, error); } - /* all queues should be empty now... unless new requests were submitted - from the callback. this invariant captures it all: */ + /* All queues should be empty now... unless new requests were submitted + from the callback. this invariant captures it all: */ i_assert((retained + - array_count(&queue->delayed_requests) + - array_count(&queue->queued_requests) + - array_count(&queue->queued_urgent_requests)) == - array_count(&queue->requests)); + array_count(&queue->delayed_requests) + + array_count(&queue->queued_requests) + + array_count(&queue->queued_urgent_requests)) == + array_count(&queue->requests)); } static void http_client_queue_fail(struct http_client_queue *queue, - unsigned int status, const char *error) + unsigned int status, const char *error) { http_client_queue_fail_full(queue, status, error, FALSE); } @@ -216,17 +217,17 @@ http_client_queue_is_last_connect_ip(struct http_client_queue *queue) i_assert(queue->ips_connect_idx < ips_count); i_assert(queue->ips_connect_start_idx < ips_count); - /* if a maximum connect attempts > 1 is set, enforce it directly */ + /* If a maximum connect attempts > 1 is set, enforce it directly */ if (set->max_connect_attempts > 1 && queue->connect_attempts >= set->max_connect_attempts) return TRUE; - - /* otherwise, we'll always go through all the IPs. we don't necessarily + + /* Otherwise, we'll always go through all the IPs. we don't necessarily start connecting from the first IP, so we'll need to treat the IPs as a ring buffer where we automatically wrap back to the first IP when necessary. */ - return (queue->ips_connect_idx + 1) % ips_count == - queue->ips_connect_start_idx; + return ((queue->ips_connect_idx + 1) % ips_count == + queue->ips_connect_start_idx); } static void @@ -242,12 +243,12 @@ http_client_queue_recover_from_lookup(struct http_client_queue *queue) return; } - if (http_client_host_get_ip_idx(host, - &queue->cur_peer->shared->addr.a.tcp.ip, &ip_idx)) { - /* continue with current peer */ + if (http_client_host_get_ip_idx( + host, &queue->cur_peer->shared->addr.a.tcp.ip, &ip_idx)) { + /* Continue with current peer */ queue->ips_connect_idx = queue->ips_connect_start_idx = ip_idx; } else { - /* reset connect attempts */ + /* Reset connect attempts */ queue->ips_connect_idx = queue->ips_connect_start_idx = 0; } } @@ -265,23 +266,24 @@ http_client_queue_soft_connect_timeout(struct http_client_queue *queue) timeout_remove(&queue->to_connect); if (http_client_queue_is_last_connect_ip(queue)) { - /* no more IPs to try */ + /* No more IPs to try */ return; } - /* if our our previous connection attempt takes longer than the + /* If our our previous connection attempt takes longer than the soft_connect_timeout, we start a connection attempt to the next IP in parallel */ https_name = http_client_peer_addr_get_https_name(addr); e_debug(queue->event, "Connection to %s%s is taking a long time; " "starting parallel connection attempt to next IP", - http_client_peer_addr2str(addr), (https_name == NULL ? "" : - t_strdup_printf(" (SSL=%s)", https_name))); + http_client_peer_addr2str(addr), + (https_name == NULL ? + "" : t_strdup_printf(" (SSL=%s)", https_name))); - /* next IP */ + /* Next IP */ queue->ips_connect_idx = (queue->ips_connect_idx + 1) % ips_count; - /* setup connection to new peer (can start new soft timeout) */ + /* Setup connection to new peer (can start new soft timeout) */ http_client_queue_connection_setup(queue); } @@ -301,50 +303,54 @@ http_client_queue_connection_attempt(struct http_client_queue *queue) if (num_requests == 0) return NULL; - /* check whether host IPs are still up-to-date */ - if ((ret=http_client_host_refresh(host)) < 0) { - /* performing asynchronous lookup */ + /* Check whether host IPs are still up-to-date */ + ret = http_client_host_refresh(host); + if (ret < 0) { + /* Performing asynchronous lookup */ timeout_remove(&queue->to_connect); return NULL; } if (ret > 0) { - /* new lookup performed */ + /* New lookup performed */ http_client_queue_recover_from_lookup(queue); } - /* update our peer address */ + /* Update our peer address */ if (queue->addr.type != HTTP_CLIENT_PEER_ADDR_UNIX) { - const struct ip_addr *ip = http_client_host_get_ip(host, - queue->ips_connect_idx); + const struct ip_addr *ip = + http_client_host_get_ip(host, queue->ips_connect_idx); + queue->addr.a.tcp.ip = *ip; ssl = http_client_peer_addr_get_https_name(addr); ssl = (ssl == NULL ? "" : t_strdup_printf(" (SSL=%s)", ssl)); } - /* already got a peer? */ + /* Already got a peer? */ peer = NULL; if (queue->cur_peer != NULL) { i_assert(array_count(&queue->pending_peers) == 0); - /* is it still the one we want? */ - if (http_client_peer_addr_cmp - (addr, &queue->cur_peer->shared->addr) == 0) { - /* is it still connected? */ + /* Is it still the one we want? */ + if (http_client_peer_addr_cmp( + addr, &queue->cur_peer->shared->addr) == 0) { + /* Is it still connected? */ if (http_client_peer_is_connected(queue->cur_peer)) { - /* yes */ + /* Yes */ e_debug(queue->event, "Using existing connection to %s%s " "(%u requests pending)", - http_client_peer_addr2str(addr), ssl, num_requests); + http_client_peer_addr2str(addr), + ssl, num_requests); - /* handle requests; */ - http_client_peer_trigger_request_handler(queue->cur_peer); + /* Handle requests; */ + http_client_peer_trigger_request_handler( + queue->cur_peer); return queue->cur_peer; } - /* no */ + /* No */ peer = queue->cur_peer; } else { - /* peer is not relevant to this queue anymore */ + /* Peer is not relevant to this queue anymore */ http_client_peer_unlink_queue(queue->cur_peer, queue); } @@ -354,18 +360,18 @@ http_client_queue_connection_attempt(struct http_client_queue *queue) if (peer == NULL) peer = http_client_peer_get(queue->client, addr); - e_debug(queue->event, "Setting up connection to %s%s " - "(%u requests pending)", http_client_peer_addr2str(addr), ssl, - num_requests); + e_debug(queue->event, + "Setting up connection to %s%s (%u requests pending)", + http_client_peer_addr2str(addr), ssl, num_requests); - /* create provisional link between queue and peer */ + /* Create provisional link between queue and peer */ http_client_peer_link_queue(peer, queue); - /* handle requests; creates new connections when needed/possible */ + /* Handle requests; creates new connections when needed/possible */ http_client_peer_trigger_request_handler(peer); if (http_client_peer_is_connected(peer)) { - /* drop any pending peers */ + /* Drop any pending peers */ if (array_count(&queue->pending_peers) > 0) { struct http_client_peer *const *peer_idx; @@ -375,8 +381,8 @@ http_client_queue_connection_attempt(struct http_client_queue *queue) */ continue; } - i_assert(http_client_peer_addr_cmp - (&(*peer_idx)->shared->addr, addr) != 0); + i_assert(http_client_peer_addr_cmp( + &(*peer_idx)->shared->addr, addr) != 0); http_client_peer_unlink_queue(*peer_idx, queue); } array_clear(&queue->pending_peers); @@ -390,12 +396,12 @@ http_client_queue_connection_attempt(struct http_client_queue *queue) unsigned int msecs; bool new_peer = TRUE; - /* not already connected, wait for connections */ + /* Not already connected, wait for connections */ - /* we may be waiting for this peer already */ + /* We may be waiting for this peer already */ array_foreach(&queue->pending_peers, peer_idx) { - if (http_client_peer_addr_cmp - (&(*peer_idx)->shared->addr, addr) == 0) { + if (http_client_peer_addr_cmp( + &(*peer_idx)->shared->addr, addr) == 0) { i_assert(*peer_idx == peer); new_peer = FALSE; break; @@ -410,13 +416,16 @@ http_client_queue_connection_attempt(struct http_client_queue *queue) queue->first_connect_time = ioloop_timeval; } - /* start soft connect time-out (but only if we have another IP left) */ + /* Start soft connect time-out + (but only if we have another IP left) */ if (queue->addr.type != HTTP_CLIENT_PEER_ADDR_UNIX) { msecs = client->set.soft_connect_timeout_msecs; - if (!http_client_queue_is_last_connect_ip(queue) && msecs > 0 && - queue->to_connect == NULL) { - queue->to_connect = timeout_add_to(client->ioloop, msecs, - http_client_queue_soft_connect_timeout, queue); + if (!http_client_queue_is_last_connect_ip(queue) && + msecs > 0 && queue->to_connect == NULL) { + queue->to_connect = timeout_add_to( + client->ioloop, msecs, + http_client_queue_soft_connect_timeout, + queue); } } } @@ -434,6 +443,7 @@ http_client_queue_host_lookup_done(struct http_client_queue *queue) { unsigned int reqs_pending = http_client_queue_requests_pending(queue, NULL); + http_client_queue_recover_from_lookup(queue); if (reqs_pending > 0) http_client_queue_connection_setup(queue); @@ -443,34 +453,33 @@ http_client_queue_host_lookup_done(struct http_client_queue *queue) void http_client_queue_host_lookup_failure( struct http_client_queue *queue, const char *error) { - http_client_queue_fail(queue, - HTTP_CLIENT_REQUEST_ERROR_HOST_LOOKUP_FAILED, error); + http_client_queue_fail( + queue, HTTP_CLIENT_REQUEST_ERROR_HOST_LOOKUP_FAILED, error); } -void -http_client_queue_connection_success(struct http_client_queue *queue, - struct http_client_peer *peer) +void http_client_queue_connection_success(struct http_client_queue *queue, + struct http_client_peer *peer) { const struct http_client_peer_addr *addr = &peer->shared->addr; struct http_client_host *host = queue->host; if (http_client_host_ready(host) && - queue->addr.type != HTTP_CLIENT_PEER_ADDR_UNIX) { - /* we achieved at least one connection the the addr->ip */ - if (!http_client_host_get_ip_idx(host, - &addr->a.tcp.ip, &queue->ips_connect_start_idx)) { + queue->addr.type != HTTP_CLIENT_PEER_ADDR_UNIX) { + /* We achieved at least one connection the the addr->ip */ + if (!http_client_host_get_ip_idx( + host, &addr->a.tcp.ip, &queue->ips_connect_start_idx)) { /* list of IPs changed during connect */ queue->ips_connect_start_idx = 0; } } - /* reset attempt counter */ + /* Reset attempt counter */ queue->connect_attempts = 0; /* stop soft connect time-out */ timeout_remove(&queue->to_connect); - /* drop all other attempts to the hport. note that we get here whenever + /* Drop all other attempts to the hport. note that we get here whenever a connection is successfully created, so pending_peers array may be empty. */ if (array_count(&queue->pending_peers) > 0) { @@ -478,16 +487,17 @@ http_client_queue_connection_success(struct http_client_queue *queue, array_foreach(&queue->pending_peers, peer_idx) { if (*peer_idx == peer) { - /* don't drop any connections to the successfully - connected peer, even if some of the connections - are pending. they may be intended for urgent - requests. */ + /* Don't drop any connections to the + successfully connected peer, even if some of + the connections are pending. they may be + intended for urgent requests. */ i_assert(queue->cur_peer == NULL); queue->cur_peer = *peer_idx; continue; } - /* unlink this queue from the peer; if this was the last/only queue, the - peer will be freed, closing all connections. + /* Unlink this queue from the peer; if this was the + last/only queue, the peer will be freed, closing all + connections. */ http_client_peer_unlink_queue(*peer_idx, queue); } @@ -497,9 +507,9 @@ http_client_queue_connection_success(struct http_client_queue *queue, } } -void -http_client_queue_connection_failure(struct http_client_queue *queue, - struct http_client_peer *peer, const char *reason) +void http_client_queue_connection_failure(struct http_client_queue *queue, + struct http_client_peer *peer, + const char *reason) { const struct http_client_settings *set = &queue->client->set; @@ -516,8 +526,8 @@ http_client_queue_connection_failure(struct http_client_queue *queue, "Failed to set up connection to %s%s: %s " "(%u peers pending, %u requests pending)", http_client_peer_addr2str(addr), - (https_name == NULL ? "" : - t_strdup_printf(" (SSL=%s)", https_name)), + (https_name == NULL ? + "" : t_strdup_printf(" (SSL=%s)", https_name)), reason, array_count(&queue->pending_peers), num_requests); http_client_peer_unlink_queue(peer, queue); @@ -530,14 +540,15 @@ http_client_queue_connection_failure(struct http_client_queue *queue, i_assert(queue->cur_peer == NULL); - /* we're still doing the initial connections to this hport. if - we're also doing parallel connections with soft timeouts - (pending_peer_count>1), wait for them to finish - first. */ + /* We're still doing the initial connections to this hport. if + we're also doing parallel connections with soft timeouts + (pending_peer_count>1), wait for them to finish first. */ array_foreach(&queue->pending_peers, peer_idx) { if (*peer_idx == peer) { array_delete(&queue->pending_peers, - array_foreach_idx(&queue->pending_peers, peer_idx), 1); + array_foreach_idx( + &queue->pending_peers, + peer_idx), 1); found = TRUE; break; } @@ -549,14 +560,16 @@ http_client_queue_connection_failure(struct http_client_queue *queue, return; } - /* one of the connections failed. if we're not using soft timeouts, - we need to try to connect to the next IP. if we are using soft - timeouts, we've already tried all of the IPs by now. */ + /* One of the connections failed. if we're not using soft + timeouts, we need to try to connect to the next IP. if we are + using soft timeouts, we've already tried all of the IPs by + now. */ timeout_remove(&queue->to_connect); if (queue->addr.type == HTTP_CLIENT_PEER_ADDR_UNIX) { - http_client_queue_fail(queue, - HTTP_CLIENT_REQUEST_ERROR_CONNECT_FAILED, reason); + http_client_queue_fail( + queue, HTTP_CLIENT_REQUEST_ERROR_CONNECT_FAILED, + reason); return; } } @@ -576,31 +589,35 @@ http_client_queue_connection_failure(struct http_client_queue *queue, queue->connect_attempts >= set->max_connect_attempts) { e_debug(queue->event, - "Failed to set up any connection; failing all queued requests"); + "Failed to set up any connection; " + "failing all queued requests"); if (queue->connect_attempts > 1) { unsigned int total_msecs = - timeval_diff_msecs(&ioloop_timeval, &queue->first_connect_time); - reason = t_strdup_printf("%s (%u attempts in %u.%03u secs)", + timeval_diff_msecs(&ioloop_timeval, + &queue->first_connect_time); + reason = t_strdup_printf( + "%s (%u attempts in %u.%03u secs)", reason, queue->connect_attempts, total_msecs/1000, total_msecs%1000); } queue->connect_attempts = 0; - http_client_queue_fail(queue, - HTTP_CLIENT_REQUEST_ERROR_CONNECT_FAILED, reason); + http_client_queue_fail( + queue, HTTP_CLIENT_REQUEST_ERROR_CONNECT_FAILED, + reason); return; } } else { - queue->ips_connect_idx = (queue->ips_connect_idx + 1) % ips_count; + queue->ips_connect_idx = + (queue->ips_connect_idx + 1) % ips_count; } - + if (http_client_queue_connection_attempt(queue) != peer) http_client_peer_unlink_queue(peer, queue); return; } -void -http_client_queue_peer_disconnected(struct http_client_queue *queue, - struct http_client_peer *peer) +void http_client_queue_peer_disconnected(struct http_client_queue *queue, + struct http_client_peer *peer) { struct http_client_peer *const *peer_idx; @@ -612,7 +629,8 @@ http_client_queue_peer_disconnected(struct http_client_queue *queue, array_foreach(&queue->pending_peers, peer_idx) { if (*peer_idx == peer) { array_delete(&queue->pending_peers, - array_foreach_idx(&queue->pending_peers, peer_idx), 1); + array_foreach_idx(&queue->pending_peers, + peer_idx), 1); break; } } @@ -622,9 +640,8 @@ http_client_queue_peer_disconnected(struct http_client_queue *queue, * Main request queue */ -void -http_client_queue_drop_request(struct http_client_queue *queue, - struct http_client_request *req) +void http_client_queue_drop_request(struct http_client_queue *queue, + struct http_client_request *req) { struct http_client_request **reqs; unsigned int count, i; @@ -632,12 +649,14 @@ http_client_queue_drop_request(struct http_client_queue *queue, e_debug(queue->event, "Dropping request %s", http_client_request_label(req)); - /* drop from queue */ + /* Drop from queue */ if (req->urgent) { - reqs = array_get_modifiable(&queue->queued_urgent_requests, &count); + reqs = array_get_modifiable(&queue->queued_urgent_requests, + &count); for (i = 0; i < count; i++) { if (reqs[i] == req) { - array_delete(&queue->queued_urgent_requests, i, 1); + array_delete(&queue->queued_urgent_requests, + i, 1); break; } } @@ -651,7 +670,7 @@ http_client_queue_drop_request(struct http_client_queue *queue, } } - /* drop from delay queue */ + /* Drop from delay queue */ if (req->release_time.tv_sec > 0) { reqs = array_get_modifiable(&queue->delayed_requests, &count); for (i = 0; i < count; i++) { @@ -664,7 +683,8 @@ http_client_queue_drop_request(struct http_client_queue *queue, timeout_remove(&queue->to_delayed); if (count > 1) { i_assert(reqs[1]->release_time.tv_sec > 0); - http_client_queue_set_delay_timer(queue, reqs[1]->release_time); + http_client_queue_set_delay_timer( + queue, reqs[1]->release_time); } } } @@ -672,7 +692,7 @@ http_client_queue_drop_request(struct http_client_queue *queue, } } - /* drop from main request list */ + /* Drop from main request list */ reqs = array_get_modifiable(&queue->requests, &count); for (i = 0; i < count; i++) { if (reqs[i] == req) @@ -683,8 +703,10 @@ http_client_queue_drop_request(struct http_client_queue *queue, if (i == 0) { if (queue->to_request != NULL) { timeout_remove(&queue->to_request); - if (count > 1 && reqs[1]->timeout_time.tv_sec > 0) - http_client_queue_set_request_timer(queue, &reqs[1]->timeout_time); + if (count > 1 && reqs[1]->timeout_time.tv_sec > 0) { + http_client_queue_set_request_timer(queue, + &reqs[1]->timeout_time); + } } } req->queue = NULL; @@ -695,8 +717,7 @@ http_client_queue_drop_request(struct http_client_queue *queue, return; } -static void -http_client_queue_request_timeout(struct http_client_queue *queue) +static void http_client_queue_request_timeout(struct http_client_queue *queue) { struct http_client_request *const *reqs; ARRAY_TYPE(http_client_request) failed_requests; @@ -707,24 +728,25 @@ http_client_queue_request_timeout(struct http_client_queue *queue) e_debug(queue->event, "Timeout (now: %s.%03lu)", t_strflocaltime("%Y-%m-%d %H:%M:%S", ioloop_timeval.tv_sec), - ((unsigned long)ioloop_timeval.tv_usec)/1000); + ((unsigned long)ioloop_timeval.tv_usec) / 1000); timeout_remove(&queue->to_request); - /* collect failed requests */ + /* Collect failed requests */ reqs = array_get(&queue->requests, &count); i_assert(count > 0); t_array_init(&failed_requests, count); for (i = 0; i < count; i++) { if (reqs[i]->timeout_time.tv_sec > 0 && - timeval_cmp_margin(&reqs[i]->timeout_time, - &ioloop_timeval, TIMEOUT_CMP_MARGIN_USECS) > 0) { + timeval_cmp_margin(&reqs[i]->timeout_time, + &ioloop_timeval, + TIMEOUT_CMP_MARGIN_USECS) > 0) { break; } array_push_back(&failed_requests, &reqs[i]); } - /* update timeout */ + /* Update timeout */ if (i < count) new_to = reqs[i]->timeout_time; @@ -732,9 +754,9 @@ http_client_queue_request_timeout(struct http_client_queue *queue) str_append(str, "Request "); prefix_size = str_len(str); - /* abort all failed request */ + /* Abort all failed request */ reqs = array_get(&failed_requests, &count); - i_assert(count > 0); /* at least one request timed out */ + i_assert(count > 0); /* At least one request timed out */ for (i = 0; i < count; i++) { struct http_client_request *req = reqs[i]; @@ -744,8 +766,8 @@ http_client_queue_request_timeout(struct http_client_queue *queue) e_debug(queue->event, "Absolute timeout expired for request %s (%s)", http_client_request_label(req), str_c(str)); - http_client_request_error(&req, - HTTP_CLIENT_REQUEST_ERROR_TIMED_OUT, + http_client_request_error( + &req, HTTP_CLIENT_REQUEST_ERROR_TIMED_OUT, t_strdup_printf( "Absolute request timeout expired (%s)", str_c(str))); @@ -759,19 +781,19 @@ http_client_queue_request_timeout(struct http_client_queue *queue) static void http_client_queue_set_request_timer(struct http_client_queue *queue, - const struct timeval *time) + const struct timeval *time) { i_assert(time->tv_sec > 0); - timeout_remove(&queue->to_request); + timeout_remove(&queue->to_request); e_debug(queue->event, "Set request timeout to %s.%03lu (now: %s.%03lu)", t_strflocaltime("%Y-%m-%d %H:%M:%S", time->tv_sec), - ((unsigned long)time->tv_usec)/1000, + ((unsigned long)time->tv_usec) / 1000, t_strflocaltime("%Y-%m-%d %H:%M:%S", ioloop_timeval.tv_sec), - ((unsigned long)ioloop_timeval.tv_usec)/1000); + ((unsigned long)ioloop_timeval.tv_usec) / 1000); - /* set timer */ + /* Set timer */ queue->to_request = timeout_add_absolute_to( queue->client->ioloop, time, http_client_queue_request_timeout, queue); @@ -779,7 +801,7 @@ http_client_queue_set_request_timer(struct http_client_queue *queue, static int http_client_queue_request_timeout_cmp(struct http_client_request *const *req1, - struct http_client_request *const *req2) + struct http_client_request *const *req2) { int ret; @@ -787,27 +809,29 @@ http_client_queue_request_timeout_cmp(struct http_client_request *const *req1, if ((*req1)->timeout_time.tv_sec == 0) { if ((*req2)->timeout_time.tv_sec == 0) { /* sort by age */ - if ((ret=timeval_cmp(&(*req1)->submit_time, &(*req2)->submit_time)) != 0) + ret = timeval_cmp(&(*req1)->submit_time, + &(*req2)->submit_time); + if (ret != 0) return ret; - } else { return 1; } } else if ((*req2)->timeout_time.tv_sec == 0) { return -1; - /* sort by timeout */ - } else if - ((ret=timeval_cmp(&(*req1)->timeout_time, &(*req2)->timeout_time)) != 0) { + /* Sort by timeout */ + } else if ((ret = timeval_cmp(&(*req1)->timeout_time, + &(*req2)->timeout_time)) != 0) { return ret; } - /* sort by minimum attempts for fairness */ + /* Sort by minimum attempts for fairness */ return ((*req2)->attempts - (*req1)->attempts); } -static void http_client_queue_submit_now(struct http_client_queue *queue, - struct http_client_request *req) +static void +http_client_queue_submit_now(struct http_client_queue *queue, + struct http_client_request *req) { ARRAY_TYPE(http_client_request) *req_queue; @@ -819,21 +843,21 @@ static void http_client_queue_submit_now(struct http_client_queue *queue, else req_queue = &queue->queued_requests; - /* enqueue */ + /* Enqueue */ if (req->timeout_time.tv_sec == 0) { - /* no timeout; enqueue at end */ + /* No timeout; enqueue at end */ array_push_back(req_queue, &req); - - } else if (timeval_diff_msecs(&req->timeout_time, &ioloop_timeval) <= 1) { - /* pretty much already timed out; don't bother */ + } else if (timeval_diff_msecs(&req->timeout_time, + &ioloop_timeval) <= 1) { + /* Pretty much already timed out; don't bother */ return; - } else { unsigned int insert_idx; - /* keep transmission queue sorted earliest timeout first */ - (void)array_bsearch_insert_pos(req_queue, - &req, http_client_queue_request_timeout_cmp, &insert_idx); + /* Keep transmission queue sorted earliest timeout first */ + (void)array_bsearch_insert_pos( + req_queue, &req, + http_client_queue_request_timeout_cmp, &insert_idx); array_insert(req_queue, insert_idx, &req, 1); } @@ -857,7 +881,8 @@ http_client_queue_delay_timeout(struct http_client_queue *queue) reqs = array_get(&queue->delayed_requests, &count); for (i = 0; i < count; i++) { if (timeval_cmp_margin(&reqs[i]->release_time, - &ioloop_timeval, TIMEOUT_CMP_MARGIN_USECS) > 0) { + &ioloop_timeval, + TIMEOUT_CMP_MARGIN_USECS) > 0) { break; } @@ -867,32 +892,32 @@ http_client_queue_delay_timeout(struct http_client_queue *queue) http_client_queue_submit_now(queue, reqs[i]); finished++; } - if (i < count) { + if (i < count) http_client_queue_set_delay_timer(queue, reqs[i]->release_time); - } array_delete(&queue->delayed_requests, 0, finished); } static void http_client_queue_set_delay_timer(struct http_client_queue *queue, - struct timeval time) + struct timeval time) { struct http_client *client = queue->client; int usecs = timeval_diff_usecs(&time, &ioloop_timeval); int msecs; - /* round up to nearest microsecond */ + /* Round up to nearest microsecond */ msecs = (usecs + 999) / 1000; - /* set timer */ - timeout_remove(&queue->to_delayed); - queue->to_delayed = timeout_add_to(client->ioloop, msecs, + /* Set timer */ + timeout_remove(&queue->to_delayed); + queue->to_delayed = timeout_add_to( + client->ioloop, msecs, http_client_queue_delay_timeout, queue); } static int http_client_queue_delayed_cmp(struct http_client_request *const *req1, - struct http_client_request *const *req2) + struct http_client_request *const *req2) { return timeval_cmp(&(*req1)->release_time, &(*req2)->release_time); } @@ -902,7 +927,7 @@ http_client_queue_delayed_cmp(struct http_client_request *const *req1, */ void http_client_queue_submit_request(struct http_client_queue *queue, - struct http_client_request *req) + struct http_client_request *req) { unsigned int insert_idx; @@ -910,15 +935,15 @@ void http_client_queue_submit_request(struct http_client_queue *queue, http_client_queue_drop_request(req->queue, req); req->queue = queue; - /* check delay vs timeout */ + /* Check delay vs timeout */ if (req->release_time.tv_sec > 0 && req->timeout_time.tv_sec > 0 && - timeval_cmp_margin(&req->release_time, - &req->timeout_time, TIMEOUT_CMP_MARGIN_USECS) >= 0) { - /* release time is later than absolute timeout */ + timeval_cmp_margin(&req->release_time, &req->timeout_time, + TIMEOUT_CMP_MARGIN_USECS) >= 0) { + /* Release time is later than absolute timeout */ req->release_time.tv_sec = 0; req->release_time.tv_usec = 0; - /* timeout rightaway */ + /* Timeout rightaway */ req->timeout_time = ioloop_timeval; e_debug(queue->event, @@ -927,41 +952,49 @@ void http_client_queue_submit_request(struct http_client_queue *queue, (req->urgent ? " (urgent)" : "")); } - /* add to main request list */ + /* Add to main request list */ if (req->timeout_time.tv_sec == 0) { - /* no timeout; just append */ + /* No timeout; just append */ array_push_back(&queue->requests, &req); - } else { unsigned int insert_idx; - /* keep main request list sorted earliest timeout first */ - (void)array_bsearch_insert_pos(&queue->requests, - &req, http_client_queue_request_timeout_cmp, &insert_idx); + /* Keep main request list sorted earliest timeout first */ + (void)array_bsearch_insert_pos( + &queue->requests, &req, + http_client_queue_request_timeout_cmp, &insert_idx); array_insert(&queue->requests, insert_idx, &req, 1); - /* now first in queue; update timer */ - if (insert_idx == 0) - http_client_queue_set_request_timer(queue, &req->timeout_time); + /* Now first in queue; update timer */ + if (insert_idx == 0) { + http_client_queue_set_request_timer(queue, + &req->timeout_time); + } } - /* handle delay */ + /* Handle delay */ if (req->release_time.tv_sec > 0) { io_loop_time_refresh(); - if (timeval_cmp_margin(&req->release_time, - &ioloop_timeval, TIMEOUT_CMP_MARGIN_USECS) > 0) { + if (timeval_cmp_margin(&req->release_time, &ioloop_timeval, + TIMEOUT_CMP_MARGIN_USECS) > 0) { e_debug(queue->event, - "Delayed request %s%s submitted (time remaining: %d msecs)", + "Delayed request %s%s submitted " + "(time remaining: %d msecs)", http_client_request_label(req), (req->urgent ? " (urgent)" : ""), - timeval_diff_msecs(&req->release_time, &ioloop_timeval)); - - (void)array_bsearch_insert_pos(&queue->delayed_requests, - &req, http_client_queue_delayed_cmp, &insert_idx); - array_insert(&queue->delayed_requests, insert_idx, &req, 1); - if (insert_idx == 0) - http_client_queue_set_delay_timer(queue, req->release_time); + timeval_diff_msecs(&req->release_time, + &ioloop_timeval)); + + (void)array_bsearch_insert_pos( + &queue->delayed_requests, &req, + http_client_queue_delayed_cmp, &insert_idx); + array_insert(&queue->delayed_requests, insert_idx, + &req, 1); + if (insert_idx == 0) { + http_client_queue_set_delay_timer( + queue, req->release_time); + } return; } } @@ -975,7 +1008,8 @@ void http_client_queue_submit_request(struct http_client_queue *queue, struct http_client_request * http_client_queue_claim_request(struct http_client_queue *queue, - const struct http_client_peer_addr *addr, bool no_urgent) + const struct http_client_peer_addr *addr, + bool no_urgent) { struct http_client_request *const *requests; struct http_client_request *req; @@ -1006,23 +1040,22 @@ http_client_queue_claim_request(struct http_client_queue *queue, unsigned int http_client_queue_requests_pending(struct http_client_queue *queue, - unsigned int *num_urgent_r) + unsigned int *num_urgent_r) { - unsigned int urg_count = array_count(&queue->queued_urgent_requests); + unsigned int urg_count = array_count(&queue->queued_urgent_requests); if (num_urgent_r != NULL) *num_urgent_r = urg_count; return array_count(&queue->queued_requests) + urg_count; } -unsigned int -http_client_queue_requests_active(struct http_client_queue *queue) +unsigned int http_client_queue_requests_active(struct http_client_queue *queue) { return array_count(&queue->requests); } /* - * ioloop + * Ioloop */ void http_client_queue_switch_ioloop(struct http_client_queue *queue)