Skip to content

Commit 05fefa1

Browse files
committed
conn: Return non-zero delay when in CONNECTING state
If connection is in state CONNECTING and there is data ready to be sent using value 0 for the delay results in busy loop because Sender calls select with the value returned from here and then just keeps on calling it again and again until connection completes or fails. In some cases this can take a long time and if the connection failed it will just be retried and the busy loop continues. This return value was changed from 999999999 to 0 in commit 16c13f9 and it doesn't look like it needs to be exactly zero for other logic to work correctly. Fixes #1907
1 parent 7a69952 commit 05fefa1

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

kafka/conn.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class BrokerConnection(object):
107107
server-side log entries that correspond to this client. Also
108108
submitted to GroupCoordinator for logging with respect to
109109
consumer group administration. Default: 'kafka-python-{version}'
110+
connecting_delay_ms (int): Time to wait before sending data when a
111+
connection is in state CONNECTING. Small but non-zero timeout is
112+
recommended to avoid busy loop as well as to avoid unnecessary
113+
delays while connecting. Default: 100
110114
reconnect_backoff_ms (int): The amount of time in milliseconds to
111115
wait before attempting to reconnect to a given host.
112116
Default: 50.
@@ -192,6 +196,7 @@ class BrokerConnection(object):
192196

193197
DEFAULT_CONFIG = {
194198
'client_id': 'kafka-python-' + __version__,
199+
'connecting_delay_ms': 100,
195200
'node_id': 0,
196201
'request_timeout_ms': 30000,
197202
'reconnect_backoff_ms': 50,
@@ -769,15 +774,16 @@ def connection_delay(self):
769774
"""
770775
Return the number of milliseconds to wait, based on the connection
771776
state, before attempting to send data. When disconnected, this respects
772-
the reconnect backoff time. When connecting, returns 0 to allow
773-
non-blocking connect to finish. When connected, returns a very large
774-
number to handle slow/stalled connections.
777+
the reconnect backoff time. When connecting, returns value defined by
778+
connecting_delay_ms config value -- typically something less than a
779+
second -- to allow non-blocking connect to finish. When connected,
780+
returns a very large number to handle slow/stalled connections.
775781
"""
776782
time_waited = time.time() - (self.last_attempt or 0)
777783
if self.state is ConnectionStates.DISCONNECTED:
778784
return max(self._reconnect_backoff - time_waited, 0) * 1000
779785
elif self.connecting():
780-
return 0
786+
return self.config['connecting_delay_ms']
781787
else:
782788
return float('inf')
783789

0 commit comments

Comments
 (0)