def __init__(self, **configs): self.config = copy.copy(self.DEFAULT_CONFIG) for key in self.config: if key in configs: self.config[key] = configs[key] # these properties need to be set on top of the initialization pipeline # because they are used when __del__ method is called self._closed = False self._wake_r, self._wake_w = socket.socketpair() self._selector = self.config["selector"]() self.cluster = ClusterMetadata(**self.config) self._topics = set() # empty set will fetch all topic metadata self._metadata_refresh_in_progress = False self._conns = Dict() # object to support weakrefs self._api_versions = None self._connecting = set() self._sending = set() self._refresh_on_disconnects = True self._last_bootstrap = 0 self._bootstrap_fails = 0 self._wake_r.setblocking(False) self._wake_w.settimeout(self.config["wakeup_timeout_ms"] / 1000.0) self._wake_lock = threading.Lock() self._lock = threading.RLock() # when requests complete, they are transferred to this queue prior to # invocation. The purpose is to avoid invoking them while holding the # lock above. self._pending_completion = collections.deque() self._selector.register(self._wake_r, selectors.EVENT_READ) self._idle_expiry_manager = IdleConnectionManager( self.config["connections_max_idle_ms"]) self._sensors = None if self.config["metrics"]: self._sensors = KafkaClientMetrics( self.config["metrics"], self.config["metric_group_prefix"], weakref.proxy(self._conns), ) self._num_bootstrap_hosts = len( collect_hosts(self.config["bootstrap_servers"])) # Check Broker Version if not set explicitly if self.config["api_version"] is None: check_timeout = self.config["api_version_auto_timeout_ms"] / 1000 self.config["api_version"] = self.check_version( timeout=check_timeout)
def __init__(self, **configs): self.config = copy.copy(self.DEFAULT_CONFIG) for key in self.config: if key in configs: self.config[key] = configs[key] if self.config['api_version'] is not None: assert self.config['api_version'] in self.API_VERSIONS, ( 'api_version [{0}] must be one of: {1}'.format( self.config['api_version'], str(self.API_VERSIONS))) self.cluster = ClusterMetadata(**self.config) self._topics = set() # empty set will fetch all topic metadata self._metadata_refresh_in_progress = False self._selector = self.config['selector']() self._conns = Dict() # object to support weakrefs self._connecting = set() self._refresh_on_disconnects = True self._last_bootstrap = 0 self._bootstrap_fails = 0 self._wake_r, self._wake_w = socket.socketpair() self._wake_r.setblocking(False) self._wake_lock = threading.Lock() self._lock = threading.RLock() # when requests complete, they are transferred to this queue prior to # invocation. The purpose is to avoid invoking them while holding the # lock above. self._pending_completion = collections.deque() self._selector.register(self._wake_r, selectors.EVENT_READ) self._idle_expiry_manager = IdleConnectionManager( self.config['connections_max_idle_ms']) self._closed = False self._sensors = None if self.config['metrics']: self._sensors = KafkaClientMetrics( self.config['metrics'], self.config['metric_group_prefix'], weakref.proxy(self._conns)) self._bootstrap(collect_hosts(self.config['bootstrap_servers'])) # Check Broker Version if not set explicitly if self.config['api_version'] is None: check_timeout = self.config['api_version_auto_timeout_ms'] / 1000 self.config['api_version'] = self.check_version( timeout=check_timeout)