Class: Rapidity::Share::Limit

Inherits:
Object
  • Object
show all
Defined in:
lib/rapidity/share/limit.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#intervalObject (readonly)

Returns the value of attribute interval.



5
6
7
# File 'lib/rapidity/share/limit.rb', line 5

def interval
  @interval
end

#last_usedObject (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_queueObject (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_tokensObject (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

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/rapidity/share/limit.rb', line 5

def name
  @name
end

#rateObject (readonly)

Returns the value of attribute rate.



5
6
7
# File 'lib/rapidity/share/limit.rb', line 5

def rate
  @rate
end

#semaphoreObject (readonly)

Returns the value of attribute semaphore.



5
6
7
# File 'lib/rapidity/share/limit.rb', line 5

def semaphore
  @semaphore
end

#tokensObject (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_paramsObject



40
41
42
# File 'lib/rapidity/share/limit.rb', line 40

def base_params
  [max_tokens, interval, max_queue]
end

#persisted?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rapidity/share/limit.rb', line 32

def persisted?
  @last_used > 0
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rapidity/share/limit.rb', line 36

def valid?
  @max_tokens > 0 && @interval > 0
end