Skip to content

conn: Return non-zero delay when in CONNECTING state #1908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions kafka/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ class BrokerConnection(object):
server-side log entries that correspond to this client. Also
submitted to GroupCoordinator for logging with respect to
consumer group administration. Default: 'kafka-python-{version}'
connecting_delay_ms (int): Time to wait before sending data when a
connection is in state CONNECTING. Small but non-zero timeout is
recommended to avoid busy loop as well as to avoid unnecessary
delays while connecting. Default: 100
reconnect_backoff_ms (int): The amount of time in milliseconds to
wait before attempting to reconnect to a given host.
Default: 50.
Expand Down Expand Up @@ -192,6 +196,7 @@ class BrokerConnection(object):

DEFAULT_CONFIG = {
'client_id': 'kafka-python-' + __version__,
'connecting_delay_ms': 100,
'node_id': 0,
'request_timeout_ms': 30000,
'reconnect_backoff_ms': 50,
Expand Down Expand Up @@ -769,15 +774,16 @@ def connection_delay(self):
"""
Return the number of milliseconds to wait, based on the connection
state, before attempting to send data. When disconnected, this respects
the reconnect backoff time. When connecting, returns 0 to allow
non-blocking connect to finish. When connected, returns a very large
number to handle slow/stalled connections.
the reconnect backoff time. When connecting, returns value defined by
connecting_delay_ms config value -- typically something less than a
second -- to allow non-blocking connect to finish. When connected,
returns a very large number to handle slow/stalled connections.
"""
time_waited = time.time() - (self.last_attempt or 0)
if self.state is ConnectionStates.DISCONNECTED:
return max(self._reconnect_backoff - time_waited, 0) * 1000
elif self.connecting():
return 0
return self.config['connecting_delay_ms']
else:
return float('inf')

Expand Down
2 changes: 1 addition & 1 deletion test/test_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_connection_delay(conn):
conn.last_attempt = 1000
assert conn.connection_delay() == conn.config['reconnect_backoff_ms']
conn.state = ConnectionStates.CONNECTING
assert conn.connection_delay() == 0
assert conn.connection_delay() == 100
conn.state = ConnectionStates.CONNECTED
assert conn.connection_delay() == float('inf')

Expand Down