From 1a467468ff9bc27313a24b797c24f30db80d1083 Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Wed, 5 Nov 2008 17:17:41 +0000 Subject: [PATCH] Don't fill the array with zeroes when we reinitialize the Throttle with the same number of operations as we already had, as that allows a potentially dangerous burst of operations. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5505 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/Throttle.as | 37 +++++++++++++------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/as/com/threerings/util/Throttle.as b/src/as/com/threerings/util/Throttle.as index e90b7d4b5..64d22bef3 100644 --- a/src/as/com/threerings/util/Throttle.as +++ b/src/as/com/threerings/util/Throttle.as @@ -68,7 +68,7 @@ public class Throttle /** * Updates the number of operations for this throttle to a new maximum, retaining the current - * history of operations if the limit is being increased and truncating the oldest operations + * history of operations if the limit is being increased and truncating the newest operations * if the limit is decreased. * * @param operations the new maximum number of operations. @@ -76,24 +76,25 @@ public class Throttle */ public function reinit (operations :int, period :int) :void { - var ops :Array = ArrayUtil.create(operations, 0); - var ii :int; - var oldestOp :int; - if (operations > _ops.length) { - // copy to a larger buffer, leaving zeroes at the beginning - oldestOp = _oldestOp + operations - _ops.length; - ArrayUtil.copy(_ops, 0, ops, 0, _oldestOp); - ArrayUtil.copy(_ops, _oldestOp, ops, oldestOp, _ops.length - _oldestOp); - - } else if (operations < _ops.length) { - // if we're truncating, copy the first (oldest) stamps into ops[0..] - var endCount :int = Math.min(_ops.length - _oldestOp, operations); - ArrayUtil.copy(_ops, _oldestOp, ops, 0, endCount); - ArrayUtil.copy(_ops, 0, ops, endCount, operations - endCount); - _oldestOp = 0; - } - _ops = ops; _period = period; + if (operations != _ops.length) { + var ops :Array = ArrayUtil.create(operations, 0); + + if (operations > _ops.length) { + // copy to a larger buffer, leaving zeroes at the beginning + var oldestOp :int = _oldestOp + operations - _ops.length; + ArrayUtil.copy(_ops, 0, ops, 0, _oldestOp); + ArrayUtil.copy(_ops, _oldestOp, ops, oldestOp, _ops.length - _oldestOp); + + } else { + // if we're truncating, copy the first (oldest) stamps into ops[0..] + var endCount :int = Math.min(_ops.length - _oldestOp, operations); + ArrayUtil.copy(_ops, _oldestOp, ops, 0, endCount); + ArrayUtil.copy(_ops, 0, ops, endCount, operations - endCount); + _oldestOp = 0; + } + _ops = ops; + } } /**