summaryrefslogtreecommitdiffstats
path: root/src/lib-ldap/ldap-connection-pool.c
blob: 7635ce0e4fc342f62f031ea06388187495bf6dba (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "llist.h"
#include "ldap-private.h"
#include "ldap-connection-pool.h"

struct ldap_connection_pool {
	struct ldap_connection_list *conn_list;
	unsigned int conn_count;

	unsigned int max_connections;
};

static void ldap_connection_list_remove(struct ldap_connection_pool *pool,
					struct ldap_connection_list *list)
{
	DLLIST_REMOVE(&pool->conn_list, list);
	pool->conn_count--;

	ldap_connection_deinit(&list->conn);
	i_free(list);
}

static void
ldap_connection_pool_shrink_to(struct ldap_connection_pool *pool,
			       unsigned int max_count)
{
	struct ldap_connection_list *list, *next;

	list = pool->conn_list;
	for (; list != NULL && pool->conn_count > max_count; list = next) {
		next = list->next;
		if (list->refcount == 0)
			ldap_connection_list_remove(pool, list);
	}
}

struct ldap_connection_pool *
ldap_connection_pool_init(unsigned int max_connections)
{
	struct ldap_connection_pool *pool;

	pool = i_new(struct ldap_connection_pool, 1);
	pool->max_connections = max_connections;
	return pool;
}

void ldap_connection_pool_deinit(struct ldap_connection_pool **_pool)
{
	struct ldap_connection_pool *pool = *_pool;

	*_pool = NULL;

	ldap_connection_pool_shrink_to(pool, 0);
	i_assert(pool->conn_list == NULL);
	i_free(pool);
}

int ldap_connection_pool_get(struct ldap_connection_pool *pool,
			     struct ldap_client *client,
			     const struct ldap_client_settings *set,
			     struct ldap_connection_list **list_r,
			     const char **error_r)
{
	struct ldap_connection_list *list;
	struct ldap_connection *conn;

	for (list = pool->conn_list; list != NULL; list = list->next) {
		if (ldap_connection_have_settings(list->conn, set)) {
			list->refcount++;
			*list_r = list;
			return 0;
		}
	}
	if (ldap_connection_init(client, set, &conn, error_r) < 0)
		return -1;

	list = i_new(struct ldap_connection_list, 1);
	list->conn = conn;
	list->refcount++;

	DLLIST_PREPEND(&pool->conn_list, list);
	pool->conn_count++;

	ldap_connection_pool_shrink_to(pool, pool->max_connections);
	*list_r = list;
	return 0;
}

void ldap_connection_pool_unref(struct ldap_connection_pool *pool,
				struct ldap_connection_list **_list)
{
	struct ldap_connection_list *list = *_list;

	*_list = NULL;

	i_assert(list->refcount > 0);

	if (--list->refcount == 0)
		ldap_connection_pool_shrink_to(pool, pool->max_connections);
}

bool ldap_connection_pool_have_references(struct ldap_connection_pool *pool)
{
	struct ldap_connection_list *list;

	for (list = pool->conn_list; list != NULL; list = list->next) {
		if (list->refcount > 0)
			return TRUE;
	}
	return FALSE;
}