Class: PgFailover::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_failover/throttle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(throttle_interval: nil) ⇒ Throttle

Returns a new instance of Throttle.



5
6
7
8
# File 'lib/pg_failover/throttle.rb', line 5

def initialize(throttle_interval: nil)
  @last_good_at = {}
  @throttle_interval = throttle_interval || 0.0
end

Instance Attribute Details

#throttle_intervalObject (readonly)

Returns the value of attribute throttle_interval.



10
11
12
# File 'lib/pg_failover/throttle.rb', line 10

def throttle_interval
  @throttle_interval
end

Instance Method Details

#known?(connection) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/pg_failover/throttle.rb', line 33

def known?(connection)
  @last_good_at[connection]
end

#on_stale(connection) ⇒ Object



16
17
18
19
20
21
22
23
24
# 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) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# 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

#sizeObject



12
13
14
# File 'lib/pg_failover/throttle.rb', line 12

def size
  @last_good_at.count
end