Class: Faulty::Storage::Redis
- Inherits:
-
Object
- Object
- Faulty::Storage::Redis
- Defined in:
- lib/faulty/storage/redis.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: Options
Constant Summary collapse
- ENTRY_SEPARATOR =
Separates the time/status for history entry strings
':'
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
Redis storage is not fault-tolerant.
-
#history(circuit) ⇒ Array<Array>
Get the circuit history up to
max_sample_size
. -
#initialize(**options) {|Options| ... } ⇒ Redis
constructor
A new instance of Redis.
- #list ⇒ Object
-
#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.
9 10 11 |
# File 'lib/faulty/storage/redis.rb', line 9 def @options end |
Instance Method Details
#close(circuit) ⇒ Boolean
Mark a circuit as closed
122 123 124 125 126 127 128 |
# File 'lib/faulty/storage/redis.rb', line 122 def close(circuit) redis do |r| closed = compare_and_set(r, state_key(circuit), ['open'], 'closed') r.del(entries_key(circuit)) if closed closed end end |
#entry(circuit, time, success) ⇒ Status
Add an entry to storage
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/faulty/storage/redis.rb', line 80 def entry(circuit, time, success) key = entries_key(circuit) pipe do |r| r.sadd(list_key, circuit.name) r.expire(list_key, .circuit_ttl + .list_granularity) if .circuit_ttl r.lpush(key, "#{time}#{ENTRY_SEPARATOR}#{success ? 1 : 0}") r.ltrim(key, 0, .max_sample_size - 1) r.expire(key, .sample_ttl) if .sample_ttl end status(circuit) end |
#fault_tolerant? ⇒ true
Redis storage is not fault-tolerant
206 207 208 |
# File 'lib/faulty/storage/redis.rb', line 206 def fault_tolerant? false end |
#history(circuit) ⇒ Array<Array>
Get the circuit history up to max_sample_size
194 195 196 197 |
# File 'lib/faulty/storage/redis.rb', line 194 def history(circuit) entries = redis { |r| r.lrange(entries_key(circuit), 0, -1) } map_entries(entries).reverse end |
#list ⇒ Object
199 200 201 |
# File 'lib/faulty/storage/redis.rb', line 199 def list redis { |r| r.sunion(*all_list_keys) } end |
#lock(circuit, state) ⇒ void
This method returns an undefined value.
Lock a circuit open or closed
The circuit_ttl does not apply to locks
137 138 139 |
# File 'lib/faulty/storage/redis.rb', line 137 def lock(circuit, state) redis { |r| r.set(lock_key(circuit), state) } end |
#open(circuit, opened_at) ⇒ Boolean
Mark a circuit as open
98 99 100 101 102 103 104 |
# File 'lib/faulty/storage/redis.rb', line 98 def open(circuit, opened_at) redis do |r| opened = compare_and_set(r, state_key(circuit), ['closed', nil], 'open') r.set(opened_at_key(circuit), opened_at, ex: .circuit_ttl) if opened opened end end |
#reopen(circuit, opened_at, previous_opened_at) ⇒ Boolean
Mark a circuit as reopened
111 112 113 114 115 |
# File 'lib/faulty/storage/redis.rb', line 111 def reopen(circuit, opened_at, previous_opened_at) redis do |r| compare_and_set(r, opened_at_key(circuit), [previous_opened_at.to_s], opened_at) end end |
#reset(circuit) ⇒ void
This method returns an undefined value.
Reset a circuit
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/faulty/storage/redis.rb', line 155 def reset(circuit) pipe do |r| r.del( entries_key(circuit), opened_at_key(circuit), lock_key(circuit) ) r.set(state_key(circuit), 'closed', ex: .circuit_ttl) end end |
#status(circuit) ⇒ Status
Get the status of a circuit
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/faulty/storage/redis.rb', line 171 def status(circuit) futures = {} pipe do |r| futures[:state] = r.get(state_key(circuit)) futures[:lock] = r.get(lock_key(circuit)) futures[:opened_at] = r.get(opened_at_key(circuit)) futures[:entries] = r.lrange(entries_key(circuit), 0, -1) end Faulty::Status.from_entries( map_entries(futures[:entries].value), state: futures[:state].value&.to_sym || :closed, lock: futures[:lock].value&.to_sym, opened_at: futures[:opened_at].value ? futures[:opened_at].value.to_i : nil, options: circuit. ) end |
#unlock(circuit) ⇒ void
This method returns an undefined value.
Unlock a circuit
146 147 148 |
# File 'lib/faulty/storage/redis.rb', line 146 def unlock(circuit) redis { |r| r.del(lock_key(circuit)) } end |