summaryrefslogtreecommitdiffstats
path: root/src/indexer/worker-pool.c
blob: e0de136aa5a0d5274e9bffceade21a603e180611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "ioloop.h"
#include "llist.h"
#include "connection.h"
#include "master-service.h"
#include "worker-connection.h"
#include "worker-pool.h"

#define MAX_WORKER_IDLE_SECS (60*5)

struct worker_pool {
	char *socket_path;
	indexer_status_callback_t *callback;
	worker_available_callback_t *avail_callback;

	struct connection_list *connection_list;
};

struct worker_pool *
worker_pool_init(const char *socket_path, indexer_status_callback_t *callback,
		 worker_available_callback_t *avail_callback)
{
	struct worker_pool *pool;

	pool = i_new(struct worker_pool, 1);
	pool->socket_path = i_strdup(socket_path);
	pool->callback = callback;
	pool->avail_callback = avail_callback;
	pool->connection_list = worker_connection_list_create();
	return pool;
}

void worker_pool_deinit(struct worker_pool **_pool)
{
	struct worker_pool *pool = *_pool;

	*_pool = NULL;

	if (pool->connection_list != NULL)
		connection_list_deinit(&pool->connection_list);

	i_free(pool->connection_list);
	i_free(pool->socket_path);
	i_free(pool);
}

bool worker_pool_have_connections(struct worker_pool *pool)
{
	return pool->connection_list->connections != NULL;
}

static int worker_pool_add_connection(struct worker_pool *pool,
				      struct connection **conn_r)
{
	struct connection *conn;

	conn = worker_connection_create(pool->socket_path, pool->callback,
					pool->avail_callback,
					pool->connection_list);
	if (connection_client_connect(conn) < 0) {
		worker_connection_destroy(conn);
		return -1;
	}

	*conn_r = conn;
	return 0;
}

bool worker_pool_get_connection(struct worker_pool *pool,
				struct connection **conn_r)
{
	unsigned int max_connections;

	max_connections = I_MAX(1, worker_connections_get_process_limit());
	if (pool->connection_list->connections_count >= max_connections)
		return FALSE;
	if (worker_pool_add_connection(pool, conn_r) < 0)
		return FALSE;

	return TRUE;
}

struct connection *
worker_pool_find_username_connection(struct worker_pool *pool,
				     const char *username)
{
	struct connection *list;
	const char *worker_user;

	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;
	}
	return NULL;
}