class Polyphony::Throttler

Implements general-purpose throttling

Public Class Methods

new(rate) click to toggle source
# File lib/polyphony/core/throttler.rb, line 6
def initialize(rate)
  @rate = rate_from_argument(rate)
  @min_dt = 1.0 / @rate
  @next_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end

Public Instance Methods

call() { |self| ... } click to toggle source
# File lib/polyphony/core/throttler.rb, line 12
def call
  now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  delta = @next_time - now
  Polyphony.backend_sleep(delta) if delta > 0
  yield self

  while true
    @next_time += @min_dt
    break if @next_time > now
  end
end
Also aliased as: process
process()
Alias for: call
stop() click to toggle source
# File lib/polyphony/core/throttler.rb, line 25
def stop
  @stop = true
end

Private Instance Methods

rate_from_argument(arg) click to toggle source
# File lib/polyphony/core/throttler.rb, line 31
def rate_from_argument(arg)
  return arg if arg.is_a?(Numeric)

  if arg.is_a?(Hash)
    return 1.0 / arg[:interval] if arg[:interval]
    return arg[:rate] if arg[:rate]
  end
  raise "Invalid rate argument #{arg.inspect}"
end