Class: Rapidity::Share::Limit
- Inherits:
-
Object
- Object
- Rapidity::Share::Limit
- Defined in:
- lib/rapidity/share/limit.rb
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#last_used ⇒ Object
readonly
Returns the value of attribute last_used.
-
#max_queue ⇒ Object
readonly
Returns the value of attribute max_queue.
-
#max_tokens ⇒ Object
readonly
Returns the value of attribute max_tokens.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rate ⇒ Object
readonly
Returns the value of attribute rate.
-
#semaphore ⇒ Object
readonly
Returns the value of attribute semaphore.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
- #base_params ⇒ Object
-
#initialize(name, max_tokens, interval, namespace: nil, tokens: nil, last_used: nil, rate: nil, semaphore: nil, max_queue: nil, validate: true, **kwargs) ⇒ Limit
constructor
A new instance of Limit.
- #persisted? ⇒ Boolean
- #valid? ⇒ Boolean
Constructor Details
#initialize(name, max_tokens, interval, namespace: nil, tokens: nil, last_used: nil, rate: nil, semaphore: nil, max_queue: nil, validate: true, **kwargs) ⇒ Limit
Returns a new instance of Limit.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rapidity/share/limit.rb', line 7 def initialize(name, max_tokens, interval, namespace: nil, tokens: nil, last_used: nil, rate: nil, semaphore: nil, max_queue: nil, validate: true, **kwargs) @name = namespace.to_s.empty? ? name : [namespace, name].join(':') @namespace = namespace.to_s @max_tokens = max_tokens.to_i @tokens = tokens.to_i @interval = interval.to_i @semaphore = semaphore.to_i @max_queue = max_queue.to_i @last_used = last_used.to_i @rate = rate.to_i == 0 ? @max_tokens.to_f/@interval.to_f : rate @kwargs = kwargs validate_parameters! if validate end |
Instance Attribute Details
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
5 6 7 |
# File 'lib/rapidity/share/limit.rb', line 5 def interval @interval end |
#last_used ⇒ Object (readonly)
Returns the value of attribute last_used.
5 6 7 |
# File 'lib/rapidity/share/limit.rb', line 5 def last_used @last_used end |
#max_queue ⇒ Object (readonly)
Returns the value of attribute max_queue.
5 6 7 |
# File 'lib/rapidity/share/limit.rb', line 5 def max_queue @max_queue end |
#max_tokens ⇒ Object (readonly)
Returns the value of attribute max_tokens.
5 6 7 |
# File 'lib/rapidity/share/limit.rb', line 5 def max_tokens @max_tokens end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/rapidity/share/limit.rb', line 5 def name @name end |
#rate ⇒ Object (readonly)
Returns the value of attribute rate.
5 6 7 |
# File 'lib/rapidity/share/limit.rb', line 5 def rate @rate end |
#semaphore ⇒ Object (readonly)
Returns the value of attribute semaphore.
5 6 7 |
# File 'lib/rapidity/share/limit.rb', line 5 def semaphore @semaphore end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
5 6 7 |
# File 'lib/rapidity/share/limit.rb', line 5 def tokens @tokens end |
Class Method Details
.from_hash(name, **kwargs) ⇒ Object
26 27 28 29 30 |
# File 'lib/rapidity/share/limit.rb', line 26 def self.from_hash(name, **kwargs) max_tokens = kwargs.delete(:max_tokens) interval = kwargs.delete(:interval) self.new(name, max_tokens, interval, **kwargs) end |
Instance Method Details
#base_params ⇒ Object
40 41 42 |
# File 'lib/rapidity/share/limit.rb', line 40 def base_params [max_tokens, interval, max_queue] end |
#persisted? ⇒ Boolean
32 33 34 |
# File 'lib/rapidity/share/limit.rb', line 32 def persisted? @last_used > 0 end |
#valid? ⇒ Boolean
36 37 38 |
# File 'lib/rapidity/share/limit.rb', line 36 def valid? @max_tokens > 0 && @interval > 0 end |