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

Defined in:
lib/karafka/pro/routing/features/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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/karafka/pro/routing/features/direct_assignments/topic.rb', line 52

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 calls the parent class initializer and then sets up the extra instance variable to nil. The explicit initialization to nil is included as an optimization for Ruby’s object shapes system, which improves memory layout and access performance.



34
35
36
37
# File 'lib/karafka/pro/routing/features/direct_assignments/topic.rb', line 34

def initialize(...)
  super
  @direct_assignments = nil
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



77
78
79
80
81
# File 'lib/karafka/pro/routing/features/direct_assignments/topic.rb', line 77

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