Class: ResilientReads::HealthChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/resilient_reads/health_checker.rb

Overview

Background thread that periodically verifies replica reachability. Unhealthy replicas are re-checked so they can be restored to the pool once they recover.

Instance Method Summary collapse

Constructor Details

#initialize(replica_pool, interval:) ⇒ HealthChecker

Returns a new instance of HealthChecker.



6
7
8
9
10
11
# File 'lib/resilient_reads/health_checker.rb', line 6

def initialize(replica_pool, interval:)
  @replica_pool = replica_pool
  @interval = interval
  @thread = nil
  @running = false
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/resilient_reads/health_checker.rb', line 33

def running?
  @running && @thread&.alive?
end

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/resilient_reads/health_checker.rb', line 13

def start
  return if @replica_pool.empty?

  # Prevent duplicate threads — stop any existing thread first.
  stop if @running || @thread&.alive?

  @running = true
  @thread = Thread.new { run_loop }
  @thread.name = "resilient_reads_health"
  @thread.abort_on_exception = false
  @thread.report_on_exception = false
end

#stopObject



26
27
28
29
30
31
# File 'lib/resilient_reads/health_checker.rb', line 26

def stop
  @running = false
  @thread&.wakeup rescue nil
  @thread&.join(5) rescue nil
  @thread = nil
end