Sha256: b8e863fba8a9b28a17b44b2724ca93e9234ffe3ce5ca32a98b06504ca8334e24

Contents?: true

Size: 840 Bytes

Versions: 7

Compression:

Stored size: 840 Bytes

Contents

# frozen_string_literal: true

export_default :Throttler

# Implements general-purpose throttling
class Throttler
  def initialize(rate)
    @rate = rate_from_argument(rate)
    @min_dt = 1.0 / @rate
    @last_iteration_clock = clock - @min_dt
  end

  def clock
    ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  end

  def call(&block)
    now = clock
    dt = now - @last_iteration_clock

    sleep(@min_dt - dt) if dt < @min_dt

    @last_iteration_clock = dt > @min_dt ? now : @last_iteration_clock + @min_dt
    block.call(self)
  end

  alias_method :process, :call

  private

  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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
polyphony-0.26 lib/polyphony/core/throttler.rb
polyphony-0.25 lib/polyphony/core/throttler.rb
polyphony-0.24 lib/polyphony/core/throttler.rb
polyphony-0.23 lib/polyphony/core/throttler.rb
polyphony-0.22 lib/polyphony/core/throttler.rb
polyphony-0.21 lib/polyphony/core/throttler.rb
polyphony-0.20 lib/polyphony/core/throttler.rb