]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
indexer: worker-connection - Prepare for using connection.c
authorAki Tuomi <aki.tuomi@open-xchange.com>
Thu, 6 May 2021 07:59:24 +0000 (10:59 +0300)
committerTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 14 Jun 2021 14:56:40 +0000 (17:56 +0300)
src/indexer/worker-connection.c
src/indexer/worker-connection.h
src/indexer/worker-pool.c

index a2f619576a07ad8d2fef52c378747398e092b2b6..fe52d9b3a2254d2ea5635e87e7b4b53b178cfc7e 100644 (file)
@@ -6,6 +6,7 @@
 #include "connection.h"
 #include "ioloop.h"
 #include "istream.h"
+#include "llist.h"
 #include "ostream.h"
 #include "str.h"
 #include "strescape.h"
@@ -245,7 +246,8 @@ worker_connection_get_request(struct connection *conn)
 
 struct connection *
 worker_connection_create(const char *socket_path,
-                        indexer_status_callback_t *callback)
+                        indexer_status_callback_t *callback,
+                        struct connection_list *list)
 {
        struct worker_connection *conn;
 
@@ -254,5 +256,6 @@ worker_connection_create(const char *socket_path,
        conn->conn.base_name = i_strdup(socket_path);
        conn->callback = callback;
        conn->conn.fd_in = -1;
+       DLLIST_PREPEND(&list->connections, &conn->conn);
        return &conn->conn;
 }
index 80f83fb1b4d69c45d31504baabf160b20651122c..9b84a71221e75eb6770d618adf55c22311b396e2 100644 (file)
@@ -4,12 +4,16 @@
 #include "indexer.h"
 
 struct indexer_request;
+struct connection_list;
 
 struct connection *
 worker_connection_create(const char *socket_path,
-                        indexer_status_callback_t *callback);
+                        indexer_status_callback_t *callback,
+                        struct connection_list *list);
 void worker_connection_destroy(struct connection **conn);
 
+struct connection_list *worker_connection_list_create(void);
+
 int worker_connection_connect(struct connection *conn);
 /* Returns TRUE if worker is connected to (not necessarily handshaked yet) */
 bool worker_connection_is_connected(struct connection *conn);
index c102364c673343807ed8c0368774c765b1982664..072ea50a6a11fa008436c5bf191385a6d555c8be 100644 (file)
@@ -14,14 +14,9 @@ struct worker_pool {
        char *socket_path;
        indexer_status_callback_t *callback;
 
-       unsigned int connection_count;
-       struct connection_list connection_list;
+       struct connection_list *connection_list;
 };
 
-static void
-worker_connection_list_free(struct worker_pool *pool,
-                           struct connection *list);
-
 struct worker_pool *
 worker_pool_init(const char *socket_path, indexer_status_callback_t *callback)
 {
@@ -30,6 +25,7 @@ worker_pool_init(const char *socket_path, indexer_status_callback_t *callback)
        pool = i_new(struct worker_pool, 1);
        pool->socket_path = i_strdup(socket_path);
        pool->callback = callback;
+       pool->connection_list = worker_connection_list_create();
        return pool;
 }
 
@@ -39,20 +35,16 @@ void worker_pool_deinit(struct worker_pool **_pool)
 
        *_pool = NULL;
 
-       while (pool->connection_list.connections != NULL) {
-               struct connection *list = pool->connection_list.connections;
-
-               DLLIST_REMOVE(&pool->connection_list.connections, list);
-               worker_connection_list_free(pool, list);
-       }
+       connection_list_deinit(&pool->connection_list);
 
+       i_free(pool->connection_list);
        i_free(pool->socket_path);
        i_free(pool);
 }
 
 bool worker_pool_have_busy_connections(struct worker_pool *pool)
 {
-       return pool->connection_list.connections != NULL;
+       return pool->connection_list->connections_count > 0;
 }
 
 static int worker_pool_add_connection(struct worker_pool *pool,
@@ -60,10 +52,10 @@ static int worker_pool_add_connection(struct worker_pool *pool,
 {
        struct connection *conn;
 
-       pool->connection_count++;
-       conn = worker_connection_create(pool->socket_path, pool->callback);
-       if (worker_connection_connect(conn) < 0) {
-               worker_connection_destroy(&conn);
+       conn = worker_connection_create(pool->socket_path, pool->callback,
+                                       pool->connection_list);
+       if (connection_client_connect(conn) < 0) {
+               worker_connection_unref(&conn);
                return -1;
        }
 
@@ -71,25 +63,15 @@ static int worker_pool_add_connection(struct worker_pool *pool,
        return 0;
 }
 
-static void
-worker_connection_list_free(struct worker_pool *pool,
-                           struct connection *list)
-{
-       i_assert(pool->connection_count > 0);
-       pool->connection_count--;
-
-       worker_connection_destroy(&list);
-}
-
 static unsigned int worker_pool_find_max_connections(struct worker_pool *pool)
 {
        struct connection *list;
        unsigned int limit;
 
-       if (pool->connection_list.connections == NULL)
+       if (pool->connection_list->connections == NULL)
                return 1;
 
-       for (list = pool->connection_list.connections; list != NULL; list = list->next) {
+       for (list = pool->connection_list->connections; list != NULL; list = list->next) {
                if (worker_connection_get_process_limit(list, &limit))
                        return limit;
        }
@@ -104,30 +86,18 @@ bool worker_pool_get_connection(struct worker_pool *pool,
        unsigned int max_connections;
 
        max_connections = worker_pool_find_max_connections(pool);
-       if (pool->connection_count >= max_connections)
+       if (pool->connection_list->connections_count >= max_connections)
                return FALSE;
        if (worker_pool_add_connection(pool, conn_r) < 0)
                return FALSE;
-       DLLIST_PREPEND(&pool->connection_list.connections, *conn_r);
 
        return TRUE;
 }
 
-void worker_pool_release_connection(struct worker_pool *pool,
+void worker_pool_release_connection(struct worker_pool *pool ATTR_UNUSED,
                                    struct connection *conn)
 {
-       struct connection *list;
-
-       pool->connection_count--;
-       for (list = pool->connection_list.connections; list != NULL; list = list->next) {
-               if (list == conn)
-                       break;
-       }
-       i_assert(list != NULL);
-
-       DLLIST_REMOVE(&pool->connection_list.connections, list);
-
-       worker_connection_destroy(&conn);
+       worker_connection_unref(&conn);
 }
 
 struct connection *
@@ -137,7 +107,7 @@ worker_pool_find_username_connection(struct worker_pool *pool,
        struct connection *list;
        const char *worker_user;
 
-       for (list = pool->connection_list.connections; list != NULL; list = list->next) {
+       for (list = pool->connection_list->connections; list != NULL; list = list->next) {
                worker_user = worker_connection_get_username(list);
                if (worker_user != NULL && strcmp(worker_user, username) == 0)
                        return list;