Class: BreakerMachines::DSL::CircuitBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/breaker_machines/dsl.rb

Overview

DSL builder for configuring circuit breakers with a fluent interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCircuitBuilder

Returns a new instance of CircuitBuilder.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/breaker_machines/dsl.rb', line 156

def initialize
  @config = {
    failure_threshold: 5,
    failure_window: 60,
    success_threshold: 1,
    timeout: nil,
    reset_timeout: 60,
    half_open_calls: 1,
    exceptions: [StandardError],
    storage: nil,
    metrics: nil,
    fallback: nil,
    on_open: nil,
    on_close: nil,
    on_half_open: nil,
    on_reject: nil,
    notifications: [],
    fiber_safe: BreakerMachines.config.fiber_safe
  }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#backends(list) ⇒ Object

Advanced features



264
265
266
# File 'lib/breaker_machines/dsl.rb', line 264

def backends(list)
  @config[:backends] = list
end

#fallback(value = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/breaker_machines/dsl.rb', line 215

def fallback(value = nil, &block)
  raise ArgumentError, 'Fallback requires either a value or a block' if value.nil? && !block_given?

  fallback_value = block || value

  if @config[:fallback].is_a?(Array)
    @config[:fallback] << fallback_value
  elsif @config[:fallback]
    @config[:fallback] = [@config[:fallback], fallback_value]
  else
    @config[:fallback] = fallback_value
  end
end

#fiber_safe(enabled = true) ⇒ Object



259
260
261
# File 'lib/breaker_machines/dsl.rb', line 259

def fiber_safe(enabled = true)
  @config[:fiber_safe] = enabled
end

#half_open_requests(count) ⇒ Object



192
193
194
# File 'lib/breaker_machines/dsl.rb', line 192

def half_open_requests(count)
  @config[:half_open_calls] = count
end

#handle(*exceptions) ⇒ Object



255
256
257
# File 'lib/breaker_machines/dsl.rb', line 255

def handle(*exceptions)
  @config[:exceptions] = exceptions
end

#metrics(recorder = nil, &block) ⇒ Object



211
212
213
# File 'lib/breaker_machines/dsl.rb', line 211

def metrics(recorder = nil, &block)
  @config[:metrics] = recorder || block
end

#notify(service, url = nil, events: %i[open close],, **options) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/breaker_machines/dsl.rb', line 245

def notify(service, url = nil, events: %i[open close], **options)
  notification = {
    via: service,
    url: url,
    events: Array(events),
    options: options
  }
  @config[:notifications] << notification
end

#on_close(&block) ⇒ Object



233
234
235
# File 'lib/breaker_machines/dsl.rb', line 233

def on_close(&block)
  @config[:on_close] = block
end

#on_half_open(&block) ⇒ Object



237
238
239
# File 'lib/breaker_machines/dsl.rb', line 237

def on_half_open(&block)
  @config[:on_half_open] = block
end

#on_open(&block) ⇒ Object



229
230
231
# File 'lib/breaker_machines/dsl.rb', line 229

def on_open(&block)
  @config[:on_open] = block
end

#on_reject(&block) ⇒ Object



241
242
243
# File 'lib/breaker_machines/dsl.rb', line 241

def on_reject(&block)
  @config[:on_reject] = block
end

#parallel_calls(count, timeout: nil) ⇒ Object



268
269
270
271
# File 'lib/breaker_machines/dsl.rb', line 268

def parallel_calls(count, timeout: nil)
  @config[:parallel_calls] = count
  @config[:parallel_timeout] = timeout
end

#reset_after(duration, jitter: nil) ⇒ Object



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

def reset_after(duration, jitter: nil)
  @config[:reset_timeout] = duration.to_i
  @config[:reset_timeout_jitter] = jitter if jitter
end

#storage(backend) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/breaker_machines/dsl.rb', line 196

def storage(backend, **)
  @config[:storage] = case backend
                      when :memory
                        Storage::Memory.new(**)
                      when :bucket_memory
                        Storage::BucketMemory.new(**)
                      when :redis
                        Storage::Redis.new(**)
                      when Class
                        backend.new(**)
                      else
                        backend
                      end
end

#threshold(failures: nil, within: 60, successes: nil) ⇒ Object



177
178
179
180
181
# File 'lib/breaker_machines/dsl.rb', line 177

def threshold(failures: nil, within: 60, successes: nil)
  @config[:failure_threshold] = failures if failures
  @config[:failure_window] = within.to_i
  @config[:success_threshold] = successes if successes
end

#timeout(duration) ⇒ Object



188
189
190
# File 'lib/breaker_machines/dsl.rb', line 188

def timeout(duration)
  @config[:timeout] = duration.to_i
end