Method: Rdkafka::Producer#partition_count

Defined in:
lib/rdkafka/producer.rb

#partition_count(topic) ⇒ Integer

Note:

If 'allow.auto.create.topics' is set to true in the broker, the topic will be auto-created after returning nil.

Note:

We cache the partition count for a given topic for given time. This prevents us in case someone uses partition_key from querying for the count with each message. Instead we query once every 30 seconds at most if we have a valid partition count or every 5 seconds in case we were not able to obtain number of partitions

Partition count for a given topic.

Parameters:

  • topic (String)

    The topic name.

Returns:

  • (Integer)

    partition count for a given topic or -1 if it could not be obtained.

[View source] [View on GitHub]

229
230
231
232
233
234
235
236
237
# File 'lib/rdkafka/producer.rb', line 229

def partition_count(topic)
  closed_producer_check(__method__)

  @_partitions_count_cache.delete_if do |_, cached|
    monotonic_now - cached.first > PARTITIONS_COUNT_TTL
  end

  @_partitions_count_cache[topic].last
end