Skip to content

Commit

Permalink
Merge pull request #201 from matthieuprat/mprat/resgistry-thread-safety
Browse files Browse the repository at this point in the history
Make all registry methods thread safe
  • Loading branch information
Daniel Magliola committed Oct 7, 2020
2 parents 872a8eb + c3836b2 commit 726536c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/prometheus/client/registry.rb
Expand Up @@ -22,7 +22,7 @@ def register(metric)
name = metric.name

@mutex.synchronize do
if exist?(name.to_sym)
if @metrics.key?(name.to_sym)
raise AlreadyRegisteredError, "#{name} has already been registered"
end
@metrics[name.to_sym] = metric
Expand Down Expand Up @@ -73,15 +73,15 @@ def histogram(name, docstring:, labels: [], preset_labels: {},
end

def exist?(name)
@metrics.key?(name)
@mutex.synchronize { @metrics.key?(name) }
end

def get(name)
@metrics[name.to_sym]
@mutex.synchronize { @metrics[name.to_sym] }
end

def metrics
@metrics.values
@mutex.synchronize { @metrics.values }
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions spec/prometheus/client/registry_spec.rb
Expand Up @@ -33,10 +33,6 @@
mutex = Mutex.new
containers = []

def registry.exist?(*args)
super.tap { sleep(0.01) }
end

Array.new(5) do
Thread.new do
result = begin
Expand Down

0 comments on commit 726536c

Please sign in to comment.