class ElasticAPM::Util::Throttle

Usage example:

Throttle.new(5) { thing to only do once per 5 secs }

Public Class Methods

new(buffer_secs, &block) click to toggle source
# File lib/elastic_apm/util/throttle.rb, line 10
def initialize(buffer_secs, &block)
  @buffer_secs = buffer_secs
  @block = block
end

Public Instance Methods

call() click to toggle source
# File lib/elastic_apm/util/throttle.rb, line 15
def call
  if @last_call && seconds_since_last_call < @buffer_secs
    return @last_result
  end

  @last_call = now
  @last_result = @block.call
end

Private Instance Methods

now() click to toggle source
# File lib/elastic_apm/util/throttle.rb, line 26
def now
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
seconds_since_last_call() click to toggle source
# File lib/elastic_apm/util/throttle.rb, line 30
def seconds_since_last_call
  now - @last_call
end