diff --git a/metrics/counter.js b/metrics/counter.js index 9276b82..91dedb6 100644 --- a/metrics/counter.js +++ b/metrics/counter.js @@ -1,6 +1,12 @@ /* * A simple counter object */ + +/* JavaScript uses double-precision FP for all numeric types. + * Perhaps someday we'll have native 64-bit integers that can safely be + * transported via JSON without additional code, but not today. */ +var MAX_COUNTER_VALUE = Math.pow(2, 32); // 4294967296 + var Counter = module.exports = function Counter() { this.count = 0; this.type = 'counter'; @@ -8,12 +14,18 @@ var Counter = module.exports = function Counter() { Counter.prototype.inc = function(val) { if (!val) { val = 1; } - this.count += val; + if (this.count === MAX_COUNTER_VALUE) { + this.count = 0; + } else { + this.count += val; + } } Counter.prototype.dec = function(val) { if (!val) { val = 1; } - this.count -= val; + if (this.count > 0) { + this.count -= val; + } } Counter.prototype.clear = function() {