Class: Karafka::Pro::Routing::Features::Patterns::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/pro/routing/features/patterns/pattern.rb

Overview

Karafka topic pattern object It represents a topic that is not yet materialized and that contains a name that is a regexp and not a “real” value. Underneath we define a dynamic topic, that is not active, that can be a subject to normal flow validations, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, regexp, config) ⇒ Pattern

Returns a new instance of Pattern.

Parameters:

  • name (String, Symbol, nil)

    name or the regexp for building the topic name or nil if we want to make it based on the regexp content

  • regexp (Regexp)

    regular expression to match topics

  • config (Proc)

    config for topic bootstrap



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/karafka/pro/routing/features/patterns/pattern.rb', line 42

def initialize(name, regexp, config)
  @regexp = regexp
  # This name is also used as the underlying matcher topic name
  #
  # It can be used provided by the user in case user wants to use exclusions of topics
  # or we can generate it if irrelevant.
  #
  # We generate it based on the regexp so within the same consumer group they are
  # always unique (checked by topic validations)
  #
  # This will not prevent users from creating a different regexps matching the same
  # topic but this minimizes simple mistakes
  #
  # This sub-part of sh1 should be unique enough and short-enough to use it here
  digest = Digest::SHA1.hexdigest(safe_regexp.source)[8..16]
  @name = name ? name.to_s : "karafka-pattern-#{digest}"
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Config for real-topic configuration during injection



36
37
38
# File 'lib/karafka/pro/routing/features/patterns/pattern.rb', line 36

def config
  @config
end

#nameObject (readonly)

Each pattern has its own “topic” that we use as a routing reference that we define with non-existing topic for the routing to correctly pick it up for operations Virtual topic name for initial subscription



30
31
32
# File 'lib/karafka/pro/routing/features/patterns/pattern.rb', line 30

def name
  @name
end

#regexpObject

Pattern regexp



25
26
27
# File 'lib/karafka/pro/routing/features/patterns/pattern.rb', line 25

def regexp
  @regexp
end

#topicObject

Associated created virtual topic reference



33
34
35
# File 'lib/karafka/pro/routing/features/patterns/pattern.rb', line 33

def topic
  @topic
end

Instance Method Details

#regexp_stringString

Returns defined regexp representation as a string that is compatible with librdkafka expectations. We use it as a subscription name for initial patterns subscription start.

Returns:

  • (String)

    defined regexp representation as a string that is compatible with librdkafka expectations. We use it as a subscription name for initial patterns subscription start.



64
65
66
# File 'lib/karafka/pro/routing/features/patterns/pattern.rb', line 64

def regexp_string
  "^#{safe_regexp.source}"
end

#to_hHash

Returns hash representation of this routing pattern.

Returns:

  • (Hash)

    hash representation of this routing pattern



69
70
71
72
73
74
75
# File 'lib/karafka/pro/routing/features/patterns/pattern.rb', line 69

def to_h
  {
    regexp: regexp,
    name: name,
    regexp_string: regexp_string
  }.freeze
end