Skip to content

Commit 2e6a438

Browse files
committed
Rely on selector to detect completed connection attempts
1 parent 61fa0b2 commit 2e6a438

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

kafka/client_async.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ def _conn_state_change(self, node_id, sock, conn):
267267
if node_id not in self._connecting:
268268
self._connecting.add(node_id)
269269
try:
270-
self._selector.register(sock, selectors.EVENT_WRITE)
270+
self._selector.register(sock, selectors.EVENT_WRITE, conn)
271271
except KeyError:
272-
self._selector.modify(sock, selectors.EVENT_WRITE)
272+
self._selector.modify(sock, selectors.EVENT_WRITE, conn)
273273

274274
if self.cluster.is_bootstrap(node_id):
275275
self._last_bootstrap = time.time()
@@ -624,6 +624,9 @@ def _poll(self, timeout):
624624
self._clear_wake_fd()
625625
continue
626626
elif not (events & selectors.EVENT_READ):
627+
conn = key.data
628+
if conn.node_id in self._connecting:
629+
self._maybe_connect(conn.node_id)
627630
continue
628631
conn = key.data
629632
processed.add(conn)

kafka/conn.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -748,16 +748,16 @@ def connection_delay(self):
748748
"""
749749
Return the number of milliseconds to wait, based on the connection
750750
state, before attempting to send data. When disconnected, this respects
751-
the reconnect backoff time. When connecting, returns 0 to allow
752-
non-blocking connect to finish. When connected, returns a very large
753-
number to handle slow/stalled connections.
751+
the reconnect backoff time. When connecting or connected, returns a very
752+
large number to handle slow/stalled connections.
754753
"""
755754
time_waited = time.time() - (self.last_attempt or 0)
756755
if self.state is ConnectionStates.DISCONNECTED:
757756
return max(self._reconnect_backoff - time_waited, 0) * 1000
758-
elif self.connecting():
759-
return 0
760757
else:
758+
# When connecting or connected, we should be able to delay
759+
# indefinitely since other events (connection or data acked) will
760+
# cause a wakeup once data can be sent.
761761
return float('inf')
762762

763763
def connected(self):

kafka/producer/sender.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def run_once(self):
157157
# difference between now and its linger expiry time; otherwise the
158158
# select time will be the time difference between now and the
159159
# metadata expiry time
160-
self._client.poll(poll_timeout_ms)
160+
self._client.poll(timeout_ms=poll_timeout_ms)
161161

162162
def initiate_close(self):
163163
"""Start closing the sender (won't complete until all data is sent)."""

test/test_client_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_conn_state_change(mocker, cli, conn):
9494
sock = conn._sock
9595
cli._conn_state_change(node_id, sock, conn)
9696
assert node_id in cli._connecting
97-
sel.register.assert_called_with(sock, selectors.EVENT_WRITE)
97+
sel.register.assert_called_with(sock, selectors.EVENT_WRITE, conn)
9898

9999
conn.state = ConnectionStates.CONNECTED
100100
cli._conn_state_change(node_id, sock, conn)

test/test_conn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_connection_delay(conn):
8585
conn.last_attempt = 1000
8686
assert conn.connection_delay() == conn.config['reconnect_backoff_ms']
8787
conn.state = ConnectionStates.CONNECTING
88-
assert conn.connection_delay() == 0
88+
assert conn.connection_delay() == float('inf')
8989
conn.state = ConnectionStates.CONNECTED
9090
assert conn.connection_delay() == float('inf')
9191

0 commit comments

Comments
 (0)