2323from kafka .serializer import Serializer
2424from kafka .structs import TopicPartition
2525
26-
2726log = logging .getLogger (__name__ )
2827PRODUCER_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