From 6b97c4231ddca63dc85a1ff1d289cb125bae858a Mon Sep 17 00:00:00 2001
From: Lars Beckers <lars.beckers@rwth-aachen.de>
Date: Mon, 26 Apr 2021 00:24:59 +0200
Subject: [PATCH] dovecot: add options to finetune imap resource usage

---
 dovecot/defaults/main.yml                  |  4 ++++
 dovecot/templates/conf.d/10-master.conf.j2 | 12 ++++++++++--
 dovecot/templates/conf.d/20-imap.conf.j2   |  8 +++-----
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/dovecot/defaults/main.yml b/dovecot/defaults/main.yml
index b253b5b..24ac6b0 100644
--- a/dovecot/defaults/main.yml
+++ b/dovecot/defaults/main.yml
@@ -35,6 +35,10 @@ dovecot_sieve: 'file:~/sieve;active=~/.dovecot.sieve'
 
 # These variables were introduced for compatibility to a certain setup.
 # They may disappear without prior notice and/or may not work as expected.
+dovecot_process_limit: 100
+dovecot_client_limit: 1000
+dovecot_imap_idle_interval: '29 mins'
+dovecot_imap_max_userip_connections: 40
 dovecot_lda_mailbox_autocreate: false
 dovecot_lda_mailbox_autosubscribe: false
 dovecot_auth_realms: []
diff --git a/dovecot/templates/conf.d/10-master.conf.j2 b/dovecot/templates/conf.d/10-master.conf.j2
index 3aabeeb..957da6b 100644
--- a/dovecot/templates/conf.d/10-master.conf.j2
+++ b/dovecot/templates/conf.d/10-master.conf.j2
@@ -1,5 +1,5 @@
-#default_process_limit = 100
-#default_client_limit = 1000
+default_process_limit = {{ dovecot_process_limit }}
+default_client_limit = {{ dovecot_client_limit }}
 
 # Default VSZ (virtual memory size) limit for service processes. This is mainly
 # intended to catch and kill processes that leak memory before they eat up
@@ -99,7 +99,15 @@ service auth {
 #  user = $default_internal_user
    user = dovecot
    group = dovecot
+{% if dovecot_client_limit != 1000 %}
+  client_limit = {{ dovecot_client_limit * 2 }}
+{% endif %}
 }
+{% if dovecot_client_limit != 1000 %}
+service anvil {
+  client_limit = {{ dovecot_client_limit + 500 }}
+}
+{% endif %}
 
 service auth-worker {
   # Auth worker process is run as root by default, so that it can access
diff --git a/dovecot/templates/conf.d/20-imap.conf.j2 b/dovecot/templates/conf.d/20-imap.conf.j2
index a3ddfe4..77cf9b4 100644
--- a/dovecot/templates/conf.d/20-imap.conf.j2
+++ b/dovecot/templates/conf.d/20-imap.conf.j2
@@ -39,8 +39,7 @@
 
 # How long to wait between "OK Still here" notifications when client is
 # IDLEing.
-#imap_idle_notify_interval = 2 mins
-imap_idle_notify_interval = 29 mins
+imap_idle_notify_interval = {{ dovecot_imap_idle_interval }}
 
 # ID field names and values to send to clients. Using * as the value makes
 # Dovecot use the default value. The following fields have default values
@@ -92,12 +91,11 @@ imap_idle_notify_interval = 29 mins
 
 protocol imap {
   # Space separated list of plugins to load (default is global mail_plugins).
-  #mail_plugins = $mail_plugins
+  mail_plugins = $mail_plugins
 
   # Maximum number of IMAP connections allowed for a user from each IP address.
   # NOTE: The username is compared case-sensitively.
-  #mail_max_userip_connections = 10
-  mail_max_userip_connections = 40
+  mail_max_userip_connections = {{ dovecot_imap_max_userip_connections }}
 }
 
 {% if dovecot_content_filter %}
-- 
GitLab