Sha256: fa17e40e01a5e979cb0f6f6f847c38b1e686f59722cec5e791bb68f8bd3f3c6d

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Polyphony
  # Implements general-purpose throttling
  class Throttler

    # Initializes a throttler instance with the given rate.
    #
    # @param rate [Number] throttler rate in times per second
    def initialize(rate)
      @rate = rate_from_argument(rate)
      @min_dt = 1.0 / @rate
      @next_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
    end

    # Invokes the throttler with the given block. The throttler will
    # automatically introduce a delay to keep to the maximum specified rate.
    # The throttler instance is passed to the given block.
    #
    # @return [any] given block's return value
    def call
      now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
      delta = @next_time - now
      Polyphony.backend_sleep(delta) if delta > 0
      result = yield self

      while true
        @next_time += @min_dt
        break if @next_time > now
      end

      result
    end
    alias_method :process, :call

    private

    # Converts the given argument to a rate. If a hash is given, the throttler's
    # rate is computed from the value of either the `:interval` or `:rate` keys.
    #
    # @param arg [Number, Hash] rate argument
    # @return [Number] rate in times per second
    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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
polyphony-1.1.1 lib/polyphony/core/throttler.rb
polyphony-1.1 lib/polyphony/core/throttler.rb
polyphony-1.0.2 lib/polyphony/core/throttler.rb
polyphony-1.0.1 lib/polyphony/core/throttler.rb
polyphony-1.0 lib/polyphony/core/throttler.rb
polyphony-0.99.6 lib/polyphony/core/throttler.rb
polyphony-0.99.5 lib/polyphony/core/throttler.rb