Class: Hatchet::RateLimit

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet/rate_limit.rb

Overview

Defines a rate limit for a task

Examples:

Static rate limit

Hatchet::RateLimit.new(static_key: "api-calls", units: 1)

Dynamic rate limit per user

Hatchet::RateLimit.new(
  dynamic_key: "input.user_id",
  units: 1,
  limit: 10,
  duration: :minute
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(static_key: nil, dynamic_key: nil, units: 1, limit: nil, duration: nil) ⇒ RateLimit

Returns a new instance of RateLimit.

Parameters:

  • static_key (String, nil) (defaults to: nil)

    Static rate limit key

  • dynamic_key (String, nil) (defaults to: nil)

    Dynamic rate limit key expression

  • units (Integer) (defaults to: 1)

    Units consumed per execution

  • limit (Integer, nil) (defaults to: nil)

    Max units in the duration window

  • duration (Symbol, nil) (defaults to: nil)

    Duration window (:second, :minute, :hour, etc.)

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
# File 'lib/hatchet/rate_limit.rb', line 48

def initialize(static_key: nil, dynamic_key: nil, units: 1, limit: nil, duration: nil)
  raise ArgumentError, "Must specify either static_key or dynamic_key" if static_key.nil? && dynamic_key.nil?
  raise ArgumentError, "Cannot specify both static_key and dynamic_key" if static_key && dynamic_key

  @static_key = static_key
  @dynamic_key = dynamic_key
  @units = units
  @limit = limit
  @duration = duration
end

Instance Attribute Details

#durationSymbol? (readonly)

Returns Duration window for the rate limit.

Returns:

  • (Symbol, nil)

    Duration window for the rate limit



41
42
43
# File 'lib/hatchet/rate_limit.rb', line 41

def duration
  @duration
end

#dynamic_keyString? (readonly)

Returns Dynamic rate limit key (CEL expression).

Returns:

  • (String, nil)

    Dynamic rate limit key (CEL expression)



32
33
34
# File 'lib/hatchet/rate_limit.rb', line 32

def dynamic_key
  @dynamic_key
end

#limitInteger? (readonly)

Returns Maximum number of units allowed in the duration.

Returns:

  • (Integer, nil)

    Maximum number of units allowed in the duration



38
39
40
# File 'lib/hatchet/rate_limit.rb', line 38

def limit
  @limit
end

#static_keyString? (readonly)

Returns Static rate limit key.

Returns:

  • (String, nil)

    Static rate limit key



29
30
31
# File 'lib/hatchet/rate_limit.rb', line 29

def static_key
  @static_key
end

#unitsInteger (readonly)

Returns Number of units consumed per execution.

Returns:

  • (Integer)

    Number of units consumed per execution



35
36
37
# File 'lib/hatchet/rate_limit.rb', line 35

def units
  @units
end

Instance Method Details

#to_hHash

Returns:

  • (Hash)


60
61
62
63
64
65
66
67
# File 'lib/hatchet/rate_limit.rb', line 60

def to_h
  h = { units: @units }
  h[:static_key] = @static_key if @static_key
  h[:dynamic_key] = @dynamic_key if @dynamic_key
  h[:limit] = @limit if @limit
  h[:duration] = @duration.to_s.upcase if @duration
  h
end