Module: BreakerMachines

Defined in:
lib/breaker_machines.rb,
lib/breaker_machines/dsl.rb,
lib/breaker_machines/types.rb,
lib/breaker_machines/errors.rb,
lib/breaker_machines/circuit.rb,
lib/breaker_machines/console.rb,
lib/breaker_machines/storage.rb,
lib/breaker_machines/version.rb,
lib/breaker_machines/registry.rb,
lib/breaker_machines/circuit/base.rb,
lib/breaker_machines/storage/base.rb,
lib/breaker_machines/storage/null.rb,
lib/breaker_machines/async_circuit.rb,
lib/breaker_machines/async_support.rb,
lib/breaker_machines/circuit_group.rb,
lib/breaker_machines/storage/cache.rb,
lib/breaker_machines/circuit/native.rb,
lib/breaker_machines/storage/memory.rb,
lib/breaker_machines/storage/native.rb,
lib/breaker_machines/native_extension.rb,
lib/breaker_machines/cascading_circuit.rb,
lib/breaker_machines/circuit/callbacks.rb,
lib/breaker_machines/circuit/execution.rb,
lib/breaker_machines/dsl/hedged_builder.rb,
lib/breaker_machines/coordinated_circuit.rb,
lib/breaker_machines/dsl/circuit_builder.rb,
lib/breaker_machines/hedged_async_support.rb,
lib/breaker_machines/circuit/configuration.rb,
lib/breaker_machines/circuit/introspection.rb,
lib/breaker_machines/storage/backend_state.rb,
lib/breaker_machines/storage/bucket_memory.rb,
lib/breaker_machines/storage/fallback_chain.rb,
lib/breaker_machines/circuit/state_callbacks.rb,
lib/breaker_machines/circuit/hedged_execution.rb,
lib/breaker_machines/circuit/state_management.rb,
lib/breaker_machines/dsl/cascading_circuit_builder.rb,
lib/breaker_machines/dsl/parallel_fallback_wrapper.rb,
lib/breaker_machines/circuit/async_state_management.rb,
lib/breaker_machines/circuit/coordinated_state_management.rb

Overview

BreakerMachines provides a thread-safe implementation of the Circuit Breaker pattern for Ruby applications, helping to prevent cascading failures in distributed systems.

Defined Under Namespace

Modules: AsyncSupport, DSL, HedgedAsyncSupport, NativeExtension, Storage Classes: AsyncCircuit, CascadeInfo, CascadingCircuit, Circuit, CircuitBulkheadError, CircuitDependencyError, CircuitGroup, CircuitOpenError, CircuitTimeoutError, Configuration, ConfigurationError, Console, CoordinatedCircuit, Error, ErrorInfo, Event, ParallelFallbackError, Registry, Stats, Status, StorageError, StorageTimeoutError

Constant Summary collapse

VERSION =
'0.10.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject



125
126
127
# File 'lib/breaker_machines.rb', line 125

def logger
  @logger ||= ActiveSupport::Logger.new($stdout)
end

Class Method Details

.configObject



50
51
52
# File 'lib/breaker_machines.rb', line 50

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



54
55
56
# File 'lib/breaker_machines.rb', line 54

def configure
  yield config
end

.consoleObject

Launch the interactive console



153
154
155
156
# File 'lib/breaker_machines.rb', line 153

def console
  require_relative 'breaker_machines/console'
  Console.start
end

.default_failure_thresholdObject



83
84
85
# File 'lib/breaker_machines.rb', line 83

def default_failure_threshold
  config.default_failure_threshold
end

.default_failure_threshold=(value) ⇒ Object



87
88
89
# File 'lib/breaker_machines.rb', line 87

def default_failure_threshold=(value)
  config.default_failure_threshold = value
end

.default_reset_timeoutObject



75
76
77
# File 'lib/breaker_machines.rb', line 75

def default_reset_timeout
  config.default_reset_timeout
end

.default_reset_timeout=(value) ⇒ Object



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

def default_reset_timeout=(value)
  config.default_reset_timeout = value
end

.default_storageObject

Delegate config attributes to config object for backward compatibility



59
60
61
# File 'lib/breaker_machines.rb', line 59

def default_storage
  config.default_storage
end

.default_storage=(value) ⇒ Object



63
64
65
# File 'lib/breaker_machines.rb', line 63

def default_storage=(value)
  config.default_storage = value
end

.default_timeoutObject



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

def default_timeout
  config.default_timeout
end

.default_timeout=(value) ⇒ Object



71
72
73
# File 'lib/breaker_machines.rb', line 71

def default_timeout=(value)
  config.default_timeout = value
end

.fiber_safeObject



99
100
101
# File 'lib/breaker_machines.rb', line 99

def fiber_safe
  config.fiber_safe
end

.fiber_safe=(value) ⇒ Object



103
104
105
# File 'lib/breaker_machines.rb', line 103

def fiber_safe=(value)
  config.fiber_safe = value
end

.instrument(event, payload = {}) ⇒ Object



140
141
142
143
144
# File 'lib/breaker_machines.rb', line 140

def instrument(event, payload = {})
  return unless config.log_events

  ActiveSupport::Notifications.instrument("breaker_machines.#{event}", payload)
end

.loaderObject



46
47
48
# File 'lib/breaker_machines.rb', line 46

def loader
  loader
end

.log(level, message) ⇒ Object

Centralized logging helper

Parameters:

  • level (Symbol)

    log level (:debug, :info, :warn, :error)

  • message (String)

    message to log



134
135
136
137
138
# File 'lib/breaker_machines.rb', line 134

def log(level, message)
  return unless config.log_events && logger

  logger.public_send(level, "[BreakerMachines] #{message}")
end

.log_eventsObject



91
92
93
# File 'lib/breaker_machines.rb', line 91

def log_events
  config.log_events
end

.log_events=(value) ⇒ Object



95
96
97
# File 'lib/breaker_machines.rb', line 95

def log_events=(value)
  config.log_events = value
end

.monotonic_timeFloat

Returns the current monotonic time in seconds. Monotonic time is guaranteed to always increase and is not affected by system clock adjustments, making it ideal for measuring durations.

Returns:

  • (Float)

    current monotonic time in seconds



184
185
186
# File 'lib/breaker_machines.rb', line 184

def monotonic_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

.native_available?Boolean

Check if native extension is available

Returns:

  • (Boolean)

    true if native extension loaded successfully



148
149
150
# File 'lib/breaker_machines.rb', line 148

def native_available?
  @native_available || false
end

.register(circuit) ⇒ Object

Register a circuit with the global registry



164
165
166
# File 'lib/breaker_machines.rb', line 164

def register(circuit)
  registry.register(circuit)
end

.registryObject

Get the global registry



159
160
161
# File 'lib/breaker_machines.rb', line 159

def registry
  Registry.instance
end

.reset!Object

Reset the registry and configurations (useful for testing)



169
170
171
172
173
174
175
176
177
# File 'lib/breaker_machines.rb', line 169

def reset!
  registry.clear
  config.default_storage = :bucket_memory
  config.default_timeout = nil
  config.default_reset_timeout = 60.seconds
  config.default_failure_threshold = 5
  config.log_events = true
  config.fiber_safe = false
end

.setup_notificationsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/breaker_machines.rb', line 107

def setup_notifications
  return unless config.log_events

  ActiveSupport::Notifications.subscribe(/^breaker_machines\./) do |name, _start, _finish, _id, payload|
    event_type = name.split('.').last
    circuit_name = payload[:circuit]

    case event_type
    when 'opened'
      logger&.warn "[BreakerMachines] Circuit '#{circuit_name}' opened"
    when 'closed'
      logger&.info "[BreakerMachines] Circuit '#{circuit_name}' closed"
    when 'half_opened'
      logger&.info "[BreakerMachines] Circuit '#{circuit_name}' half-opened"
    end
  end
end