summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Leftwich <rleftwich@lightkeeper.com>2012-10-27 11:10:42 (GMT)
committerPatrick Hensley <spaceboy@indirect.com>2013-12-03 21:14:18 (GMT)
commit604609496bee77d8c800c95b10909dd6583e5a26 (patch)
tree6f2ec46266f017e9e1f8289b87b9760406be5b96
parent479826e9bb0a849f37c4a524cc3c86b3c9c59afa (diff)
downloadgstatsd-604609496bee77d8c800c95b10909dd6583e5a26.zip
gstatsd-604609496bee77d8c800c95b10909dd6583e5a26.tar.gz
gstatsd-604609496bee77d8c800c95b10909dd6583e5a26.tar.bz2
Add semaphore to protect stats against reentrancy issues when server is busy
-rw-r--r--gstatsd/service.py39
1 files changed, 21 insertions, 18 deletions
diff --git a/gstatsd/service.py b/gstatsd/service.py
index 4ece952..a77beb2 100644
--- a/gstatsd/service.py
+++ b/gstatsd/service.py
@@ -18,7 +18,9 @@ from core import __version__
# vendor
import gevent, gevent.socket
socket = gevent.socket
-
+# protect stats
+from gevent.thread import allocate_lock as Lock
+stats_lock = Lock()
# constants
INTERVAL = 10.0
@@ -130,9 +132,10 @@ class StatsDaemon(object):
self._reset_stats()
def _reset_stats(self):
- self._stats = Stats()
- self._stats.percent = self._percent
- self._stats.interval = self._interval
+ with stats_lock:
+ self._stats = Stats()
+ self._stats.percent = self._percent
+ self._stats.interval = self._interval
def exit(self, msg, code=1):
self.error(msg)
@@ -201,20 +204,20 @@ class StatsDaemon(object):
value = fields[0]
stype = fields[1].strip()
- # timer (milliseconds)
- if stype == 'ms':
- stats.timers[key].append(float(value if value else 0))
-
- # counter with optional sample rate
- elif stype == 'c':
- if length == 3 and fields[2].startswith('@'):
- srate = float(fields[2][1:])
- value = float(value if value else 1) * (1 / srate)
- stats.counts[key] += value
- elif stype == 'g':
- value = float(value if value else 1)
- stats.gauges[key] = value
-
+ with stats_lock:
+ # timer (milliseconds)
+ if stype == 'ms':
+ stats.timers[key].append(float(value if value else 0))
+
+ # counter with optional sample rate
+ elif stype == 'c':
+ if length == 3 and fields[2].startswith('@'):
+ srate = float(fields[2][1:])
+ value = float(value if value else 1) * (1 / srate)
+ stats.counts[key] += value
+ elif stype == 'g':
+ value = float(value if value else 1)
+ stats.gauges[key] = value
def main():