Skip to content

Commit ae8bbad

Browse files
committed
Clear connection gai cache after all entries fail
1 parent be3f126 commit ae8bbad

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

kafka/conn.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ def __init__(self, host, port, afi, **configs):
203203
self.afi = afi
204204
self._init_host = host
205205
self._init_port = port
206-
self._init_afi = afi
207206
self.in_flight_requests = collections.deque()
208207
self._api_versions = None
209208

@@ -264,7 +263,7 @@ def connect(self):
264263
log.debug('%s: creating new socket', self)
265264
# if self.afi is set to AF_UNSPEC, then we need to do a name
266265
# resolution and try all available address families
267-
if self._init_afi == socket.AF_UNSPEC:
266+
if self.afi == socket.AF_UNSPEC:
268267
if self._gai is None:
269268
# XXX: all DNS functions in Python are blocking. If we really
270269
# want to be non-blocking here, we need to use a 3rd-party
@@ -291,10 +290,10 @@ def connect(self):
291290
self._gai_index += 1
292291
while True:
293292
if self._gai_index >= len(self._gai):
294-
error = 'Unable to connect to any of the names for {0}:{1}'.format(
295-
self._init_host, self._init_port)
296-
log.error(error)
297-
self.close(Errors.ConnectionError(error))
293+
log.error('Unable to connect to any of the names for {0}:{1}'
294+
.format(self._init_host, self._init_port))
295+
self._gai = None
296+
self._gai_index = 0
298297
return
299298
afi, _, __, ___, sockaddr = self._gai[self._gai_index]
300299
if afi not in (socket.AF_INET, socket.AF_INET6):
@@ -304,7 +303,7 @@ def connect(self):
304303
self.host, self.port = sockaddr[:2]
305304
self._sock = socket.socket(afi, socket.SOCK_STREAM)
306305
else:
307-
self._sock = socket.socket(self._init_afi, socket.SOCK_STREAM)
306+
self._sock = socket.socket(self.afi, socket.SOCK_STREAM)
308307

309308
for option in self.config['socket_options']:
310309
log.debug('%s: setting socket option %s', self, option)
@@ -328,10 +327,10 @@ def connect(self):
328327
ret = None
329328
try:
330329
ret = self._sock.connect_ex((self.host, self.port))
331-
# if we got here through a host lookup, we've found a host,port,af tuple
332-
# that works save it so we don't do a GAI lookup again
330+
# if we got here through a host lookup,
331+
# we've found a (host, port, af) tuple that works
332+
# and we can stop iterating through the getaddrinfo list
333333
if self._gai is not None:
334-
self.afi = self._sock.family
335334
self._gai = None
336335
except socket.error as err:
337336
ret = err.errno

test/test_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_connect_dns_failure(_socket, conn):
7070
assert conn.state is ConnectionStates.DISCONNECTED
7171
assert conn._gai is None
7272
# Setup _gai / index to state where we need gai and there are no more entries to test
73-
conn._init_afi = socket.AF_UNSPEC
73+
conn.afi = socket.AF_UNSPEC
7474
conn._gai = ['foo']
7575
conn._gai_index = 1
7676
conn.connect()

0 commit comments

Comments
 (0)