]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add connection_client_connect_async()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Thu, 24 Feb 2022 17:11:19 +0000 (18:11 +0100)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Fri, 4 Mar 2022 06:57:51 +0000 (06:57 +0000)
src/lib/connection.c
src/lib/connection.h

index 71ace0171cd70b123baa549c41cbdee346e8a068..23bda44789e89e9aa14aa96e3bea47021ff3b0dd 100644 (file)
@@ -761,6 +761,27 @@ int connection_client_connect(struct connection *conn)
                conn->list->set.unix_client_connect_msecs);
 }
 
+static void connection_client_connect_failed(struct connection *conn)
+{
+       timeout_remove(&conn->to);
+       errno = conn->connect_failed_errno;
+       conn->v.client_connected(conn, FALSE);
+       connection_closed(conn, CONNECTION_DISCONNECT_CONN_CLOSED);
+}
+
+int connection_client_connect_async(struct connection *conn)
+{
+       i_assert(conn->v.client_connected != NULL);
+
+       if (connection_client_connect(conn) < 0) {
+               i_assert(conn->to == NULL);
+               conn->connect_failed_errno = errno;
+               conn->to = timeout_add_short(0, connection_client_connect_failed, conn);
+               return -1;
+       }
+       return 0;
+}
+
 static void connection_update_counters(struct connection *conn)
 {
        if (conn->input != NULL)
index 398637cfdec9581e1f799508b5cca2f57f78695d..4bd836e2c9d1f228ffdeeb325af9366bfc99717e 100644 (file)
@@ -152,6 +152,7 @@ struct connection {
        /* handlers */
        struct connection_vfuncs v;
 
+       int connect_failed_errno;
        enum connection_disconnect_reason disconnect_reason;
 
        bool version_received:1;
@@ -200,7 +201,12 @@ void connection_init_from_streams(struct connection_list *list,
                                  struct istream *input,
                                  struct ostream *output) ATTR_NULL(3);
 
+/* connect() to the server. If the connect() fails immediately, return -1. */
 int connection_client_connect(struct connection *conn);
+/* connect() to the server. If the connect() fails immediately, call the
+   client_connected() and destroy() asynchronously from a timeout. This
+   simulates what happens on a delayed connect() failure. */
+int connection_client_connect_async(struct connection *conn);
 /* Connect to UNIX socket. If it fails, try it up to msecs is reached.
    Overrides connection_settings.unix_client_connect_msecs. */
 int connection_client_connect_with_retries(struct connection *conn,