Class: HTTPigeon::CircuitBreaker::MemoryStore
- Inherits:
-
Object
- Object
- HTTPigeon::CircuitBreaker::MemoryStore
- Defined in:
- lib/httpigeon/circuit_breaker/memory_store.rb
Constant Summary collapse
- MAX_SAMPLE_WINDOW =
180
Instance Attribute Summary collapse
-
#sample_window ⇒ Object
readonly
Returns the value of attribute sample_window.
Instance Method Summary collapse
- #delete(key) ⇒ Object (also: #del)
- #get(key) ⇒ Object (also: #[])
- #increment(key, value = 1, opts = {}) ⇒ Object (also: #incr)
-
#initialize(sample_window) ⇒ MemoryStore
constructor
A new instance of MemoryStore.
- #key?(key) ⇒ Boolean
- #reset! ⇒ Object (also: #clear!)
- #set(key, value, opts = {}) ⇒ Object (also: #store)
- #storage ⇒ Object
Constructor Details
#initialize(sample_window) ⇒ MemoryStore
Returns a new instance of MemoryStore.
8 9 10 11 12 |
# File 'lib/httpigeon/circuit_breaker/memory_store.rb', line 8 def initialize(sample_window) @storage = {} @mutex = Mutex.new @sample_window = [sample_window.to_i, MAX_SAMPLE_WINDOW].min end |
Instance Attribute Details
#sample_window ⇒ Object (readonly)
Returns the value of attribute sample_window.
6 7 8 |
# File 'lib/httpigeon/circuit_breaker/memory_store.rb', line 6 def sample_window @sample_window end |
Instance Method Details
#delete(key) ⇒ Object Also known as: del
48 49 50 |
# File 'lib/httpigeon/circuit_breaker/memory_store.rb', line 48 def delete(key) @mutex.synchronize { @storage.delete(key) } end |
#get(key) ⇒ Object Also known as: []
14 15 16 |
# File 'lib/httpigeon/circuit_breaker/memory_store.rb', line 14 def get(key) @mutex.synchronize { fetch_bucket(key)&.value } end |
#increment(key, value = 1, opts = {}) ⇒ Object Also known as: incr
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/httpigeon/circuit_breaker/memory_store.rb', line 29 def increment(key, value = 1, opts = {}) @mutex.synchronize do existing_bucket = fetch_bucket(key) if existing_bucket existing_bucket.expires_at = relative_expires_at(opts[:expires_in]) existing_bucket.value += value else @storage[key] = DataBucket.new(value, relative_expires_at(opts[:expires_in])) value end end end |
#key?(key) ⇒ Boolean
44 45 46 |
# File 'lib/httpigeon/circuit_breaker/memory_store.rb', line 44 def key?(key) @mutex.synchronize { !fetch_bucket(key).nil? } end |
#reset! ⇒ Object Also known as: clear!
53 54 55 |
# File 'lib/httpigeon/circuit_breaker/memory_store.rb', line 53 def reset! @mutex.synchronize { @storage.clear } end |
#set(key, value, opts = {}) ⇒ Object Also known as: store
19 20 21 22 23 24 25 26 |
# File 'lib/httpigeon/circuit_breaker/memory_store.rb', line 19 def set(key, value, opts = {}) @mutex.synchronize do flush(key) @storage[key] = DataBucket.new(value, relative_expires_at(opts[:expires_in])) value end end |
#storage ⇒ Object
58 59 60 |
# File 'lib/httpigeon/circuit_breaker/memory_store.rb', line 58 def storage @mutex.synchronize { @storage.dup } end |