Skip to content
Browse files

Only reconnect if a given time has passed

This is a per-connection timeout, default is 60 seconds.
  • Loading branch information...
1 parent 7b6fa52 commit fd19aabbe3192503cb876ab9690592ff29a4bfaa @klange klange committed with Oct 2, 2012
Showing with 13 additions and 4 deletions.
  1. +11 −4 gearman/connection.py
  2. +2 −0 tests/_core_testing.py
View
15 gearman/connection.py
@@ -11,6 +11,8 @@
gearman_logger = logging.getLogger(__name__)
+RECONNECT_TIMEOUT_DEFAULT = 60.0
+
class GearmanConnection(object):
"""A connection between a client/worker and a server. Can be used to reconnect (unlike a socket)
@@ -30,6 +32,8 @@ def __init__(self, host=None, port=DEFAULT_GEARMAN_PORT):
self.gearman_host = host
self.gearman_port = port
+ self.reconnect_timeout = RECONNECT_TIMEOUT_DEFAULT
+
if host is None:
raise ServerUnavailable("No host specified")
@@ -91,6 +95,8 @@ def connect(self):
self._is_client_side = True
self._is_server_side = False
+ self.connect_time = time.time()
+
def _create_client_socket(self):
"""Creates a client side socket and subsequently binds/configures our socket options"""
try:
@@ -190,10 +196,11 @@ def send_commands_to_buffer(self):
self._outgoing_buffer = ''.join(packed_data)
def reconnect(self):
- """If we don't have outgoing data waiting, close the current connection and open a new one."""
- if not self._outgoing_buffer:
- self.close()
- self.connect()
+ """If we don't have outgoing data waiting after a length of time, close the current connection and open a new one."""
+ if self.connect_time < time.time() - self.reconnect_timeout:
+ if not self._outgoing_buffer:
+ self.close()
+ self.connect()
def send_data_to_socket(self):
"""Send data from buffer -> socket
View
2 tests/_core_testing.py
@@ -20,6 +20,8 @@ def __init__(self, host=None, port=DEFAULT_GEARMAN_PORT):
self._fail_on_bind = False
self._fail_on_read = False
self._fail_on_write = False
+ self.connect_time = 0.0
+ self.reconnect_timeout = 0.0
def _create_client_socket(self):
if self._fail_on_bind:

0 comments on commit fd19aab

Please sign in to comment.
Something went wrong with that request. Please try again.