changeset 21332:51ace2c84845

lib-http: client: Moved connection backoff timer management to separate functions.
author Stephan Bosch <stephan.bosch@dovecot.fi>
date Fri, 16 Dec 2016 22:00:47 +0100
parents 0fcd4cdd97ed
children f7bc77720fe5
files src/lib-http/http-client-peer.c
diffstat 1 files changed, 25 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-http/http-client-peer.c	Fri Dec 16 21:41:29 2016 +0100
+++ b/src/lib-http/http-client-peer.c	Fri Dec 16 22:00:47 2016 +0100
@@ -183,6 +183,28 @@
 }
 
 static void
+http_client_peer_increase_backoff_timer(struct http_client_peer *peer)
+{
+	const struct http_client_settings *set = &peer->client->set;
+
+	if (peer->backoff_time_msecs == 0)
+		peer->backoff_time_msecs = set->connect_backoff_time_msecs;
+	else
+		peer->backoff_time_msecs *= 2;
+	if (peer->backoff_time_msecs > set->connect_backoff_max_time_msecs)
+		peer->backoff_time_msecs = set->connect_backoff_max_time_msecs;
+}
+
+static void
+http_client_peer_reset_backoff_timer(struct http_client_peer *peer)
+{
+	peer->backoff_time_msecs = 0;
+
+	if (peer->to_backoff != NULL)
+		timeout_remove(&peer->to_backoff);
+}
+
+static void
 http_client_peer_connect(struct http_client_peer *peer, unsigned int count)
 {
 	if (http_client_peer_start_backoff_timer(peer))
@@ -745,10 +767,7 @@
 		array_count(&peer->conns));
 
 	peer->last_failure.tv_sec = peer->last_failure.tv_usec = 0;
-	peer->backoff_time_msecs = 0;
-
-	if (peer->to_backoff != NULL)
-		timeout_remove(&peer->to_backoff);
+	http_client_peer_reset_backoff_timer(peer);
 
 	array_foreach(&peer->queues, queue) {
 		http_client_queue_connection_success(*queue, &peer->addr);
@@ -760,7 +779,6 @@
 void http_client_peer_connection_failure(struct http_client_peer *peer,
 					 const char *reason)
 {
-	const struct http_client_settings *set = &peer->client->set;
 	struct http_client_queue *const *queue;
 	unsigned int pending;
 
@@ -776,14 +794,8 @@
 		array_count(&peer->conns), pending);
 
 	/* manage backoff timer only when this was the only attempt */
-	if (pending == 1) {
-		if (peer->backoff_time_msecs == 0)
-			peer->backoff_time_msecs = set->connect_backoff_time_msecs;
-		else
-			peer->backoff_time_msecs *= 2;
-		if (peer->backoff_time_msecs > set->connect_backoff_max_time_msecs)
-			peer->backoff_time_msecs = set->connect_backoff_max_time_msecs;
-	}
+	if (pending == 1)
+		http_client_peer_increase_backoff_timer(peer);
 
 	if (pending > 1) {
 		/* if there are other connections attempting to connect, wait