class PgFailover::Throttle

Attributes

throttle_interval[R]

Public Class Methods

new(throttle_interval: nil) click to toggle source
# File lib/pg_failover/throttle.rb, line 5
def initialize(throttle_interval: nil)
  @last_good_at = {}
  @throttle_interval = throttle_interval || 0.0
end

Public Instance Methods

known?(connection) click to toggle source
# File lib/pg_failover/throttle.rb, line 33
def known?(connection)
  @last_good_at[connection]
end
on_stale(connection) { || ... } click to toggle source
# File lib/pg_failover/throttle.rb, line 16
def on_stale(connection)
  return if should_throttle?(connection)

  clear_stale_throttle_times

  valid = yield

  @last_good_at[connection] = Time.now.to_f if valid
end
should_throttle?(connection) click to toggle source
# File lib/pg_failover/throttle.rb, line 26
def should_throttle?(connection)
  return if @last_good_at[connection].nil?

  connection_check_age = (Time.now.to_f - @last_good_at[connection])
  connection_check_age < throttle_interval
end
size() click to toggle source
# File lib/pg_failover/throttle.rb, line 12
def size
  @last_good_at.count
end

Private Instance Methods

clear_stale_throttle_times() click to toggle source
# File lib/pg_failover/throttle.rb, line 39
def clear_stale_throttle_times
  stale_after = Time.now.to_f - throttle_interval * 3

  @last_good_at.delete_if { |_k, v| stale_after > v }
end