Skip to content

Commit

Permalink
lib-imap-client: Change server IP only on connect() failures
Browse files Browse the repository at this point in the history
Also log an warning-level message about it. This is mainly useful to see
that a slow connection could be caused by a connect() timeout. Since more
IPs are still available, it's not yet an error.
  • Loading branch information
sirainen authored and GitLab committed May 19, 2017
1 parent 78aafd0 commit 651630f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/lib-imap-client/imapc-client.c
Expand Up @@ -409,7 +409,7 @@ void imapc_client_mailbox_reconnect(struct imapc_client_mailbox *box,
{
i_assert(!box->reconnecting);

imapc_connection_try_reconnect(box->conn, errmsg, 0);
imapc_connection_try_reconnect(box->conn, errmsg, 0, FALSE);
}

void imapc_client_mailbox_close(struct imapc_client_mailbox **_box)
Expand Down
19 changes: 12 additions & 7 deletions src/lib-imap-client/imapc-connection.c
Expand Up @@ -522,9 +522,12 @@ static void imapc_connection_reconnect(struct imapc_connection *conn)

void imapc_connection_try_reconnect(struct imapc_connection *conn,
const char *errstr,
unsigned int delay_msecs)
unsigned int delay_msecs,
bool connect_error)
{
if (conn->prev_connect_idx + 1 < conn->ips_count) {
/* Try the next IP address only for connect() problems. */
if (conn->prev_connect_idx + 1 < conn->ips_count && connect_error) {
i_warning("imapc(%s): %s - trying the next IP", conn->name, errstr);
conn->reconnect_ok = TRUE;
imapc_connection_disconnect_full(conn, TRUE);
imapc_connection_connect(conn);
Expand Down Expand Up @@ -1544,7 +1547,7 @@ static void imapc_connection_input(struct imapc_connection *conn)
str_printfa(str, "Server disconnected unexpectedly: %s",
errstr);
}
imapc_connection_try_reconnect(conn, str_c(str), 0);
imapc_connection_try_reconnect(conn, str_c(str), 0, FALSE);
}
imapc_connection_unref(&conn);
}
Expand Down Expand Up @@ -1642,7 +1645,7 @@ static void imapc_connection_connected(struct imapc_connection *conn)
imapc_connection_try_reconnect(conn, t_strdup_printf(
"connect(%s, %u) failed: %s",
net_ip2addr(ip), conn->client->set.port,
strerror(err)), conn->client->set.connect_retry_interval_msecs);
strerror(err)), conn->client->set.connect_retry_interval_msecs, TRUE);
return;
}
conn->io = io_add(conn->fd, IO_READ, imapc_connection_input, conn);
Expand All @@ -1657,12 +1660,14 @@ static void imapc_connection_timeout(struct imapc_connection *conn)
{
const struct ip_addr *ip = &conn->ips[conn->prev_connect_idx];
const char *errstr;
bool connect_error = FALSE;

switch (conn->state) {
case IMAPC_CONNECTION_STATE_CONNECTING:
errstr = t_strdup_printf("connect(%s, %u) timed out after %u seconds",
net_ip2addr(ip), conn->client->set.port,
conn->client->set.connect_timeout_msecs/1000);
connect_error = TRUE;
break;
case IMAPC_CONNECTION_STATE_AUTHENTICATING:
errstr = t_strdup_printf("Authentication timed out after %u seconds",
Expand All @@ -1671,7 +1676,7 @@ static void imapc_connection_timeout(struct imapc_connection *conn)
default:
i_unreached();
}
imapc_connection_try_reconnect(conn, errstr, 0);
imapc_connection_try_reconnect(conn, errstr, 0, connect_error);
}

static void
Expand Down Expand Up @@ -1726,7 +1731,7 @@ static void imapc_connection_connect_next_ip(struct imapc_connection *conn)
net_ip2addr(ip), conn->client->set.port);
if (conn->prev_connect_idx+1 == conn->ips_count) {
imapc_connection_try_reconnect(conn, "No more IP address(es) to try",
conn->client->set.connect_retry_interval_msecs);
conn->client->set.connect_retry_interval_msecs, TRUE);
return;
}
}
Expand Down Expand Up @@ -1923,7 +1928,7 @@ static void imapc_command_timeout(struct imapc_connection *conn)
i_assert(count > 0);

imapc_connection_try_reconnect(conn, t_strdup_printf(
"Command '%s' timed out", imapc_command_get_readable(cmds[0])), 0);
"Command '%s' timed out", imapc_command_get_readable(cmds[0])), 0, FALSE);
}

static bool
Expand Down
3 changes: 2 additions & 1 deletion src/lib-imap-client/imapc-connection.h
Expand Up @@ -35,7 +35,8 @@ void imapc_connection_disconnect_full(struct imapc_connection *conn,
bool reconnecting);
void imapc_connection_try_reconnect(struct imapc_connection *conn,
const char *errstr,
unsigned int delay_msecs);
unsigned int delay_msecs,
bool connect_error);
void imapc_connection_abort_commands(struct imapc_connection *conn,
struct imapc_client_mailbox *only_box,
bool keep_retriable) ATTR_NULL(2);
Expand Down

0 comments on commit 651630f

Please sign in to comment.