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.


5
6
7
8
9
# File 'lib/kafka/broker_pool.rb', line 5

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

Instance Method Details

#closeObject


25
26
27
28
29
30
# File 'lib/kafka/broker_pool.rb', line 25

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

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


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kafka/broker_pool.rb', line 11

def connect(host, port, node_id: nil)
  return @brokers.fetch(node_id) if @brokers.key?(node_id)

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

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

  broker
end