Permalink
Browse files

make metrics access threadsafe

  • Loading branch information...
1 parent ec45d8a commit 711e5e2dd86c08a2397aaa04be17a84ed5e8a29f @jordansissel committed Jun 7, 2012
Showing with 7 additions and 2 deletions.
  1. +7 −2 lib/cabin/metrics.rb
View
@@ -51,6 +51,7 @@ class Cabin::Metrics
# Get us a new metrics container.
public
def initialize
+ @metrics_lock = Mutex.new
@metrics = {}
end # def initialize
@@ -76,7 +77,9 @@ def create(instance, name, metric_object)
if @channel
@channel.debug("Created metric", :instance => instance, :type => metric_object.class)
end
- return @metrics[metric_name] = metric_object
+ return @metrics_lock.synchronize do
+ @metrics[metric_name] = metric_object
+ end
end # def create
# Create a new Counter metric
@@ -114,6 +117,8 @@ def timer(instance, name=nil)
# iterate over each metric. yields identifer, metric
def each(&block)
# delegate to the @metrics hash until we need something fancier
- @metrics.each(&block)
+ @metrics_lock.synchronize do
+ @metrics.each(&block)
+ end
end # def each
end # class Cabin::Metrics

0 comments on commit 711e5e2

Please sign in to comment.