Class: BreakerMachines::Registry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/breaker_machines/registry.rb

Overview

Global registry for tracking all circuit breaker instances

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



11
12
13
14
15
16
# File 'lib/breaker_machines/registry.rb', line 11

def initialize
  @circuits = Concurrent::Map.new
  @mutex = Mutex.new
  @registration_count = 0
  @cleanup_interval = 100 # Clean up every N registrations
end

Instance Method Details

#all_circuitsObject

Get all active circuits



41
42
43
44
45
46
47
48
49
# File 'lib/breaker_machines/registry.rb', line 41

def all_circuits
  @mutex.synchronize do
    @circuits.values.map do |weak_ref|
      weak_ref.__getobj__
    rescue WeakRef::RefError
      nil
    end.compact
  end
end

#cleanup_dead_referencesObject

Clean up dead references (thread-safe)



79
80
81
82
83
# File 'lib/breaker_machines/registry.rb', line 79

def cleanup_dead_references
  @mutex.synchronize do
    cleanup_dead_references_unsafe
  end
end

#clearObject

Clear all circuits (useful for testing)



72
73
74
75
76
# File 'lib/breaker_machines/registry.rb', line 72

def clear
  @mutex.synchronize do
    @circuits.clear
  end
end

#detailed_reportObject

Get detailed information for all circuits



67
68
69
# File 'lib/breaker_machines/registry.rb', line 67

def detailed_report
  all_circuits.map(&:to_h)
end

#find_by_name(name) ⇒ Object

Find circuits by name



52
53
54
# File 'lib/breaker_machines/registry.rb', line 52

def find_by_name(name)
  all_circuits.select { |circuit| circuit.name == name }
end

#register(circuit) ⇒ Object

Register a circuit instance



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/breaker_machines/registry.rb', line 19

def register(circuit)
  @mutex.synchronize do
    # Use circuit object as key - Concurrent::Map handles object identity correctly
    @circuits[circuit] = WeakRef.new(circuit)

    # Periodic cleanup
    @registration_count += 1
    if @registration_count >= @cleanup_interval
      cleanup_dead_references_unsafe
      @registration_count = 0
    end
  end
end

#stats_summaryObject

Get summary statistics



57
58
59
60
61
62
63
64
# File 'lib/breaker_machines/registry.rb', line 57

def stats_summary
  circuits = all_circuits
  {
    total: circuits.size,
    by_state: circuits.group_by(&:status_name).transform_values(&:count),
    by_name: circuits.group_by(&:name).transform_values(&:count)
  }
end

#unregister(circuit) ⇒ Object

Unregister a circuit instance



34
35
36
37
38
# File 'lib/breaker_machines/registry.rb', line 34

def unregister(circuit)
  @mutex.synchronize do
    @circuits.delete(circuit)
  end
end