/** * Constructs a new instance of this class using the given clock to calculate efficiency. * * @param timer the timer to use */ public DynamicThrottlePolicy(Timer timer) { this.timer = timer; this.timeOfLastMessage = timer.milliTime(); }
public RateThrottlingPolicy(double desiredRate, Timer timer) { this.desiredRate = desiredRate; this.timer = timer; currentPeriod = timer.milliTime() / PERIOD; }
public void pushTask(Runnable task, long milliSecondsToWait) { synchronized (tasks) { if (shutdown) { throw new RejectedExecutionException("Tasks can't be scheduled since queue has been shut down."); } tasks.add(new Entry(task, timer.milliTime() + milliSecondsToWait, sequenceCounter++)); tasks.notifyAll(); } }
@Override public boolean canSend(Message message, int pendingCount) { if ( ! super.canSend(message, pendingCount)) { return false; } long time = timer.milliTime(); double elapsed = (time - timeOfLastMessage); if (elapsed > IDLE_TIME_MILLIS) { windowSize = Math.min(windowSize, pendingCount + windowSizeIncrement); } timeOfLastMessage = time; return pendingCount < windowSize; }
public boolean canSend(Message message, int pendingCount) { if (!super.canSend(message, pendingCount)) { return false; } long period = timer.milliTime() / PERIOD; while (currentPeriod < period) { if (allotted > 0) { allotted = 0.0; } allotted = allotted + PERIOD * desiredRate / 1000; currentPeriod++; } if (allotted > 0.0) { allotted -= 1; return true; } return false; } }
/** * If there is a task ready for scheduling, remove it from the queue and return it. * * @return The next task. */ public Runnable popTask() { synchronized (tasks) { Iterator<Entry> iter = tasks.iterator(); if (!iter.hasNext()) { return null; } Entry retEntry = iter.next(); if (retEntry.getTimestamp() > timer.milliTime()) { return null; } iter.remove(); return retEntry.getTask(); } }
long time = timer.milliTime(); double elapsed = time - resizeTime; resizeTime = time;
/** * Closes all unused target connections. Unless the force argument is true, this method will allow a grace period * for all connections after last use before it starts closing them. This allows the most recently used connections * to stay open. * * @param force Whether or not to force flush. */ public synchronized void flushTargets(boolean force) { Iterator<Entry> it = targets.values().iterator(); long currentTime = timer.milliTime(); long expireTime = currentTime - expireMillis; while (it.hasNext()) { Entry entry = it.next(); RPCTarget target = entry.target; if (target.getJRTTarget().isValid()) { if (target.getRefCount() > 1) { entry.lastUse = currentTime; continue; // someone is using this } if (!force) { if (entry.lastUse > expireTime) { continue; // not sufficiently idle } } } target.subRef(); it.remove(); } }
long timeNow = timer.milliTime(); if (retEntry.getTimestamp() > timeNow) { waiting = true;
/** * This method will return a target for the given address. If a target does not currently exist for the given * address, it will be created and added to the internal map. Each target is also reference counted so that an * active target is never expired. * * @param orb The supervisor to use to connect to the target. * @param address The address to resolve to a target. * @return A target for the given address. */ public RPCTarget getTarget(Supervisor orb, RPCServiceAddress address) { Spec spec = address.getConnectionSpec(); String key = spec.toString(); RPCTarget ret; synchronized (this) { Entry entry = targets.get(key); if (entry != null) { if (entry.target.getJRTTarget().isValid()) { entry.target.addRef(); entry.lastUse = timer.milliTime(); return entry.target; } entry.target.subRef(); targets.remove(key); } ret = new RPCTarget(spec, orb); targets.put(key, new Entry(ret, timer.milliTime())); } ret.addRef(); return ret; }