Skip to content

Commit db7215f

Browse files
Michael Kipperpushrax
authored andcommitted
Made maximum_lru_size/minimum_lru_time global configuration options
1 parent 8dd229a commit db7215f

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ The best resource is looking at the [already implemented adapters](#adapters).
102102

103103
### Configuration
104104

105+
There are some global configuration options that can be set for Semian:
106+
107+
```ruby
108+
# Maximum size of the LRU cache (default: 500)
109+
# Note: Setting this to 0 enables aggressive garbage collection.
110+
Semian.maximum_lru_size = 0
111+
112+
# Minimum time a resource should be resident in the LRU cache (default: 300s)
113+
Semian.minimum_lru_time = 60
114+
```
115+
116+
Note: `minimum_lru_time` is a stronger guarantee than `maximum_lru_size`. That
117+
is, if a resource has been updated more recently than `minimum_lru_time` it
118+
will not be garbage collected, even if it would cause the LRU cache to grow
119+
larger than `maximum_lru_size`.
120+
105121
When instantiating a resource it now needs to be configured for Semian. This is
106122
done by passing `semian` as an argument when initializing the client. Examples
107123
built in adapters:

lib/semian.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ module Semian
9090
InternalError = Class.new(BaseError)
9191
OpenCircuitError = Class.new(BaseError)
9292

93+
attr_accessor :maximum_lru_size, :minimum_lru_time
94+
self.maximum_lru_size = 500
95+
self.minimum_lru_time = 300
96+
9397
def issue_disabled_semaphores_warning
9498
return if defined?(@warning_issued)
9599
@warning_issued = true

lib/semian/lru_hash.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class LRUHash
88
extend Forwardable
99
def_delegators :@table, :size, :count, :empty?, :values
1010
attr_reader :table
11-
MAXIMUM_SIZE_OF_LRU = 1000
12-
MINIMUM_TIME_IN_LRU = 300
1311

1412
class NoopMutex
1513
def synchronize(*)
@@ -49,7 +47,7 @@ def locked?
4947
# circuits to disparate endpoints (or your circuit names are bad).
5048
# If the max_size is 0, the garbage collection will be very aggressive and
5149
# potentially computationally expensive.
52-
def initialize(max_size: MAXIMUM_SIZE_OF_LRU, min_time: MINIMUM_TIME_IN_LRU)
50+
def initialize(max_size: Semian.maximum_lru_size, min_time: Semian.minimum_lru_time)
5351
@max_size = max_size
5452
@min_time = min_time
5553
@table = {}

test/lru_hash_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,21 @@ def test_monotonically_increasing
166166

167167
# Before: [a, b, c]
168168
# After: [a, c, b]
169-
Timecop.travel(60) do
169+
Timecop.travel(Semian.minimum_lru_time - 1) do
170170
@lru_hash.get('b')
171171
assert_monotonic.call
172172
end
173173

174174
# Before: [a, c, b]
175175
# After: [a, c, b, d]
176-
Timecop.travel(60) do
176+
Timecop.travel(Semian.minimum_lru_time - 1) do
177177
@lru_hash.set('d', create_circuit_breaker('d'))
178178
assert_monotonic.call
179179
end
180180

181181
# Before: [a, c, b, d]
182182
# After: [b, d, e]
183-
Timecop.travel(LRUHash::MINIMUM_TIME_IN_LRU) do
183+
Timecop.travel(Semian.minimum_lru_time) do
184184
@lru_hash.set('e', create_circuit_breaker('e'))
185185
assert_monotonic.call
186186
end
@@ -195,7 +195,7 @@ def test_max_size
195195
lru_hash.set('c', create_circuit_breaker('c'))
196196
assert_equal 3, lru_hash.table.length
197197

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

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

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

0 commit comments

Comments
 (0)