Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keepalive interface to allow controlling the keepalive for specifi... #151

Merged
merged 1 commit into from
Nov 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions rpyc/core/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(self, sock):

@classmethod
def _connect(cls, host, port, family = socket.AF_INET, socktype = socket.SOCK_STREAM,
proto = 0, timeout = 3, nodelay = False, keepalive = False):
proto = 0, timeout = 3, nodelay = False, keepalive = None):
family, socktype, proto, _, sockaddr = socket.getaddrinfo(host, port, family,
socktype, proto)[0]
s = socket.socket(family, socktype, proto)
Expand All @@ -105,13 +105,18 @@ def _connect(cls, host, port, family = socket.AF_INET, socktype = socket.SOCK_ST
if nodelay:
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if keepalive:
if keepalive < 1:
raise ValueError("Keepalive minimal value is 1 minute")
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
# Linux specific: after 10 idle minutes, start sending keepalives every 5 minutes.
# Drop connection after 10 failed keepalives
num_of_keepalives = 5
# Linux specific: after <keepalive_internal> seconds, start sending keepalives every <keepalive_internal> seconds.
# Drop connection after 5 failed keepalives
if hasattr(socket, "TCP_KEEPIDLE") and hasattr(socket, "TCP_KEEPINTVL") and hasattr(socket, "TCP_KEEPCNT"):
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 10 * 60)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 5 * 60)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 10)
# based on the provided keepalive value (in minutes), keepalive_internal is defined
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, num_of_keepalives)
keepalive_internal = keepalive * 60 * 60 / num_of_keepalives * 60 + 60
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, keepalive_internal)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, keepalive_internal)
return s

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion rpyc/utils/classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def connect_pipes(input, output):
"""
return factory.connect_pipes(input, output, SlaveService)

def connect(host, port = DEFAULT_SERVER_PORT, ipv6 = False, keepalive = False):
def connect(host, port = DEFAULT_SERVER_PORT, ipv6 = False, keepalive=None):
"""
Creates a socket connection to the given host and port.

Expand Down
2 changes: 1 addition & 1 deletion rpyc/utils/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def connect_stdpipes(service = VoidService, config = {}):
"""
return connect_stream(PipeStream.from_std(), service = service, config = config)

def connect(host, port, service = VoidService, config = {}, ipv6 = False, keepalive = False):
def connect(host, port, service = VoidService, config = {}, ipv6 = False, keepalive = None):
"""
creates a socket-connection to the given host and port

Expand Down