module OandaAPI::Throttling
Everything related to throttling the rate of new connections.
Public Instance Methods
included(base)
click to toggle source
# File lib/oanda_api/throttling/throttling.rb, line 12 def included(base) base.send(:extend, ClassMethods) end
last_new_connection_at()
click to toggle source
Time that the last connection was created. @return [Time]
# 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)
click to toggle source
Set the time the last new connection was created @param value [Time] @return [Time]
# 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)
click to toggle source
Original (unmonkey-patched) '.new' method of the including class @param klass [Class] the class that has included this module @return [UnboundMethod]
# 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)
click to toggle source
Restores the original '.new' method of the including class @param klass [Class] the class that has included this module @return [void]
# 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)
click to toggle source
Alias to `Throttling.original_new_method`
# File lib/oanda_api/throttling/throttling.rb, line 37 def save_original_new_method(klass) original_new_method klass end
throttle_connection_rate()
click to toggle source
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.
@return [void]
# 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
Private Instance Methods
_throttle(delta, time)
click to toggle source
@private
# File lib/oanda_api/throttling/throttling.rb, line 98 def _throttle(delta, time) sleep OandaAPI.configuration.min_new_connection_interval - delta end