Module: Karafka::Pro::Routing::Features::Patterns::ConsumerGroup

Defined in:
lib/karafka/pro/routing/features/patterns/consumer_group.rb

Overview

Expansion of the consumer groups routing component to work with patterns

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object

Parameters:

  • args (Object)

    whatever consumer group accepts



22
23
24
25
# File 'lib/karafka/pro/routing/features/patterns/consumer_group.rb', line 22

def initialize(*args)
  super
  @patterns = Patterns.new([])
end

#pattern=(regexp_or_name, regexp = nil, &block) ⇒ Object

Creates the pattern for topic matching with appropriate virtual topic

Parameters:

  • regexp_or_name (Symbol, String, Regexp)

    name of the pattern or regexp for automatic-based named patterns

  • regexp (Regexp, nil) (defaults to: nil)

    nil if we use auto-generated name based on the regexp or the regexp if we used named patterns

  • block (Proc)

    appropriate underlying topic settings



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/karafka/pro/routing/features/patterns/consumer_group.rb', line 38

def pattern=(regexp_or_name, regexp = nil, &block)
  # This code allows us to have a nice nameless (automatic-named) patterns that do not
  # have to be explicitly named. However if someone wants to use names for exclusions
  # it can be done by providing both
  if regexp_or_name.is_a?(Regexp)
    name = nil
    regexp = regexp_or_name
  else
    name = regexp_or_name
  end

  pattern = Pattern.new(name, regexp, block)
  virtual_topic = public_send(:topic=, pattern.name, &block)
  # Indicate the nature of this topic (matcher)
  virtual_topic.patterns(active: true, type: :matcher, pattern: pattern)
  pattern.topic = virtual_topic
  @patterns << pattern
end

#patterns::Karafka::Pro::Routing::Features::Patterns::Patterns

Returns created patterns.



28
29
30
# File 'lib/karafka/pro/routing/features/patterns/consumer_group.rb', line 28

def patterns
  @patterns
end

#to_hHash

Returns consumer group with patterns injected.

Returns:

  • (Hash)

    consumer group with patterns injected



58
59
60
61
62
# File 'lib/karafka/pro/routing/features/patterns/consumer_group.rb', line 58

def to_h
  super.merge(
    patterns: patterns.map(&:to_h)
  ).freeze
end