Class: Hatchet::RateLimit
- Inherits:
-
Object
- Object
- Hatchet::RateLimit
- Defined in:
- lib/hatchet/rate_limit.rb
Overview
Defines a rate limit for a task
Instance Attribute Summary collapse
-
#duration ⇒ Symbol?
readonly
Duration window for the rate limit.
-
#dynamic_key ⇒ String?
readonly
Dynamic rate limit key (CEL expression).
-
#limit ⇒ Integer?
readonly
Maximum number of units allowed in the duration.
-
#static_key ⇒ String?
readonly
Static rate limit key.
-
#units ⇒ Integer
readonly
Number of units consumed per execution.
Instance Method Summary collapse
-
#initialize(static_key: nil, dynamic_key: nil, units: 1, limit: nil, duration: nil) ⇒ RateLimit
constructor
A new instance of RateLimit.
- #to_h ⇒ Hash
Constructor Details
#initialize(static_key: nil, dynamic_key: nil, units: 1, limit: nil, duration: nil) ⇒ RateLimit
Returns a new instance of RateLimit.
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
#duration ⇒ Symbol? (readonly)
Returns Duration window for the rate limit.
41 42 43 |
# File 'lib/hatchet/rate_limit.rb', line 41 def duration @duration end |
#dynamic_key ⇒ String? (readonly)
Returns Dynamic rate limit key (CEL expression).
32 33 34 |
# File 'lib/hatchet/rate_limit.rb', line 32 def dynamic_key @dynamic_key end |
#limit ⇒ Integer? (readonly)
Returns Maximum number of units allowed in the duration.
38 39 40 |
# File 'lib/hatchet/rate_limit.rb', line 38 def limit @limit end |
#static_key ⇒ String? (readonly)
Returns Static rate limit key.
29 30 31 |
# File 'lib/hatchet/rate_limit.rb', line 29 def static_key @static_key end |
#units ⇒ Integer (readonly)
Returns 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_h ⇒ 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 |