From 2045c0d5637e4c0bba30fb03b248f15220e03509 Mon Sep 17 00:00:00 2001 From: "par.winzell" Date: Wed, 5 Nov 2008 00:18:21 +0000 Subject: [PATCH] When we reinitialize the Throttle with a smaller number of allowed operations within a time period, we should preserve the oldest rather than the newest ones, or we'll most likely be in immediate and unfair violation of the limit. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2469 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/jdbc/depot/EHCacheAdapter.java | 10 ++++++++++ src/java/com/samskivert/util/Throttle.java | 7 +++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/EHCacheAdapter.java b/src/java/com/samskivert/jdbc/depot/EHCacheAdapter.java index 54c543c8..6df66f39 100644 --- a/src/java/com/samskivert/jdbc/depot/EHCacheAdapter.java +++ b/src/java/com/samskivert/jdbc/depot/EHCacheAdapter.java @@ -43,9 +43,19 @@ public class EHCacheAdapter * cache manager when it knows that Depot and any other clients no longer need it. */ public EHCacheAdapter (CacheManager cachemgr) + { + this(cachemgr, 1000, 300, 20); + } + + public EHCacheAdapter (CacheManager cachemgr, int maxElementsInMemory, + int timeToIdleSeconds, int timeToLiveSeconds) { _cachemgr = cachemgr; + _maxElementsInMemory = maxElementsInMemory; + _timeToIdleSeconds = timeToIdleSeconds; + _timeToLiveSeconds = timeToLiveSeconds; + CacheManagerPeerListener listener = _cachemgr.getCachePeerListener(); CacheManagerPeerProvider provider = _cachemgr.getCachePeerProvider(); if ((provider != null) != (listener != null)) { diff --git a/src/java/com/samskivert/util/Throttle.java b/src/java/com/samskivert/util/Throttle.java index a54e57f1..63feac70 100644 --- a/src/java/com/samskivert/util/Throttle.java +++ b/src/java/com/samskivert/util/Throttle.java @@ -81,10 +81,9 @@ public class Throttle System.arraycopy(_ops, _lastOp, ops, lastOp, _ops.length - _lastOp); } else if (operations < _ops.length) { - // copy to a smaller buffer, truncating older operations - int lastOp = (_lastOp + _ops.length - operations) % _ops.length; - int endCount = Math.min(_ops.length - lastOp, operations); - System.arraycopy(_ops, lastOp, ops, 0, endCount); + // if we're truncating, copy the first (oldest) stamps into ops[0..] + int endCount = Math.min(operations, _ops.length - _lastOp); + System.arraycopy(_ops, _lastOp, ops, 0, endCount); System.arraycopy(_ops, 0, ops, endCount, operations - endCount); _lastOp = 0; }