Class: RateLimit::Worker

Inherits:
Object
  • Object
show all
Includes:
Throttler
Defined in:
lib/rate_limit/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Throttler

#throttle

Constructor Details

#initialize(topic:, value:, raise_errors: false, only_failures: false) ⇒ Worker

Returns a new instance of Worker.



9
10
11
12
13
14
15
16
# File 'lib/rate_limit/worker.rb', line 9

def initialize(topic:, value:, raise_errors: false, only_failures: false)
  @topic         = topic.to_s
  @value         = value.to_s
  @windows       = Window.find_all(worker: self, topic: @topic)
  @result        = Result.new(topic: @topic, value: @value)
  @raise_errors = raise_errors
  @only_failures = only_failures
end

Instance Attribute Details

#exceeded_windowObject

Returns the value of attribute exceeded_window.



7
8
9
# File 'lib/rate_limit/worker.rb', line 7

def exceeded_window
  @exceeded_window
end

#limitsObject

Returns the value of attribute limits.



7
8
9
# File 'lib/rate_limit/worker.rb', line 7

def limits
  @limits
end

#only_failuresObject

Returns the value of attribute only_failures.



7
8
9
# File 'lib/rate_limit/worker.rb', line 7

def only_failures
  @only_failures
end

#raise_errorsObject

Returns the value of attribute raise_errors.



7
8
9
# File 'lib/rate_limit/worker.rb', line 7

def raise_errors
  @raise_errors
end

#resultObject

Returns the value of attribute result.



7
8
9
# File 'lib/rate_limit/worker.rb', line 7

def result
  @result
end

#topicObject

Returns the value of attribute topic.



7
8
9
# File 'lib/rate_limit/worker.rb', line 7

def topic
  @topic
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'lib/rate_limit/worker.rb', line 7

def value
  @value
end

#windowsObject

Returns the value of attribute windows.



7
8
9
# File 'lib/rate_limit/worker.rb', line 7

def windows
  @windows
end

Instance Method Details

#clear_cache_counterObject



22
23
24
# File 'lib/rate_limit/worker.rb', line 22

def clear_cache_counter
  Window.clear_cache_counter(windows)
end

#failure!Object



42
43
44
45
46
47
# File 'lib/rate_limit/worker.rb', line 42

def failure!
  result.failure!(self)
  RateLimit.config.failure_callback(result)

  raise Errors::LimitExceededError, result if raise_errors
end

#increment_cache_counterObject



18
19
20
# File 'lib/rate_limit/worker.rb', line 18

def increment_cache_counter
  Window.increment_cache_counter(windows)
end

#limit_exceeded?Boolean

Returns:

  • (Boolean)


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

def limit_exceeded?
  exceeded_window.present?
end

#reloaded_limit_exceeded?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/rate_limit/worker.rb', line 26

def reloaded_limit_exceeded?
  @exceeded_window = Window.find_exceeded(windows)

  limit_exceeded?
end

#success!(skip_increment_cache: false) ⇒ Object



36
37
38
39
40
# File 'lib/rate_limit/worker.rb', line 36

def success!(skip_increment_cache: false)
  increment_cache_counter unless skip_increment_cache
  result.success!
  RateLimit.config.success_callback(result)
end