summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2016-12-13 11:37:29 +0100
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2016-12-13 11:37:50 +0100
commit927dce48ba8566d20b08a8392943dc23b8aebcc5 (patch)
treed2fefb08d38610e5edb70bf14f7848350a081b06
parent27eff31b4d4a89416d12cf68932fb498a073b0fb (diff)
downloadpykolab-927dce48ba8566d20b08a8392943dc23b8aebcc5.tar.gz
Set system keepalive parameters on the IMAP socket
-rw-r--r--pykolab/imap/__init__.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/pykolab/imap/__init__.py b/pykolab/imap/__init__.py
index 47625e1..63f9cc7 100644
--- a/pykolab/imap/__init__.py
+++ b/pykolab/imap/__init__.py
@@ -21,6 +21,7 @@
import logging
import re
import time
+import socket
import sys
from urlparse import urlparse
@@ -211,6 +212,11 @@ class IMAP(object):
# IMAP connection to be used.
self.imap = self._imap[hostname]
+ if hasattr(self.imap, 'm') and hasattr(self.imap.m, 'sock'):
+ self._set_socket_keepalive(self.imap.m.sock)
+ elif hasattr(self.imap, 'sock'):
+ self._set_socket_keepalive(self.imap.sock)
+
def disconnect(self, server=None):
if server == None:
# No server specified, but make sure self.imap is None anyways
@@ -839,6 +845,18 @@ class IMAP(object):
else:
return False
+ def _set_socket_keepalive(self, sock):
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
+
+ with open('/proc/sys/net/ipv4/tcp_keepalive_time', 'r') as f:
+ sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, (int)(f.read()))
+
+ with open('/proc/sys/net/ipv4/tcp_keepalive_intvl', 'r') as f:
+ sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, (int)(f.read()))
+
+ with open('/proc/sys/net/ipv4/tcp_keepalive_probes', 'r') as f:
+ sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, (int)(f.read()))
+
def _set_kolab_mailfolder_acls(self, acls, folder=None, update=False):
# special case, folder has no ACLs assigned and update was requested,
# remove all existing ACL entries