Skip to content

Instantly share code, notes, and snippets.

@novas0x2a
Created June 20, 2014 21:28
Show Gist options
  • Save novas0x2a/57213e44c6db93814068 to your computer and use it in GitHub Desktop.
Save novas0x2a/57213e44c6db93814068 to your computer and use it in GitHub Desktop.
From 890e8c22f605f65634a29d6d62e94ff3a64aa983 Mon Sep 17 00:00:00 2001
From: Mike Lundy <mike@fluffypenguin.org>
Date: Thu, 7 Nov 2013 05:07:39 -0800
Subject: Make host timeout independent of the number of hosts
- Every host gets the full timeout period
- Check whether we should be continuing after every host
---
kazoo/client.py | 4 ++++
kazoo/protocol/connection.py | 25 ++++++++++---------------
2 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/kazoo/client.py b/kazoo/client.py
index 5bd12a8..b55ee55 100644
--- a/kazoo/client.py
+++ b/kazoo/client.py
@@ -357,6 +357,10 @@ class KazooClient(object):
"""
+ if self.hosts:
+ raise ConfigurationError("Changing hosts at runtime is not "
+ "currently supported")
+
if randomize_hosts is None:
randomize_hosts = self.randomize_hosts
diff --git a/kazoo/protocol/connection.py b/kazoo/protocol/connection.py
index 650e1f6..7869e3d 100644
--- a/kazoo/protocol/connection.py
+++ b/kazoo/protocol/connection.py
@@ -1,4 +1,5 @@
"""Zookeeper Protocol Connection Handler"""
+import itertools
import logging
import os
import random
@@ -461,9 +462,10 @@ class ConnectionHandler(object):
retry = self.retry_sleeper.copy()
try:
+ hosts = itertools.cycle(self.client.hosts)
while not self.client._stopped.is_set():
# If the connect_loop returns STOP_CONNECTING, stop retrying
- if retry(self._connect_loop, retry) is STOP_CONNECTING:
+ if retry(self._connect_loop, hosts, retry) is STOP_CONNECTING:
break
except RetryFailedError:
self.logger.warning("Failed connecting to Zookeeper "
@@ -473,27 +475,20 @@ class ConnectionHandler(object):
self.client._session_callback(KeeperState.CLOSED)
self.logger.log(BLATHER, 'Connection stopped')
- def _connect_loop(self, retry):
- # Iterate through the hosts a full cycle before starting over
- status = None
- for host, port in self.client.hosts:
- if self.client._stopped.is_set():
- status = STOP_CONNECTING
- break
- status = self._connect_attempt(host, port, retry)
- if status is STOP_CONNECTING:
- break
-
- if status is STOP_CONNECTING:
+ def _connect_loop(self, hosts, retry):
+ if self.client._stopped.is_set():
+ return STOP_CONNECTING
+ elif self._connect_attempt(hosts, retry) is STOP_CONNECTING:
return STOP_CONNECTING
else:
raise ForceRetryError('Reconnecting')
- def _connect_attempt(self, host, port, retry):
+ def _connect_attempt(self, hosts, retry):
client = self.client
TimeoutError = self.handler.timeout_exception
close_connection = False
+ host, port = hosts.next()
self._socket = None
# Were we given a r/w server? If so, use that instead
@@ -594,7 +589,7 @@ class ConnectionHandler(object):
# Load return values
client._session_id = connect_result.session_id
negotiated_session_timeout = connect_result.time_out
- connect_timeout = negotiated_session_timeout / len(client.hosts)
+ connect_timeout = negotiated_session_timeout
read_timeout = negotiated_session_timeout * 2.0 / 3.0
client._session_passwd = connect_result.passwd
--
2.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment