Skip to content

Commit

Permalink
Made maximum_lru_size/minimum_lru_time global configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kipper committed May 31, 2019
1 parent 4f333da commit 0d55fb6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ The best resource is looking at the [already implemented adapters](#adapters).

### Configuration

There are some global configuration options that can be set for Semian:

```ruby
# Maximum size of the LRU cache (default: 500)
# Note: Setting this to 0 enables aggressive garbage collection.
Semian.maximum_lru_size = 0

# Minimum time a resource should be resident in the LRU cache (default: 300s)
Semian.minimum_lru_time = 60
```

Note: `minimum_lru_time` is a stronger guarantee than `maximum_lru_size`. That
is, if a resource has been updated more recently than `minimum_lru_time` it
will not be garbage collected, even if it would cause the LRU cache to grow
larger than `maximum_lru_size`.

When instantiating a resource it now needs to be configured for Semian. This is
done by passing `semian` as an argument when initializing the client. Examples
built in adapters:
Expand Down
4 changes: 4 additions & 0 deletions lib/semian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ module Semian
InternalError = Class.new(BaseError)
OpenCircuitError = Class.new(BaseError)

attr_accessor :maximum_lru_size, :minimum_lru_time
self.maximum_lru_size = 500
self.minimum_lru_time = 300

def issue_disabled_semaphores_warning
return if defined?(@warning_issued)
@warning_issued = true
Expand Down
4 changes: 1 addition & 3 deletions lib/semian/lru_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class LRUHash
extend Forwardable
def_delegators :@table, :size, :count, :empty?, :values
attr_reader :table
MAXIMUM_SIZE_OF_LRU = 1000
MINIMUM_TIME_IN_LRU = 300

class NoopMutex
def synchronize(*)
Expand Down Expand Up @@ -49,7 +47,7 @@ def locked?
# circuits to disparate endpoints (or your circuit names are bad).
# If the max_size is 0, the garbage collection will be very aggressive and
# potentially computationally expensive.
def initialize(max_size: MAXIMUM_SIZE_OF_LRU, min_time: MINIMUM_TIME_IN_LRU)
def initialize(max_size: Semian.maximum_lru_size, min_time: Semian.minimum_lru_time)
@max_size = max_size
@min_time = min_time
@table = {}
Expand Down
12 changes: 6 additions & 6 deletions test/lru_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,21 @@ def test_monotonically_increasing

# Before: [a, b, c]
# After: [a, c, b]
Timecop.travel(60) do
Timecop.travel(Semian.minimum_lru_time - 1) do
@lru_hash.get('b')
assert_monotonic.call
end

# Before: [a, c, b]
# After: [a, c, b, d]
Timecop.travel(60) do
Timecop.travel(Semian.minimum_lru_time - 1) do
@lru_hash.set('d', create_circuit_breaker('d'))
assert_monotonic.call
end

# Before: [a, c, b, d]
# After: [b, d, e]
Timecop.travel(LRUHash::MINIMUM_TIME_IN_LRU) do
Timecop.travel(Semian.minimum_lru_time) do
@lru_hash.set('e', create_circuit_breaker('e'))
assert_monotonic.call
end
Expand All @@ -195,7 +195,7 @@ def test_max_size
lru_hash.set('c', create_circuit_breaker('c'))
assert_equal 3, lru_hash.table.length

Timecop.travel(LRUHash::MINIMUM_TIME_IN_LRU) do
Timecop.travel(Semian.minimum_lru_time) do
# [a, b, c] are older than the min_time, so they get garbage collected.
lru_hash.set('d', create_circuit_breaker('d'))
assert_equal 1, lru_hash.table.length
Expand All @@ -208,14 +208,14 @@ def test_max_size_overflow
lru_hash.set('b', create_circuit_breaker('b'))
assert_equal 2, lru_hash.table.length

Timecop.travel(LRUHash::MINIMUM_TIME_IN_LRU) do
Timecop.travel(Semian.minimum_lru_time) do
# [a, b] are older than the min_time, but the hash isn't full, so
# there's no garbage collection.
lru_hash.set('c', create_circuit_breaker('c'))
assert_equal 3, lru_hash.table.length
end

Timecop.travel(LRUHash::MINIMUM_TIME_IN_LRU + 1) do
Timecop.travel(Semian.minimum_lru_time + 1) do
# [a, b] are beyond the min_time, but [c] isn't.
lru_hash.set('d', create_circuit_breaker('d'))
assert_equal 2, lru_hash.table.length
Expand Down

0 comments on commit 0d55fb6

Please sign in to comment.