From b9ab203c0c752c9b720379a1992735d15a017dff Mon Sep 17 00:00:00 2001 From: Jamie Doornbos Date: Tue, 28 Oct 2008 18:00:55 +0000 Subject: [PATCH] Fixed Throttle bug causing any increase in the number of operations to block all message throughput. Fix prevents copying from negative indices of _ops. This caused a NaN entry which always resulted in an effective age of 0 regardless of getTimer(), causing a permanent state of throttling git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5475 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/Throttle.as | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/as/com/threerings/util/Throttle.as b/src/as/com/threerings/util/Throttle.as index 918b54016..86ee81e67 100644 --- a/src/as/com/threerings/util/Throttle.as +++ b/src/as/com/threerings/util/Throttle.as @@ -77,9 +77,12 @@ public class Throttle public function reinit (operations :int, period :int) :void { var ops :Array = ArrayUtil.create(operations, 0); - // copy up to 'operations' of our most recent operations into our new array - for (var ii :int = 0; ii < operations; ii++) { - ops[operations-ii-1] = _ops[(_oldestOp + _ops.length - ii - 1) % _ops.length]; + // copy as much as we can of our most recent operations into our new array + var copyCount :int = Math.min(operations, _ops.length); + var srcOffset :int = _oldestOp + _ops.length*2 - 1; // need *2 to prevent copying _ops[-1] + var destOffset :int = operations-1; + for (var ii :int = 0; ii < copyCount; ii++) { + ops[destOffset-ii] = _ops[(srcOffset-ii) % _ops.length]; } _oldestOp = Math.max(0, operations-_ops.length); _ops = ops; @@ -160,6 +163,19 @@ public class Throttle return _ops.length + " ops per " + _period + "ms (oldest " + oldest + ")"; } + /** + * For debugging, includes the age of all operations. + */ + public function opsToString () :String + { + var now :int = getTimer(); + var ages :Array = [] + for (var ii :int = 0; ii < _ops.length; ++ii) { + ages.push(now - _ops[(_oldestOp + ii) % _ops.length]); + } + return "[" + StringUtil.toString(ages) + "] [Oldest idx = " + _oldestOp + "]"; + } + protected var _ops :Array; protected var _oldestOp :int; protected var _period :int;