Permalink
Browse files

Merge pull request #551 from iain-buclaw-sociomantic/issue528

Lock writes to MetricCache size and dictionary
  • Loading branch information...
2 parents dfc1d75 + 1a6f4ae commit d4a6a83536acb00cc31c00fe642aa20e6dbf8f7d @obfuscurity obfuscurity committed on GitHub Sep 6, 2016
Showing with 8 additions and 4 deletions.
  1. +8 −4 lib/carbon/cache.py
View
@@ -13,6 +13,7 @@
limitations under the License."""
import time
+import threading
from operator import itemgetter
from random import choice
from collections import defaultdict
@@ -103,6 +104,7 @@ def choose_item(self):
class _MetricCache(defaultdict):
"""A Singleton dictionary of metric names and lists of their datapoints"""
def __init__(self, strategy=None):
+ self.lock = threading.Lock()
self.size = 0
self.strategy = None
if strategy:
@@ -142,8 +144,9 @@ def get_datapoints(self, metric):
return sorted(self.get(metric, {}).items(), key=by_timestamp)
def pop(self, metric):
- datapoint_index = defaultdict.pop(self, metric)
- self.size -= len(datapoint_index)
+ with self.lock:
+ datapoint_index = defaultdict.pop(self, metric)
+ self.size -= len(datapoint_index)
self._check_available_space()
return sorted(datapoint_index.items(), key=by_timestamp)
@@ -156,8 +159,9 @@ def store(self, metric, datapoint):
log.msg("MetricCache is full: self.size=%d" % self.size)
events.cacheFull()
else:
- self.size += 1
- self[metric][timestamp] = value
+ with self.lock:
+ self.size += 1
+ self[metric][timestamp] = value
else:
# Updating a duplicate does not increase the cache size
self[metric][timestamp] = value

0 comments on commit d4a6a83

Please sign in to comment.