Top Level Namespace

Defined Under Namespace

Modules: Stoplight

Instance Method Summary collapse

Instance Method Details

#Stoplight(name, cool_off_time: Stoplight::T.undefined, threshold: Stoplight::T.undefined, recovery_threshold: Stoplight::T.undefined, window_size: Stoplight::T.undefined, tracked_errors: Stoplight::T.undefined, skipped_errors: Stoplight::T.undefined, data_store: Stoplight::T.undefined, error_notifier: Stoplight::T.undefined, notifiers: Stoplight::T.undefined, traffic_control: Stoplight::T.undefined, traffic_recovery: Stoplight::T.undefined) ⇒ Stoplight::Light

Creates a new Stoplight circuit brNeaker with the given name and settings.

In the example below, the TimeoutError and NetworkError exceptions will be counted towards the threshold for moving the circuit breaker into the red state. If not configured, the default tracked error is StandardError.

In the example below , the ActiveRecord::RecordNotFound doesn’t move the circuit breaker into the red state.

Examples:

configure circuit breaker behavior

light = Stoplight("Payment API", window_size: 300, threshold: 5, cool_off_time: 60)

configure data store

light = Stoplight("Payment API", data_store: Stoplight::DataStore::Redis.new(redis_client))

configure tracked errors

light = Stoplight("Payment API", tracked_errors: [TimeoutError, NetworkError])

configure skipped errors

light = Stoplight("Payment API", skipped_errors: [ActiveRecord::RecordNotFound])

configure traffic control to trip using consecutive failures method

# When 5 consecutive failures occur, the circuit breaker will trip.
light = Stoplight("Payment API", traffic_control: :consecutive_errors, threshold: 5)

configure traffic control to trip using error rate method

# When 66.6% error rate reached withing a sliding 5 minute window, the circuit breaker will trip.
light = Stoplight("Payment API", traffic_control: :error_rate, threshold: 0.666, window_size: 300)

Parameters:

  • name (String)

    The name of the circuit breaker.

  • settings (Hash)

    Optional settings to configure the circuit breaker. @option settings [Numeric] :cool_off_time The time to wait before resetting the circuit breaker. @option settings [Stoplight::DataStore::Base] :data_store The data store to use for storing state. @option settings [Array<Stoplight::Notifier::Base>] :notifiers A list of notifiers to use. @option settings [Numeric] :threshold The failure threshold to trip the circuit breaker. @option settings [Numeric] :window_size The size of the rolling window for failure tracking. @option settings [Array<StandardError>] :tracked_errors A list of errors to track. @option settings [Array<Exception>] :skipped_errors A list of errors to skip. @option settings [Symbol, Hash{Symbol, any}] :traffic_control The

    traffic control strategy to use.
    

Returns:

  • (Stoplight::Light)

    A new circuit breaker instance.

Raises:

  • (ArgumentError)

    If an unknown option is provided in the settings.



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/stoplight.rb', line 309

def Stoplight(
  name,
  cool_off_time: Stoplight::T.undefined,
  threshold: Stoplight::T.undefined,
  recovery_threshold: Stoplight::T.undefined,
  window_size: Stoplight::T.undefined,
  tracked_errors: Stoplight::T.undefined,
  skipped_errors: Stoplight::T.undefined,
  data_store: Stoplight::T.undefined,
  error_notifier: Stoplight::T.undefined,
  notifiers: Stoplight::T.undefined,
  traffic_control: Stoplight::T.undefined,
  traffic_recovery: Stoplight::T.undefined
) # rubocop:disable Naming/MethodName
  Stoplight::Common::Deprecations.deprecate(<<~MSG) if error_notifier != Stoplight::T.undefined
    Passing "error_notifier" to Stoplight('#{name}') is deprecated and will be removed in v6.0.0.

    IMPORTANT: The `error_notifier` is NOT called for exceptions in your protected code.
    It only reports internal Stoplight failures (e.g., Redis connection errors).

    To fix: Move `error_notifier` to global configuration:

      Stoplight.configure do |config|
        config.error_notifier = ->(error) { Logger.warn(error) }
      end

    See: https://github.com/bolshakov/stoplight#error-notifiers
  MSG
  Stoplight.light(
    name,
    cool_off_time:,
    threshold:,
    recovery_threshold:,
    window_size:,
    tracked_errors:,
    skipped_errors:,
    data_store:,
    error_notifier:,
    notifiers:,
    traffic_control:,
    traffic_recovery:
  )
end