Module: Karafka::Pro::Routing::Features::Throttling::Topic

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

Overview

Topic throttling API extensions

Instance Method Summary collapse

Instance Method Details

#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.



17
18
19
20
# File 'lib/karafka/pro/routing/features/throttling/topic.rb', line 17

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

#throttle(**args) ⇒ Object

Just an alias for nice API

Parameters:

  • args (Hash)

    Anything #throttling accepts

Options Hash (**args):

  • :limit (Integer)

    max messages to process in a time interval

  • :interval (Integer)

    time interval for processing in milliseconds



52
53
54
# File 'lib/karafka/pro/routing/features/throttling/topic.rb', line 52

def throttle(**args)
  throttling(**args)
end

#throttling(limit: Float::INFINITY, interval: 60_000) ⇒ Object

Parameters:

  • limit (Integer) (defaults to: Float::INFINITY)

    max messages to process in an time interval

  • interval (Integer) (defaults to: 60_000)

    time interval for processing



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/karafka/pro/routing/features/throttling/topic.rb', line 24

def throttling(
  limit: Float::INFINITY,
  interval: 60_000
)
  # Those settings are used for validation
  @throttling ||= begin
    config = Config.new(
      active: limit != Float::INFINITY,
      limit: limit,
      interval: interval
    )

    # If someone defined throttling setup, we need to create appropriate filter for it
    # and inject it via filtering feature
    if config.active?
      factory = ->(*) { Pro::Processing::Filters::Throttler.new(limit, interval) }
      filter(factory)
    end

    config
  end
end

#throttling?Boolean

Returns is a given job throttled.

Returns:

  • (Boolean)

    is a given job throttled



57
58
59
# File 'lib/karafka/pro/routing/features/throttling/topic.rb', line 57

def throttling?
  throttling.active?
end

#to_hHash

Returns topic with all its native configuration options plus throttling.

Returns:

  • (Hash)

    topic with all its native configuration options plus throttling



62
63
64
65
66
# File 'lib/karafka/pro/routing/features/throttling/topic.rb', line 62

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