class Stackify::ErrorsGovernor

Public Class Methods

new(purge_period=5) click to toggle source
# File lib/stackify/errors_governor.rb, line 6
def initialize purge_period=5
  @history = {}
  @@history_lock = Mutex.new
  @purge_period = purge_period
  update_purge_times
end

Public Instance Methods

can_send?(ex) click to toggle source
# File lib/stackify/errors_governor.rb, line 13
def can_send? ex
  key = unique_key_of(ex)
  @@history_lock.synchronize do
    epoch_minute = current_minute
    init_history_key_if_not_exists key, epoch_minute
    history_entry = @history[key]
    if history_entry[:epoch_minute] == epoch_minute
      history_entry[:count] += 1
      answer = history_entry[:count] <= Stackify.configuration.flood_limit
    else
      @history[key]={
        epoch_minute: epoch_minute,
        count: 1
      }
      answer = true
    end
    clear_old_history_entries if time_for_purge_is_come?
    answer
  end
end

Private Instance Methods

clear_old_history_entries() click to toggle source
# File lib/stackify/errors_governor.rb, line 48
def clear_old_history_entries
  @history.keep_if{ |_key, entry| entry[:epoch_minute] == current_minute }
  update_purge_times
end
current_minute() click to toggle source
# File lib/stackify/errors_governor.rb, line 58
def current_minute
  Time.now.to_i/60
end
init_history_key_if_not_exists(key, minute) click to toggle source
# File lib/stackify/errors_governor.rb, line 41
def init_history_key_if_not_exists key, minute
  @history[key] ||= {
    epoch_minute: minute,
    count: 0
  }
end
time_for_purge_is_come?() click to toggle source
# File lib/stackify/errors_governor.rb, line 62
def time_for_purge_is_come?
  !(current_minute < @next_purge_minute)
end
unique_key_of(ex) click to toggle source
# File lib/stackify/errors_governor.rb, line 36
def unique_key_of ex
  str = "#{ex.backtrace[0]['LineNum']}-#{ex.source_method}-#{ex.error_type}"
  Digest::MD5.hexdigest str
end
update_purge_times() click to toggle source
# File lib/stackify/errors_governor.rb, line 53
def update_purge_times
  @last_purge_minute = current_minute
  @next_purge_minute = @last_purge_minute + @purge_period
end