Skip to content

Commit

Permalink
Improve connection handling when bootstrap list is invalid (dpkp#1507)
Browse files Browse the repository at this point in the history
* only perform single dns lookup for connect_blocking()
* fix blocking timeout in check_version()
  • Loading branch information
dpkp authored and 88manpreet committed Aug 1, 2018
1 parent fbf34e0 commit ee8b943
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions kafka/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,7 @@ def connect_blocking(self, timeout=float('inf')):
# First attempt to perform dns lookup
# note that the underlying interface, socket.getaddrinfo,
# has no explicit timeout so we may exceed the user-specified timeout
while time.time() < timeout:
if self._dns_lookup():
break
else:
return False
self._dns_lookup()

# Loop once over all returned dns entries
selector = None
Expand Down Expand Up @@ -903,6 +899,7 @@ def check_version(self, timeout=2, strict=False):
Returns: version tuple, i.e. (0, 10), (0, 9), (0, 8, 2), ...
"""
timeout_at = time.time() + timeout
log.info('Probing node %s broker version', self.node_id)
# Monkeypatch some connection configurations to avoid timeouts
override_config = {
Expand Down Expand Up @@ -932,7 +929,7 @@ def check_version(self, timeout=2, strict=False):
]

for version, request in test_cases:
if not self.connect_blocking(timeout):
if not self.connect_blocking(timeout_at - time.time()):
raise Errors.NodeNotReadyError()
f = self.send(request)
# HACK: sleeping to wait for socket to send bytes
Expand Down

0 comments on commit ee8b943

Please sign in to comment.