HEX
Server: Apache
System: Linux sg241.singhost.net 2.6.32-896.16.1.lve1.4.51.el6.x86_64 #1 SMP Wed Jan 17 13:19:23 EST 2018 x86_64
User: honghock (909)
PHP: 8.0.30
Disabled: passthru,system,shell_exec,show_source,exec,popen,proc_open
Upload Files
File: //usr/lib/ruby/site_ruby/1.8/puppet/util/log/rate_limited_logger.rb
require 'puppet/util/logging'

# Logging utility class that limits the frequency of identical log messages
class Puppet::Util::Log::RateLimitedLogger
  include Puppet::Util::Logging

  def initialize(interval)
    raise ArgumentError, "Logging rate-limit interval must be an integer" unless interval.is_a?(Integer)
    @interval = interval
    @log_record = {}
  end

  # Override the logging entry point to rate-limit it
  def send_log(level, message)
    Puppet::Util::Log.create({:level => level, :message => message}) if should_log?(level, message)
  end

  private

  def should_log?(level, message)
    # Initialize separate records for different levels, and only when needed
    record = (@log_record[level] ||= {})
    last_log = record[message]

    # Skip logging if the time interval since the last logging hasn't elapsed yet
    return false if last_log and within_interval?(last_log)

    # Purge stale entries; do this after the interval check to reduce passes through the cache
    record.delete_if { |key, time| !within_interval?(time) }

    # Reset the beginning of the interval to the current time
    record[message] = Time.now

    true
  end

  def within_interval?(time)
    time + @interval > Time.now
  end
end