Skip to content

Commit b12c01a

Browse files
authored
Merge pull request #644 from ConnectedHomes/refresh-metadata-on-connection-error
Refresh metadata on connection error when listing topics
2 parents 4d2cd7c + ce55698 commit b12c01a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/kafka/client.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,15 @@ def create_partitions_for(name, num_partitions: 1, timeout: 30)
618618
#
619619
# @return [Array<String>] the list of topic names.
620620
def topics
621-
@cluster.list_topics
621+
attempts = 0
622+
begin
623+
attempts += 1
624+
@cluster.list_topics
625+
rescue Kafka::ConnectionError
626+
@cluster.mark_as_stale!
627+
retry unless attempts > 1
628+
raise
629+
end
622630
end
623631

624632
# Lists all consumer groups in the cluster

lib/kafka/cluster.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ def fetch_cluster_info
397397
end
398398

399399
def random_broker
400+
refresh_metadata_if_necessary!
400401
node_id = cluster_info.brokers.sample.node_id
401402
connect_to_broker(node_id)
402403
end

0 commit comments

Comments
 (0)