diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-12-12 01:39:40 +0300 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-12-12 20:38:28 +0300 |
commit | 408208bc2b75d28235092e9bb3821242fdad08fb (patch) | |
tree | 6efb1b69e76bed83caa2dba82050da31fdbd79d3 /lib | |
parent | 5904b033dba553636ae2a06cbf1469d8f19df040 (diff) |
Use AtomicFixNum to implement CAS isolated cache update.
i.e.
Using compare and swap we update the expires_at value.
The thread that actually is able to update this value will also set
the cache holding method_call enabled state
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/metrics/method_call.rb | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/lib/gitlab/metrics/method_call.rb b/lib/gitlab/metrics/method_call.rb index dc56a10957d..2c1cb789314 100644 --- a/lib/gitlab/metrics/method_call.rb +++ b/lib/gitlab/metrics/method_call.rb @@ -2,7 +2,8 @@ module Gitlab module Metrics # Class for tracking timing information about method calls class MethodCall - MEASUREMENT_ENABLED_CACHE = Concurrent::AtomicReference.new({ enabled: false, expires_at: Time.now }) + MEASUREMENT_ENABLED_CACHE = Concurrent::AtomicBoolean.new(false) + MEASUREMENT_ENABLED_CACHE_EXPIRES_AT = Concurrent::AtomicFixnum.new(Time.now.to_i) MUTEX = Mutex.new BASE_LABELS = { module: nil, method: nil }.freeze attr_reader :real_time, :cpu_time, :call_count, :labels @@ -20,18 +21,14 @@ module Gitlab end def call_measurement_enabled? - res = MEASUREMENT_ENABLED_CACHE.update do |cache| - if cache[:expires_at] < Time.now - { - enabled: Feature.get(:prometheus_metrics_method_instrumentation).enabled?, - expires_at: Time.now + 5.minutes - } - else - cache + expires_at = MEASUREMENT_ENABLED_CACHE_EXPIRES_AT.value + if expires_at < Time.now.to_i + if MEASUREMENT_ENABLED_CACHE_EXPIRES_AT.compare_and_set(expires_at, (Time.now + 30.seconds).to_i) + MEASUREMENT_ENABLED_CACHE.value = Feature.get(:prometheus_metrics_method_instrumentation).enabled? end end - res[:enabled] + MEASUREMENT_ENABLED_CACHE.value end # name - The full name of the method (including namespace) such as |