Sha256: c494a453ef0a65ad9034cde19847b48af78dc0d09e6b188bc72094cd0640b111
Contents?: true
Size: 918 Bytes
Versions: 7
Compression:
Stored size: 918 Bytes
Contents
# frozen_string_literal: true module Polyphony # Implements general-purpose throttling class Throttler def initialize(rate) @rate = rate_from_argument(rate) @min_dt = 1.0 / @rate @next_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) end def call now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) delta = @next_time - now Thread.current.backend.sleep(delta) if delta > 0 yield self while true @next_time += @min_dt break if @next_time > now end end alias_method :process, :call def stop @stop = true end 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 end
Version data entries
7 entries across 7 versions & 1 rubygems