class Faulty::Storage::FaultTolerantProxy

A wrapper for storage backends that may raise errors

{Faulty#initialize} automatically wraps all non-fault-tolerant storage backends with this class.

If the storage backend raises a `StandardError`, it will be captured and sent to the notifier.

Constants

Options

Options for {FaultTolerantProxy}

@!attribute [r] notifier

@return [Events::Notifier] A Faulty notifier

Attributes

options[R]

Public Class Methods

new(storage, **options, &block) click to toggle source

@param storage [Storage::Interface] The storage backend to wrap @param options [Hash] Attributes for {Options} @yield [Options] For setting options in a block

# File lib/faulty/storage/fault_tolerant_proxy.rb, line 36
def initialize(storage, **options, &block)
  @storage = storage
  @options = Options.new(options, &block)
end
wrap(storage, **options, &block) click to toggle source

Wrap a storage backend in a FaultTolerantProxy unless it's already fault tolerant

@param storage [Storage::Interface] The storage to maybe wrap @return [Storage::Interface] The original storage or a {FaultTolerantProxy}

# File lib/faulty/storage/fault_tolerant_proxy.rb, line 46
def self.wrap(storage, **options, &block)
  return storage if storage.fault_tolerant?

  new(storage, **options, &block)
end

Public Instance Methods

close(circuit) click to toggle source

Safely mark a circuit as closed

@see Interface#close @param (see Interface#close) @return (see Interface#close)

# File lib/faulty/storage/fault_tolerant_proxy.rb, line 129
def close(circuit)
  @storage.close(circuit)
rescue StandardError => e
  options.notifier.notify(:storage_failure, circuit: circuit, action: :close, error: e)
  false
end
entry(circuit, time, success) click to toggle source

Add a history entry safely

@see Interface#entry @param (see Interface#entry) @return (see Interface#entry)

# File lib/faulty/storage/fault_tolerant_proxy.rb, line 93
def entry(circuit, time, success)
  @storage.entry(circuit, time, success)
rescue StandardError => e
  options.notifier.notify(:storage_failure, circuit: circuit, action: :entry, error: e)
  []
end
fault_tolerant?() click to toggle source

This cache makes any storage fault tolerant, so this is always `true`

@return [true]

# File lib/faulty/storage/fault_tolerant_proxy.rb, line 154
def fault_tolerant?
  true
end
open(circuit, opened_at) click to toggle source

Safely mark a circuit as open

@see Interface#open @param (see Interface#open) @return (see Interface#open)

# File lib/faulty/storage/fault_tolerant_proxy.rb, line 105
def open(circuit, opened_at)
  @storage.open(circuit, opened_at)
rescue StandardError => e
  options.notifier.notify(:storage_failure, circuit: circuit, action: :open, error: e)
  false
end
reopen(circuit, opened_at, previous_opened_at) click to toggle source

Safely mark a circuit as reopened

@see Interface#reopen @param (see Interface#reopen) @return (see Interface#reopen)

# File lib/faulty/storage/fault_tolerant_proxy.rb, line 117
def reopen(circuit, opened_at, previous_opened_at)
  @storage.reopen(circuit, opened_at, previous_opened_at)
rescue StandardError => e
  options.notifier.notify(:storage_failure, circuit: circuit, action: :reopen, error: e)
  false
end
status(circuit) click to toggle source

Safely get the status of a circuit

If the backend is unavailable, this returns a stub status that indicates that the circuit is closed.

@see Interface#status @param (see Interface#status) @return (see Interface#status)

# File lib/faulty/storage/fault_tolerant_proxy.rb, line 144
def status(circuit)
  @storage.status(circuit)
rescue StandardError => e
  options.notifier.notify(:storage_failure, circuit: circuit, action: :status, error: e)
  stub_status(circuit)
end

Private Instance Methods

stub_status(circuit) click to toggle source

Create a stub status object to close the circuit by default

@return [Status] The stub status

# File lib/faulty/storage/fault_tolerant_proxy.rb, line 163
def stub_status(circuit)
  Faulty::Status.new(
    options: circuit.options,
    stub: true
  )
end