Skip to content

Commit

Permalink
Replace Time.now with monotonic in lru cache and resource update
Browse files Browse the repository at this point in the history
  • Loading branch information
miry committed Nov 12, 2022
1 parent 39b2818 commit 67abc55
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Avoid prepending the same prefix twice to errors messages. (#423)
This mostly happens with the `redis-rb 5+` gem as it translate `redis-client` errors.
* Fix running tests in DEBUG mode to test missing semaphores resources. (#430)
* Refactor: Replace Time.now with CLOCK_MONOTONIC in Resource updated_at field (#???)

# v0.16.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ There are some global configuration options that can be set for Semian:
# 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)
# Minimum time in seconds a resource should be resident in the LRU cache (default: 300s)
Semian.minimum_lru_time = 60
```

Expand Down
2 changes: 1 addition & 1 deletion lib/semian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module Semian
attr_accessor :maximum_lru_size, :minimum_lru_time, :default_permissions, :namespace

self.maximum_lru_size = 500
self.minimum_lru_time = 300
self.minimum_lru_time = 300 # 300 seconds / 5 minutes
self.default_permissions = 0660

def issue_disabled_semaphores_warning
Expand Down
10 changes: 5 additions & 5 deletions lib/semian/lru_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def clear
#
# Arguments:
# +max_size+ The maximum size of the table
# +min_time+ The minimum time a resource can live in the cache
# +min_time+ The minimum time in seconds a resource can live in the cache
#
# Note:
# The +min_time+ is a stronger guarantee than +max_size+. That is, if there are
Expand Down Expand Up @@ -83,7 +83,7 @@ def set(key, resource)
@lock.synchronize do
@table.delete(key)
@table[key] = resource
resource.updated_at = Time.now
resource.updated_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
clear_unused_resources if @table.length > @max_size
end
Expand All @@ -98,7 +98,7 @@ def get(key)
found = @table.delete(key)
if found
@table[key] = found
found.updated_at = Time.now
found.updated_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
found
end
Expand Down Expand Up @@ -130,9 +130,9 @@ def clear_unused_resources
timer_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

ran = try_synchronize do
# Clears resources that have not been used in the last 5 minutes.
# Clears resources that have not been used in the last 5 minutes (default value of Semian.minimum_lru_time).

stop_time = Time.now - @min_time # Don't process resources updated after this time
stop_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @min_time # Don't process resources updated after this time
@table.each do |_, resource|
payload[:examined] += 1

Expand Down
2 changes: 1 addition & 1 deletion lib/semian/protected_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(name, bulkhead, circuit_breaker)
@name = name
@bulkhead = bulkhead
@circuit_breaker = circuit_breaker
@updated_at = Time.now
@updated_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def destroy
Expand Down
2 changes: 1 addition & 1 deletion lib/semian/unprotected_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UnprotectedResource

def initialize(name)
@name = name
@updated_at = Time.now
@updated_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def registered_workers
Expand Down
2 changes: 1 addition & 1 deletion test/lru_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_clean_instrumentation
end

def test_monotonically_increasing
start_time = Time.now
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

notification = 0
subscriber = Semian.subscribe do |event, _resource, _scope, _adapter, payload|
Expand Down

0 comments on commit 67abc55

Please sign in to comment.