comm_tcp: enable TCP keepalive on host side as well

This commit is contained in:
Sebastien Bourdeauducq 2015-08-15 16:03:00 +08:00
parent 0be0b199c1
commit dee844510c
1 changed files with 18 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import logging import logging
import socket import socket
import sys
from artiq.coredevice.comm_generic import CommGeneric from artiq.coredevice.comm_generic import CommGeneric
@ -7,6 +8,22 @@ from artiq.coredevice.comm_generic import CommGeneric
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def set_keepalive(sock, after_idle, interval, max_fails):
if sys.platform.startswith("linux"):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, after_idle)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, max_fails)
elif sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
# setting max_fails is not supported, typically ends up being 5 or 10
# depending on Windows version
sock.ioctl(socket.SIO_KEEPALIVE_VALS,
(1, after_idle*1000, interval*1000))
else:
logger.warning("TCP keepalive not supported on platform '%s', ignored",
sys.platform)
class Comm(CommGeneric): class Comm(CommGeneric):
def __init__(self, dmgr, host, port=1381): def __init__(self, dmgr, host, port=1381):
self.host = host self.host = host
@ -16,6 +33,7 @@ class Comm(CommGeneric):
if hasattr(self, "socket"): if hasattr(self, "socket"):
return return
self.socket = socket.create_connection((self.host, self.port)) self.socket = socket.create_connection((self.host, self.port))
set_keepalive(self.socket, 3, 2, 3)
logger.debug("connected to host %s on port %d", self.host, self.port) logger.debug("connected to host %s on port %d", self.host, self.port)
self.write(b"ARTIQ coredev\n") self.write(b"ARTIQ coredev\n")