Skip to content

Commit 16c13f9

Browse files
committed
KafkaClient.connection_delay should return 0 when connecting to avoid unnecessary sleep in poll
1 parent b240457 commit 16c13f9

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

kafka/client_async.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,15 @@ def connection_delay(self, node_id):
231231
"""
232232
Returns the number of milliseconds to wait, based on the connection
233233
state, before attempting to send data. When disconnected, this respects
234-
the reconnect backoff time. When connecting or connected, this handles
235-
slow/stalled connections.
234+
the reconnect backoff time. When connecting, returns 0 to allow
235+
non-blocking connect to finish. When connected, returns a very large
236+
number to handle slow/stalled connections.
236237
237-
@param node_id The id of the node to check
238-
@return The number of milliseconds to wait.
238+
Arguments:
239+
node_id (int): The id of the node to check
240+
241+
Returns:
242+
int: The number of milliseconds to wait.
239243
"""
240244
if node_id not in self._conns:
241245
return 0
@@ -244,6 +248,8 @@ def connection_delay(self, node_id):
244248
time_waited_ms = time.time() - (conn.last_attempt or 0)
245249
if conn.state is ConnectionStates.DISCONNECTED:
246250
return max(self.config['reconnect_backoff_ms'] - time_waited_ms, 0)
251+
elif conn.state is ConnectionStates.CONNECTING:
252+
return 0
247253
else:
248254
return 999999999
249255

0 commit comments

Comments
 (0)