Class: Faulty::Storage::Memory
- Inherits:
-
Object
- Object
- Faulty::Storage::Memory
- Defined in:
- lib/faulty/storage/memory.rb
Overview
The default in-memory storage for circuits
This implementation is most suitable to single-process, low volume usage. It is thread-safe and circuit state is shared across threads.
Circuit state and runs are stored in memory. Although runs have a maximum size within a circuit, there is no limit on the number of circuits that can be stored. This means the user should be careful about the number of circuits that are created. To that end, it's a good idea to avoid dynamically-named circuits with this backend.
For a more robust distributed implementation, use the Redis storage backend.
This can be used as a reference implementation for storage backends that store a list of circuit run entries.
Defined Under Namespace
Classes: MemoryCircuit, Options
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#close(circuit) ⇒ Boolean
Mark a circuit as closed.
-
#entry(circuit, time, success) ⇒ Status
Add an entry to storage.
-
#fault_tolerant? ⇒ true
Memory storage is fault-tolerant by default.
-
#history(circuit) ⇒ Array<Array>
Get the circuit history up to
max_sample_size. -
#initialize(**options) {|Options| ... } ⇒ Memory
constructor
A new instance of Memory.
-
#list ⇒ Array<String>
Get a list of circuit names.
-
#lock(circuit, state) ⇒ void
Lock a circuit open or closed.
-
#open(circuit, opened_at) ⇒ Boolean
Mark a circuit as open.
-
#reopen(circuit, opened_at, previous_opened_at) ⇒ Boolean
Mark a circuit as reopened.
-
#reset(circuit) ⇒ void
Reset a circuit.
-
#status(circuit) ⇒ Status
Get the status of a circuit.
-
#unlock(circuit) ⇒ void
Unlock a circuit.
Constructor Details
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
22 23 24 |
# File 'lib/faulty/storage/memory.rb', line 22 def @options end |
Instance Method Details
#close(circuit) ⇒ Boolean
Mark a circuit as closed
118 119 120 121 122 |
# File 'lib/faulty/storage/memory.rb', line 118 def close(circuit) memory = fetch(circuit) memory.runs.modify { |_old| [] } memory.state.compare_and_set(:open, :closed) end |
#entry(circuit, time, success) ⇒ Status
Add an entry to storage
82 83 84 85 86 87 88 89 |
# File 'lib/faulty/storage/memory.rb', line 82 def entry(circuit, time, success) memory = fetch(circuit) memory.runs.borrow do |runs| runs.push([time, success]) runs.pop if runs.size > .max_sample_size end memory.status(circuit.) end |
#fault_tolerant? ⇒ true
Memory storage is fault-tolerant by default
181 182 183 |
# File 'lib/faulty/storage/memory.rb', line 181 def fault_tolerant? true end |
#history(circuit) ⇒ Array<Array>
Get the circuit history up to max_sample_size
167 168 169 |
# File 'lib/faulty/storage/memory.rb', line 167 def history(circuit) fetch(circuit).runs.value end |
#list ⇒ Array<String>
Get a list of circuit names
174 175 176 |
# File 'lib/faulty/storage/memory.rb', line 174 def list @circuits.keys end |
#lock(circuit, state) ⇒ void
This method returns an undefined value.
Lock a circuit open or closed
129 130 131 132 |
# File 'lib/faulty/storage/memory.rb', line 129 def lock(circuit, state) memory = fetch(circuit) memory.lock = state end |
#open(circuit, opened_at) ⇒ Boolean
Mark a circuit as open
96 97 98 99 100 101 |
# File 'lib/faulty/storage/memory.rb', line 96 def open(circuit, opened_at) memory = fetch(circuit) opened = memory.state.compare_and_set(:closed, :open) memory.opened_at.reset(opened_at) if opened opened end |
#reopen(circuit, opened_at, previous_opened_at) ⇒ Boolean
Mark a circuit as reopened
108 109 110 111 |
# File 'lib/faulty/storage/memory.rb', line 108 def reopen(circuit, opened_at, previous_opened_at) memory = fetch(circuit) memory.opened_at.compare_and_set(previous_opened_at, opened_at) end |
#reset(circuit) ⇒ void
This method returns an undefined value.
Reset a circuit
149 150 151 |
# File 'lib/faulty/storage/memory.rb', line 149 def reset(circuit) @circuits.delete(circuit.name) end |
#status(circuit) ⇒ Status
Get the status of a circuit
158 159 160 |
# File 'lib/faulty/storage/memory.rb', line 158 def status(circuit) fetch(circuit).status(circuit.) end |
#unlock(circuit) ⇒ void
This method returns an undefined value.
Unlock a circuit
139 140 141 142 |
# File 'lib/faulty/storage/memory.rb', line 139 def unlock(circuit) memory = fetch(circuit) memory.lock = nil end |