Class: Faulty::Storage::FaultTolerantProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/faulty/storage/fault_tolerant_proxy.rb

Overview

A wrapper for storage backends that may raise errors

Faulty::Scope 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.

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage, **options) {|Options| ... } ⇒ FaultTolerantProxy

Returns a new instance of FaultTolerantProxy.

Parameters:

Yields:

  • (Options)

    For setting options in a block



34
35
36
37
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 34

def initialize(storage, **options, &block)
  @storage = storage
  @options = Options.new(options, &block)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 13

def options
  @options
end

Instance Method Details

#close(circuit) ⇒ Boolean

Safely mark a circuit as closed

Returns:

  • (Boolean)

    True if the circuit transitioned from open to closed

See Also:



80
81
82
83
84
85
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 80

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) ⇒ Status

Add a history entry safely

Parameters:

  • circuit (Circuit)

    The circuit that ran

  • time (Integer)

    The unix timestamp for the run

  • success (Boolean)

    True if the run succeeded

Returns:

  • (Status)

    The circuit status after the run is added

See Also:



44
45
46
47
48
49
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 44

def entry(circuit, time, success)
  @storage.entry(circuit, time, success)
rescue StandardError => e
  options.notifier.notify(:storage_failure, circuit: circuit, action: :entry, error: e)
  stub_status(circuit)
end

#fault_tolerant?true

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

Returns:

  • (true)


159
160
161
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 159

def fault_tolerant?
  true
end

#history(circuit) ⇒ Array<Array>

Since history is not called in normal operation, it does not capture errors

Parameters:

  • circuit (Circuit)

    The circuit to get history for

Returns:

  • (Array<Array>)

    An array of history tuples

See Also:



138
139
140
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 138

def history(circuit)
  @storage.history(circuit)
end

#listArray<String>

Safely get the list of circuit names

If the backend is unavailable, this returns an empty array

Returns:

  • (Array<String>)

See Also:



149
150
151
152
153
154
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 149

def list
  @storage.list
rescue StandardError => e
  options.notifier.notify(:storage_failure, action: :list, error: e)
  []
end

#lock(circuit, state) ⇒ void

This method returns an undefined value.

Since lock is not called in normal operation, it does not capture errors

Parameters:

  • circuit (Circuit)

    The circuit to lock

  • state (:open, :closed)

    The state to lock the circuit in

See Also:



93
94
95
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 93

def lock(circuit, state)
  @storage.lock(circuit, state)
end

#open(circuit, opened_at) ⇒ Boolean

Safely mark a circuit as open

Parameters:

  • circuit (Circuit)

    The circuit to open

  • opened_at (Integer)

    The timestmp the circuit was opened at

Returns:

  • (Boolean)

    True if the circuit transitioned from closed to open

See Also:



56
57
58
59
60
61
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 56

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) ⇒ Boolean

Safely mark a circuit as reopened

Parameters:

  • circuit (Circuit)

    The circuit to reopen

  • opened_at (Integer)

    The timestmp the circuit was opened at

  • previous_opened_at (Integer)

    The last known value of opened_at. Can be used to comare-and-set.

Returns:

  • (Boolean)

    True if the opened_at time was updated

See Also:



68
69
70
71
72
73
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 68

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

#reset(circuit) ⇒ void

This method returns an undefined value.

Since reset is not called in normal operation, it does not capture errors

Parameters:

  • circuit (Circuit)

    The circuit to unlock

See Also:



113
114
115
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 113

def reset(circuit)
  @storage.reset(circuit)
end

#status(circuit) ⇒ Status

Safely get the status of a circuit

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

Parameters:

  • circuit (Circuit)

    The circuit to get status for

Returns:

  • (Status)

    The current status

See Also:



125
126
127
128
129
130
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 125

def status(circuit)
  @storage.status(circuit)
rescue StandardError => e
  options.notifier.notify(:storage_failure, circuit: circuit, action: :status, error: e)
  stub_status(circuit)
end

#unlock(circuit) ⇒ void

This method returns an undefined value.

Since unlock is not called in normal operation, it does not capture errors

Parameters:

  • circuit (Circuit)

    The circuit to unlock

See Also:



103
104
105
# File 'lib/faulty/storage/fault_tolerant_proxy.rb', line 103

def unlock(circuit)
  @storage.unlock(circuit)
end