Skip to content

Only refresh metadata if connection fails all dns records #2532

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

Merged
merged 1 commit into from
Mar 14, 2025
Merged
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
3 changes: 1 addition & 2 deletions kafka/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ def __init__(self, **configs):
self._api_versions = None
self._connecting = set()
self._sending = set()
self._refresh_on_disconnects = True

# Not currently used, but data is collected internally
self._last_bootstrap = 0
Expand Down Expand Up @@ -382,7 +381,7 @@ def _conn_state_change(self, node_id, sock, conn):
elif self.cluster.is_bootstrap(node_id):
self._bootstrap_fails += 1

elif self._refresh_on_disconnects and not self._closed and not idle_disconnect:
elif conn.connect_failed() and not self._closed and not idle_disconnect:
log.warning("Node %s connection failed -- refreshing metadata", node_id)
self.cluster.request_update()

Expand Down
4 changes: 4 additions & 0 deletions kafka/conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,10 @@ def disconnected(self):
"""Return True iff socket is closed"""
return self.state is ConnectionStates.DISCONNECTED

def connect_failed(self):
"""Return True iff connection attempt failed after attempting all dns records"""
return self.disconnected() and self.last_attempt >= 0 and len(self._gai) == 0

def _reset_reconnect_backoff(self):
self._failures = 0
self._reconnect_backoff = self.config['reconnect_backoff_ms'] / 1000.0
Expand Down
Loading