Module: OandaAPI::Throttling

Extended by:
Throttling
Included in:
Throttling
Defined in:
lib/oanda_api/throttling/throttling.rb

Overview

Everything related to throttling the rate of new connections.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#included(base) ⇒ Object



12
13
14
# File 'lib/oanda_api/throttling/throttling.rb', line 12

def included(base)
  base.send(:extend, ClassMethods)
end

#last_new_connection_atTime

Time that the last connection was created.

Returns:

  • (Time)


18
19
20
# File 'lib/oanda_api/throttling/throttling.rb', line 18

def last_new_connection_at
  @throttle_mutex.synchronize { @last_new_connection_at }
end

#last_new_connection_at=(value) ⇒ Time

Set the time the last new connection was created

Parameters:

  • value (Time)

Returns:

  • (Time)


25
26
27
# File 'lib/oanda_api/throttling/throttling.rb', line 25

def last_new_connection_at=(value)
  @throttle_mutex.synchronize { @last_new_connection_at = value }
end

#original_new_method(klass) ⇒ UnboundMethod

Original (unmonkey-patched) '.new' method of the including class

Parameters:

  • klass (Class)

    the class that has included this module

Returns:

  • (UnboundMethod)


32
33
34
# File 'lib/oanda_api/throttling/throttling.rb', line 32

def original_new_method(klass)
  @original_new_method ||= klass.method(:new).unbind
end

#restore_original_new_method(klass) ⇒ void

This method returns an undefined value.

Restores the original '.new' method of the including class

Parameters:

  • klass (Class)

    the class that has included this module



44
45
46
47
48
# File 'lib/oanda_api/throttling/throttling.rb', line 44

def restore_original_new_method(klass)
  klass.define_singleton_method :new do |*args, &block|
    Throttling.original_new_method(klass).bind(klass).call *args, &block
  end
end

#save_original_new_method(klass) ⇒ Object

Alias to Throttling.original_new_method



37
38
39
# File 'lib/oanda_api/throttling/throttling.rb', line 37

def save_original_new_method(klass)
  original_new_method klass
end

#throttle_connection_ratevoid

This method returns an undefined value.

Throttles the connection rate by sleeping for a duration if the interval bewteen consecutive connections is less than the allowed minimum. Only throttles when the API is configured to use_request_throttling.



55
56
57
58
59
60
61
# File 'lib/oanda_api/throttling/throttling.rb', line 55

def throttle_connection_rate
  now = Time.now
  delta = now - (last_new_connection_at || now)
  _throttle(delta, now) if delta < OandaAPI.configuration.min_new_connection_interval &&
                                   OandaAPI.configuration.use_request_throttling?
  self.last_new_connection_at = Time.now
end