Skip to content

Commit 472f287

Browse files
author
Dana Powers
committed
Remove self._dirty and check self._sock instead in kafka.conn
1 parent 2493688 commit 472f287

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

kafka/conn.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def __init__(self, host, port, timeout=DEFAULT_SOCKET_TIMEOUT_SECONDS):
5555
self.host = host
5656
self.port = port
5757
self.timeout = timeout
58-
self._dirty = None
5958
self._sock = None
6059

6160
self.reinit()
@@ -68,7 +67,11 @@ def __repr__(self):
6867
###################
6968

7069
def _raise_connection_error(self):
71-
self._dirty = True
70+
# Cleanup socket if we have one
71+
if self._sock:
72+
self.close()
73+
74+
# And then raise
7275
raise ConnectionError("Kafka @ {0}:{1} went away".format(self.host, self.port))
7376

7477
def _read_bytes(self, num_bytes):
@@ -78,7 +81,7 @@ def _read_bytes(self, num_bytes):
7881
log.debug("About to read %d bytes from Kafka", num_bytes)
7982

8083
# Make sure we have a connection
81-
if self._dirty or not self._sock:
84+
if not self._sock:
8285
self.reinit()
8386

8487
while bytes_left:
@@ -110,7 +113,7 @@ def send(self, request_id, payload):
110113
log.debug("About to send %d bytes to Kafka, request %d" % (len(payload), request_id))
111114

112115
# Make sure we have a connection
113-
if self._dirty or not self._sock:
116+
if not self._sock:
114117
self.reinit()
115118

116119
try:
@@ -158,10 +161,12 @@ def reinit(self):
158161
Re-initialize the socket connection
159162
"""
160163
log.debug("Reinitializing socket connection for %s:%d" % (self.host, self.port))
161-
self.close()
164+
165+
if self._sock:
166+
self.close()
167+
162168
try:
163169
self._sock = socket.create_connection((self.host, self.port), self.timeout)
164-
self._dirty = False
165170
except socket.error:
166171
log.exception('Unable to connect to kafka broker at %s:%d' % (self.host, self.port))
167172
self._raise_connection_error()

0 commit comments

Comments
 (0)