Skip to content

Commit 7a7a890

Browse files
Wayde2014dpkp
authored andcommitted
Added a function to determine if bootstrap is successfully connected (#1876)
1 parent 298cb0d commit 7a7a890

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

kafka/consumer/group.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,12 @@ def __init__(self, *topics, **configs):
391391
self._subscription.subscribe(topics=topics)
392392
self._client.set_topics(topics)
393393

394+
def bootstrap_connected(self):
395+
"""Return True if the bootstrap is connected."""
396+
if self._client._bootstrap_fails > 0:
397+
return False
398+
return True
399+
394400
def assign(self, partitions):
395401
"""Manually assign a list of TopicPartitions to this consumer.
396402

kafka/producer/kafka.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from kafka.serializer import Serializer
2424
from kafka.structs import TopicPartition
2525

26-
2726
log = logging.getLogger(__name__)
2827
PRODUCER_CLIENT_ID_SEQUENCE = AtomicInteger()
2928

@@ -376,13 +375,13 @@ def __init__(self, **configs):
376375
reporters = [reporter() for reporter in self.config['metric_reporters']]
377376
self._metrics = Metrics(metric_config, reporters)
378377

379-
client = KafkaClient(metrics=self._metrics, metric_group_prefix='producer',
380-
wakeup_timeout_ms=self.config['max_block_ms'],
381-
**self.config)
378+
self._client = KafkaClient(metrics=self._metrics, metric_group_prefix='producer',
379+
wakeup_timeout_ms=self.config['max_block_ms'],
380+
**self.config)
382381

383382
# Get auto-discovered version from client if necessary
384383
if self.config['api_version'] is None:
385-
self.config['api_version'] = client.config['api_version']
384+
self.config['api_version'] = self._client.config['api_version']
386385

387386
if self.config['compression_type'] == 'lz4':
388387
assert self.config['api_version'] >= (0, 8, 2), 'LZ4 Requires >= Kafka 0.8.2 Brokers'
@@ -398,9 +397,9 @@ def __init__(self, **configs):
398397

399398
message_version = self._max_usable_produce_magic()
400399
self._accumulator = RecordAccumulator(message_version=message_version, metrics=self._metrics, **self.config)
401-
self._metadata = client.cluster
400+
self._metadata = self._client.cluster
402401
guarantee_message_order = bool(self.config['max_in_flight_requests_per_connection'] == 1)
403-
self._sender = Sender(client, self._metadata,
402+
self._sender = Sender(self._client, self._metadata,
404403
self._accumulator, self._metrics,
405404
guarantee_message_order=guarantee_message_order,
406405
**self.config)
@@ -412,14 +411,22 @@ def __init__(self, **configs):
412411
atexit.register(self._cleanup)
413412
log.debug("Kafka producer started")
414413

414+
def bootstrap_connected(self):
415+
"""Return True if the bootstrap is connected."""
416+
if self._client._bootstrap_fails > 0:
417+
return False
418+
return True
419+
415420
def _cleanup_factory(self):
416421
"""Build a cleanup clojure that doesn't increase our ref count"""
417422
_self = weakref.proxy(self)
423+
418424
def wrapper():
419425
try:
420426
_self.close(timeout=0)
421427
except (ReferenceError, AttributeError):
422428
pass
429+
423430
return wrapper
424431

425432
def _unregister_cleanup(self):

0 commit comments

Comments
 (0)