diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2018-01-17 19:15:50 +0100 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2018-01-29 15:13:03 +0100 |
commit | d7edc4b46b1f7a2389d4d0fb58aae9e4ff354535 (patch) | |
tree | e8de14a13b740d4b4bc892971542fe7860a07b75 | |
parent | 087c9a5e8f5115170dd0a26470956fd33d48ebb5 (diff) | |
download | gitlab-ce-d7edc4b46b1f7a2389d4d0fb58aae9e4ff354535.tar.gz |
Use Global Mutex, and class instance variables using
```
metric = @_metrics_provider_cache&.[](name)
return metric if metric
``
acccessor that only slightly slower (<20ns) than direct class variable access
-rw-r--r-- | lib/gitlab/metrics/concern.rb | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/lib/gitlab/metrics/concern.rb b/lib/gitlab/metrics/concern.rb index 80a87059f46..fa04aee78ba 100644 --- a/lib/gitlab/metrics/concern.rb +++ b/lib/gitlab/metrics/concern.rb @@ -2,15 +2,11 @@ module Gitlab module Metrics module Concern extend ActiveSupport::Concern - - included do - @_metrics_provider_mutex ||= Mutex.new - @_metrics_provider_cache ||= {} - end + MUTEX = Mutex.new class_methods do def reload_metric!(name) - @_metrics_provider_cache.delete(name) + @_metrics_provider_cache&.delete(name) end private @@ -22,7 +18,8 @@ module Gitlab define_singleton_method(name) do # avoid unnecessary method call to speed up metric access - return @_metrics_provider_cache[name] if @_metrics_provider_cache.has_key?(name) + metric = @_metrics_provider_cache&.[](name) + return metric if metric fetch_metric(type, name, opts, &block) end @@ -30,12 +27,14 @@ module Gitlab def fetch_metric(type, name, opts = {}, &block) # avoid synchronization to speed up metrics access - return @_metrics_provider_cache[name] if @_metrics_provider_cache.has_key?(name) + metric = @_metrics_provider_cache&.[](name) + return metric if metric options = MetricOptions.new(opts) options.evaluate(&block) - @_metrics_provider_mutex.synchronize do + MUTEX.synchronize do + @_metrics_provider_cache ||= {} @_metrics_provider_cache[name] ||= build_metric!(type, name, options) end |