Skip to content

Commit

Permalink
lib-http: client: Moved connection backoff timer management to separa…
Browse files Browse the repository at this point in the history
…te functions.
  • Loading branch information
stephanbosch authored and sirainen committed Dec 20, 2016
1 parent 2cf7f1b commit adbc1f7
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/lib-http/http-client-peer.c
Expand Up @@ -182,6 +182,28 @@ http_client_peer_start_backoff_timer(struct http_client_peer *peer)
return FALSE;
}

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)
{
Expand Down Expand Up @@ -745,10 +767,7 @@ void http_client_peer_connection_success(struct http_client_peer *peer)
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);
Expand All @@ -760,7 +779,6 @@ void http_client_peer_connection_success(struct http_client_peer *peer)
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;

Expand All @@ -776,14 +794,8 @@ void http_client_peer_connection_failure(struct http_client_peer *peer,
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
Expand Down

0 comments on commit adbc1f7

Please sign in to comment.