1
/* Copyright (c) 2011-2012 Dovecot authors, see the included COPYING file */
7
#include "global-memory.h"
8
#include "stats-settings.h"
9
#include "mail-stats.h"
10
#include "mail-domain.h"
12
static struct hash_table *mail_domains_hash;
13
/* domains are sorted by their last_update timestamp, oldest first */
14
static struct mail_domain *mail_domains_head, *mail_domains_tail;
15
struct mail_domain *stable_mail_domains;
17
static size_t mail_domain_memsize(const struct mail_domain *domain)
19
return sizeof(*domain) + strlen(domain->name) + 1;
22
struct mail_domain *mail_domain_login(const char *name)
24
struct mail_domain *domain;
26
domain = hash_table_lookup(mail_domains_hash, name);
29
mail_domain_refresh(domain, NULL);
33
domain = i_new(struct mail_domain, 1);
34
domain->name = i_strdup(name);
35
domain->reset_timestamp = ioloop_time;
37
hash_table_insert(mail_domains_hash, domain->name, domain);
38
DLLIST_PREPEND_FULL(&stable_mail_domains, domain,
39
stable_prev, stable_next);
40
DLLIST2_APPEND_FULL(&mail_domains_head, &mail_domains_tail, domain,
41
sorted_prev, sorted_next);
43
domain->last_update = ioloop_timeval;
44
global_memory_alloc(mail_domain_memsize(domain));
48
struct mail_domain *mail_domain_lookup(const char *name)
50
return hash_table_lookup(mail_domains_hash, name);
53
void mail_domain_ref(struct mail_domain *domain)
58
void mail_domain_unref(struct mail_domain **_domain)
60
struct mail_domain *domain = *_domain;
62
i_assert(domain->refcount > 0);
68
static void mail_domain_free(struct mail_domain *domain)
70
i_assert(domain->refcount == 0);
71
i_assert(domain->users == NULL);
73
global_memory_free(mail_domain_memsize(domain));
74
hash_table_remove(mail_domains_hash, domain->name);
75
DLLIST_REMOVE_FULL(&stable_mail_domains, domain,
76
stable_prev, stable_next);
77
DLLIST2_REMOVE_FULL(&mail_domains_head, &mail_domains_tail, domain,
78
sorted_prev, sorted_next);
84
void mail_domain_refresh(struct mail_domain *domain,
85
const struct mail_stats *diff_stats)
87
if (diff_stats != NULL)
88
mail_stats_add(&domain->stats, diff_stats);
89
domain->last_update = ioloop_timeval;
90
DLLIST2_REMOVE_FULL(&mail_domains_head, &mail_domains_tail, domain,
91
sorted_prev, sorted_next);
92
DLLIST2_APPEND_FULL(&mail_domains_head, &mail_domains_tail, domain,
93
sorted_prev, sorted_next);
96
void mail_domains_free_memory(void)
100
while (mail_domains_head != NULL && mail_domains_head->refcount == 0) {
101
mail_domain_free(mail_domains_head);
103
if (global_used_memory < stats_settings->memory_limit ||
104
mail_domains_head == NULL)
107
diff = ioloop_time - mail_domains_head->last_update.tv_sec;
108
if (diff < stats_settings->domain_min_time)
113
void mail_domains_init(void)
116
hash_table_create(default_pool, default_pool, 0,
117
str_hash, (hash_cmp_callback_t *)strcmp);
120
void mail_domains_deinit(void)
122
while (mail_domains_head != NULL)
123
mail_domain_free(mail_domains_head);
124
hash_table_destroy(&mail_domains_hash);