Class: Kafka::BrokerPool

Inherits:
Object
  • Object
show all
Defined in:
lib/kafka/broker_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection_builder:, logger:) ⇒ BrokerPool

Returns a new instance of BrokerPool.



7
8
9
10
11
# File 'lib/kafka/broker_pool.rb', line 7

def initialize(connection_builder:, logger:)
  @logger = TaggedLogger.new(logger)
  @connection_builder = connection_builder
  @brokers = {}
end

Instance Method Details

#closeObject



34
35
36
37
38
39
# File 'lib/kafka/broker_pool.rb', line 34

def close
  @brokers.each do |id, broker|
    @logger.info "Disconnecting broker #{id}"
    broker.disconnect
  end
end

#connect(host, port, node_id: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kafka/broker_pool.rb', line 13

def connect(host, port, node_id: nil)
  if @brokers.key?(node_id)
    broker = @brokers.fetch(node_id)
    return broker if broker.address_match?(host, port)
    broker.disconnect
    @brokers[node_id] = nil
  end

  broker = Broker.new(
    connection_builder: @connection_builder,
    host: host,
    port: port,
    node_id: node_id,
    logger: @logger,
  )

  @brokers[node_id] = broker unless node_id.nil?

  broker
end