dovecot-2.1: imapc: Support retrying some IMAP commands if we ge...
dovecot at dovecot.org
dovecot at dovecot.org
Sun Oct 9 20:29:51 EEST 2011
details: http://hg.dovecot.org/dovecot-2.1/rev/c9594ff166a9
changeset: 13628:c9594ff166a9
user: Timo Sirainen <tss at iki.fi>
date: Sun Oct 09 20:38:11 2011 +0300
description:
imapc: Support retrying some IMAP commands if we get disconnected.
diffstat:
src/lib-imap-client/imapc-client-private.h | 1 +
src/lib-imap-client/imapc-client.c | 19 ++-
src/lib-imap-client/imapc-connection.c | 157 +++++++++++++++---------
src/lib-imap-client/imapc-connection.h | 1 +
src/lib-storage/index/imapc/imapc-mail-fetch.c | 1 +
src/lib-storage/index/imapc/imapc-storage.c | 3 +
src/lib-storage/index/imapc/imapc-storage.h | 1 +
src/lib-storage/index/imapc/imapc-sync.c | 17 ++
src/lib-storage/index/imapc/imapc-sync.h | 1 +
9 files changed, 142 insertions(+), 59 deletions(-)
diffs (truncated from 455 to 300 lines):
diff -r f63f2b2217c3 -r c9594ff166a9 src/lib-imap-client/imapc-client-private.h
--- a/src/lib-imap-client/imapc-client-private.h Sun Oct 09 20:36:28 2011 +0300
+++ b/src/lib-imap-client/imapc-client-private.h Sun Oct 09 20:38:11 2011 +0300
@@ -35,6 +35,7 @@
unsigned int pending_box_command_count;
bool reconnect_ok;
+ bool reconnecting;
};
void imapc_client_ref(struct imapc_client *client);
diff -r f63f2b2217c3 -r c9594ff166a9 src/lib-imap-client/imapc-client.c
--- a/src/lib-imap-client/imapc-client.c Sun Oct 09 20:36:28 2011 +0300
+++ b/src/lib-imap-client/imapc-client.c Sun Oct 09 20:38:11 2011 +0300
@@ -258,16 +258,27 @@
{
struct imapc_client_mailbox *box = context;
+ i_assert(box->reconnecting);
+ box->reconnecting = FALSE;
+
if (reply->state == IMAPC_COMMAND_STATE_OK) {
/* reopen the mailbox */
box->reopen_callback(box->reopen_context);
+ } else {
+ imapc_connection_abort_commands(box->conn);
}
}
void imapc_client_mailbox_reconnect(struct imapc_client_mailbox *box)
{
+ bool reconnect = box->reopen_callback != NULL && box->reconnect_ok;
+
+ if (reconnect) {
+ i_assert(!box->reconnecting);
+ box->reconnecting = TRUE;
+ }
imapc_connection_disconnect(box->conn);
- if (box->reopen_callback != NULL && box->reconnect_ok) {
+ if (reconnect) {
imapc_connection_connect(box->conn,
imapc_client_reconnect_cb, box);
}
@@ -282,6 +293,12 @@
/* cancel any pending commands */
imapc_connection_unselect(box);
+ if (box->reconnecting) {
+ /* need to abort the reconnection so it won't try to access
+ the box */
+ imapc_connection_disconnect(box->conn);
+ }
+
/* set this only after unselect, which may cancel some commands that
reference this box */
*_box = NULL;
diff -r f63f2b2217c3 -r c9594ff166a9 src/lib-imap-client/imapc-connection.c
--- a/src/lib-imap-client/imapc-connection.c Sun Oct 09 20:36:28 2011 +0300
+++ b/src/lib-imap-client/imapc-connection.c Sun Oct 09 20:38:11 2011 +0300
@@ -63,6 +63,7 @@
/* Waiting for '+' literal reply before we can continue */
unsigned int wait_for_literal:1;
};
+ARRAY_DEFINE_TYPE(imapc_command, struct imapc_command *);
struct imapc_connection_literal {
char *temp_path;
@@ -103,9 +104,9 @@
void *login_context;
/* commands pending in queue to be sent */
- ARRAY_DEFINE(cmd_send_queue, struct imapc_command *);
+ ARRAY_TYPE(imapc_command) cmd_send_queue;
/* commands that have been sent, waiting for their tagged reply */
- ARRAY_DEFINE(cmd_wait_list, struct imapc_command *);
+ ARRAY_TYPE(imapc_command) cmd_wait_list;
unsigned int ips_count, prev_connect_idx;
struct ip_addr *ips;
@@ -209,29 +210,70 @@
}
static void
-imapc_connection_abort_pending_commands(struct imapc_connection *conn,
- const struct imapc_command_reply *reply)
+imapc_connection_abort_commands_array(ARRAY_TYPE(imapc_command) *cmd_array,
+ ARRAY_TYPE(imapc_command) *dest_array,
+ bool keep_retriable)
{
struct imapc_command *const *cmdp, *cmd;
+ unsigned int i;
- while (array_count(&conn->cmd_wait_list) > 0) {
- cmdp = array_idx(&conn->cmd_wait_list, 0);
+ for (i = 0; i < array_count(cmd_array); ) {
+ cmdp = array_idx(cmd_array, i);
cmd = *cmdp;
- array_delete(&conn->cmd_wait_list, 0, 1);
- if (cmd->callback != NULL)
- cmd->callback(reply, cmd->context);
+ if (keep_retriable &&
+ (cmd->flags & IMAPC_COMMAND_FLAG_RETRIABLE) != 0) {
+ cmd->send_pos = 0;
+ cmd->wait_for_literal = 0;
+ i++;
+ } else {
+ array_delete(cmd_array, i, 1);
+ array_append(dest_array, &cmd, 1);
+ }
+ }
+}
+
+static void
+imapc_connection_abort_commands_full(struct imapc_connection *conn,
+ bool keep_retriable)
+{
+ struct imapc_command *const *cmdp, *cmd;
+ ARRAY_TYPE(imapc_command) tmp_array;
+ struct imapc_command_reply reply;
+
+ t_array_init(&tmp_array, 8);
+ imapc_connection_abort_commands_array(&conn->cmd_wait_list,
+ &tmp_array, keep_retriable);
+ imapc_connection_abort_commands_array(&conn->cmd_send_queue,
+ &tmp_array, keep_retriable);
+
+ if (array_count(&conn->cmd_wait_list) > 0) {
+ /* need to move all the waiting commands to send queue */
+ array_append_array(&conn->cmd_wait_list,
+ &conn->cmd_send_queue);
+ array_clear(&conn->cmd_send_queue);
+ array_append_array(&conn->cmd_send_queue,
+ &conn->cmd_wait_list);
+ array_clear(&conn->cmd_wait_list);
+ }
+
+ /* abort the commands. we'll do it here later so that if the
+ callback recurses us back here we don't crash */
+ memset(&reply, 0, sizeof(reply));
+ reply.state = IMAPC_COMMAND_STATE_DISCONNECTED;
+ reply.text_without_resp = reply.text_full =
+ "Disconnected from server";
+ array_foreach(&tmp_array, cmdp) {
+ cmd = *cmdp;
+
+ cmd->callback(&reply, cmd->context);
imapc_command_free(cmd);
}
- while (array_count(&conn->cmd_send_queue) > 0) {
- cmdp = array_idx(&conn->cmd_send_queue, 0);
- cmd = *cmdp;
- array_delete(&conn->cmd_send_queue, 0, 1);
+}
- if (cmd->callback != NULL)
- cmd->callback(reply, cmd->context);
- imapc_command_free(cmd);
- }
+void imapc_connection_abort_commands(struct imapc_connection *conn)
+{
+ imapc_connection_abort_commands_full(conn, FALSE);
}
static void
@@ -262,7 +304,6 @@
reply.state = IMAPC_COMMAND_STATE_DISCONNECTED;
reply.text_without_resp = reply.text_full =
"Disconnected from server";
- imapc_connection_abort_pending_commands(conn, &reply);
imapc_login_callback(conn, &reply);
conn->idling = FALSE;
@@ -272,9 +313,6 @@
conn->selecting_box = NULL;
conn->selected_box = NULL;
break;
- case IMAPC_CONNECTION_STATE_DONE:
- imapc_command_send_more(conn);
- break;
default:
break;
}
@@ -306,6 +344,9 @@
void imapc_connection_disconnect(struct imapc_connection *conn)
{
+ bool reconnecting = conn->selected_box != NULL &&
+ conn->selected_box->reconnecting;
+
if (conn->fd == -1)
return;
@@ -327,6 +368,13 @@
net_disconnect(conn->fd);
conn->fd = -1;
+ imapc_connection_abort_commands_full(conn, reconnecting);
+ imapc_connection_set_state(conn, IMAPC_CONNECTION_STATE_DISCONNECTED);
+}
+
+static void imapc_connection_set_disconnected(struct imapc_connection *conn)
+{
+ imapc_connection_abort_commands(conn);
imapc_connection_set_state(conn, IMAPC_CONNECTION_STATE_DISCONNECTED);
}
@@ -794,6 +842,7 @@
{
const struct imap_arg *imap_args;
const char *name, *value;
+ struct imap_parser *parser;
struct imapc_untagged_reply reply;
int ret;
@@ -854,7 +903,12 @@
reply.untagged_box_context =
conn->selected_box->untagged_box_context;
}
+
+ /* the callback may disconnect and destroy the parser */
+ parser = conn->parser;
+ imap_parser_ref(parser);
conn->client->untagged_callback(&reply, conn->client->untagged_context);
+ imap_parser_unref(&parser);
imapc_connection_input_reset(conn);
return 1;
}
@@ -893,8 +947,7 @@
imapc_command_reply_free(struct imapc_command *cmd,
const struct imapc_command_reply *reply)
{
- if (cmd->callback != NULL)
- cmd->callback(reply, cmd->context);
+ cmd->callback(reply, cmd->context);
imapc_command_free(cmd);
}
@@ -904,6 +957,7 @@
unsigned int i, count;
char *line, *linep;
const char *p;
+ struct imap_parser *parser;
struct imapc_command_reply reply;
line = i_stream_next_line(conn->input);
@@ -985,7 +1039,13 @@
}
imapc_connection_input_reset(conn);
+
+ parser = conn->parser;
+ imap_parser_ref(parser);
imapc_command_reply_free(cmd, &reply);
+ imap_parser_unref(&parser);
+
+ imapc_command_send_more(conn);
return 1;
}
@@ -1215,8 +1275,7 @@
ip = &conn->ips[conn->prev_connect_idx];
fd = net_connect_ip(ip, conn->client->set.port, NULL);
if (fd == -1) {
- imapc_connection_set_state(conn,
- IMAPC_CONNECTION_STATE_DISCONNECTED);
+ imapc_connection_set_disconnected(conn);
return;
}
conn->fd = fd;
@@ -1252,8 +1311,7 @@
if (result->ret != 0) {
i_error("imapc(%s): dns_lookup(%s) failed: %s",
conn->name, conn->client->set.host, result->error);
- imapc_connection_set_state(conn,
- IMAPC_CONNECTION_STATE_DISCONNECTED);
+ imapc_connection_set_disconnected(conn);
return;
}
@@ -1483,10 +1541,14 @@
/* SELECT/EXAMINE command */
imapc_connection_set_selecting(cmd->box);
} else if (!imapc_client_mailbox_is_opened(cmd->box)) {
+ if (cmd->box->reconnecting) {
+ /* wait for SELECT/EXAMINE */
+ return;
+ }
/* shouldn't normally happen */
memset(&reply, 0, sizeof(reply));
reply.text_without_resp = reply.text_full = "Mailbox not open";
- reply.state = IMAPC_COMMAND_STATE_BAD;
+ reply.state = IMAPC_COMMAND_STATE_DISCONNECTED;
array_delete(&conn->cmd_send_queue, 0, 1);
imapc_command_reply_free(cmd, &reply);
@@ -1577,7 +1639,14 @@
imapc_command_timeout, conn);
}
}
More information about the dovecot-cvs
mailing list