Module: Karafka::Pro::Routing::Features::ConsumerGroups::DirectAssignments::Topic

Defined in:
lib/karafka/pro/routing/features/consumer_groups/direct_assignments/topic.rb

Overview

Topic extensions for direct assignments

Instance Method Summary collapse

Instance Method Details

#direct_assignments(*partitions_or_all) ⇒ Object Also known as: assign

Allows for direct assignment of

Examples:

Assign all available partitions

direct_assignments(true)

Assign partitions 2, 3 and 5

direct_assignments(2, 3, 5)

Assign partitions from 0 to 3

direct_assignments(0..3)

Parameters:

  • partitions_or_all (true, Array<Integer>)

    informs Karafka that we want to use direct assignments instead of automatic for this topic. It also allows us to specify which partitions we’re interested in or ‘true` if in all



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/karafka/pro/routing/features/consumer_groups/direct_assignments/topic.rb', line 61

def direct_assignments(*partitions_or_all)
  @direct_assignments ||= if partitions_or_all == [true]
    Config.new(
      active: true,
      partitions: true
    )
  elsif partitions_or_all.size == 1 && partitions_or_all.first.is_a?(Range)
    partitions_or_all = partitions_or_all.first.to_a

    Config.new(
      active: true,
      partitions: partitions_or_all.to_h { |partition| [partition, true] }
    )
  else
    Config.new(
      active: !partitions_or_all.empty?,
      partitions: partitions_or_all.to_h { |partition| [partition, true] }
    )
  end
end

#initializeObject

This method sets up the extra instance variable to nil before calling the parent class initializer. The explicit initialization to nil is included as an optimization for Ruby’s object shapes system, which improves memory layout and access performance.



43
44
45
46
# File 'lib/karafka/pro/routing/features/consumer_groups/direct_assignments/topic.rb', line 43

def initialize(...)
  @direct_assignments = nil
  super
end

#to_hHash

Returns topic with all its native configuration options plus direct assignments.

Returns:

  • (Hash)

    topic with all its native configuration options plus direct assignments



86
87
88
89
90
# File 'lib/karafka/pro/routing/features/consumer_groups/direct_assignments/topic.rb', line 86

def to_h
  super.merge(
    direct_assignments: direct_assignments.to_h
  ).freeze
end