Module: BreakerMachines::DSL
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/breaker_machines/dsl.rb
Overview
DSL module for adding circuit breakers to classes
This module uses WeakRef to track instances that include the DSL. Why? In long-running applications (web servers, background workers), objects that include this DSL may be created and destroyed frequently. Without WeakRef, the registry would hold strong references to these objects, preventing garbage collection and causing memory leaks.
Example scenario: A Rails controller that includes BreakerMachines::DSL is instantiated for each request. Without WeakRef, every controller instance would be kept in memory forever.
Defined Under Namespace
Classes: CircuitBuilder
Class Method Summary collapse
-
.included(base) ⇒ Object
Use included callback to add instance tracking.
Instance Method Summary collapse
- #circuit(name) ⇒ Object
-
#circuit_instances ⇒ Object
Get all circuit instances for this object.
-
#circuits_report ⇒ Object
Get detailed information for all circuits.
-
#circuits_summary ⇒ Object
Get summary of all circuits for this instance.
-
#reset_all_circuits ⇒ Object
Reset all circuits for this instance.
Class Method Details
.included(base) ⇒ Object
Use included callback to add instance tracking
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/breaker_machines/dsl.rb', line 113 def self.included(base) super # Hook into new to register instances base.singleton_class.prepend(Module.new do def new(...) instance = super instance_registry << WeakRef.new(instance) instance end end) end |
Instance Method Details
#circuit(name) ⇒ Object
126 127 128 129 130 |
# File 'lib/breaker_machines/dsl.rb', line 126 def circuit(name) self.class.circuits[name] ||= {} @circuit_instances ||= {} @circuit_instances[name] ||= Circuit.new(name, self.class.circuits[name].merge(owner: self)) end |
#circuit_instances ⇒ Object
Get all circuit instances for this object
133 134 135 |
# File 'lib/breaker_machines/dsl.rb', line 133 def circuit_instances @circuit_instances || {} end |
#circuits_report ⇒ Object
Get detailed information for all circuits
143 144 145 |
# File 'lib/breaker_machines/dsl.rb', line 143 def circuits_report circuit_instances.transform_values(&:to_h) end |
#circuits_summary ⇒ Object
Get summary of all circuits for this instance
138 139 140 |
# File 'lib/breaker_machines/dsl.rb', line 138 def circuits_summary circuit_instances.transform_values(&:summary) end |
#reset_all_circuits ⇒ Object
Reset all circuits for this instance
148 149 150 |
# File 'lib/breaker_machines/dsl.rb', line 148 def reset_all_circuits circuit_instances.each_value(&:reset) end |