turned BandwidthLimiter into an interface, removed some of its teeth, and cleaned up TrivialBandwidthLimiter

This commit is contained in:
jrandom
2004-06-20 00:56:04 +00:00
committed by zzz
parent f25bccd19f
commit 7da0cee29a
2 changed files with 74 additions and 77 deletions

View File

@ -13,76 +13,30 @@ import net.i2p.router.RouterContext;
import net.i2p.util.Log; import net.i2p.util.Log;
/** /**
* Coordinate the bandwidth limiting across all classes of peers. Currently * Coordinate the bandwidth limiting across all classes of peers.
* treats everything as open (aka doesn't limit)
* *
*/ */
public class BandwidthLimiter { public interface BandwidthLimiter {
private Log _log;
protected RouterContext _context;
protected Object _outboundWaitLock = new Object();
protected Object _inboundWaitLock = new Object();
protected BandwidthLimiter(RouterContext context) {
_context = context;
_log = context.logManager().getLog(BandwidthLimiter.class);
}
public long getTotalSendBytes() { return 0; }
public long getTotalReceiveBytes() { return 0; }
/**
* Return how many milliseconds to wait before receiving/processing numBytes from the peer
*/
public long calculateDelayInbound(RouterIdentity peer, int numBytes) {
return 0;
}
/**
* Return how many milliseconds to wait before sending numBytes to the peer
*/
public long calculateDelayOutbound(RouterIdentity peer, int numBytes) {
return 0;
}
/**
* Note that numBytes have been read from the peer
*/
public void consumeInbound(RouterIdentity peer, int numBytes) {}
/**
* Note that numBytes have been sent to the peer
*/
public void consumeOutbound(RouterIdentity peer, int numBytes) {}
/** /**
* Delay the required amount of time before returning so that receiving numBytes * Delay the required amount of time before returning so that receiving numBytes
* from the peer will not violate the bandwidth limits * from the peer will not violate the bandwidth limits
*/ */
public void delayInbound(RouterIdentity peer, int numBytes) { public void delayInbound(RouterIdentity peer, int numBytes);
while (calculateDelayInbound(peer, numBytes) > 0) {
try {
synchronized (_inboundWaitLock) {
_inboundWaitLock.wait(10*1000);
}
} catch (InterruptedException ie) {}
}
synchronized (_inboundWaitLock) { _inboundWaitLock.notify(); }
consumeInbound(peer, numBytes);
}
/** /**
* Delay the required amount of time before returning so that sending numBytes * Delay the required amount of time before returning so that sending numBytes
* to the peer will not violate the bandwidth limits * to the peer will not violate the bandwidth limits
*/ */
public void delayOutbound(RouterIdentity peer, int numBytes) { public void delayOutbound(RouterIdentity peer, int numBytes);
while (calculateDelayOutbound(peer, numBytes) > 0) {
try { public long getTotalSendBytes();
synchronized (_outboundWaitLock) { public long getTotalReceiveBytes();
_outboundWaitLock.wait(10*1000);
}
} catch (InterruptedException ie) {} static final String PROP_INBOUND_BANDWIDTH = "i2np.bandwidth.inboundKBytesPerSecond";
} static final String PROP_OUTBOUND_BANDWIDTH = "i2np.bandwidth.outboundKBytesPerSecond";
synchronized (_outboundWaitLock) { _outboundWaitLock.notify(); } static final String PROP_INBOUND_BANDWIDTH_PEAK = "i2np.bandwidth.inboundBurstKBytes";
consumeOutbound(peer, numBytes); static final String PROP_OUTBOUND_BANDWIDTH_PEAK = "i2np.bandwidth.outboundBurstKBytes";
} static final String PROP_REPLENISH_FREQUENCY = "i2np.bandwidth.replenishFrequency";
static final String PROP_MIN_NON_ZERO_DELAY = "i2np.bandwidth.minimumNonZeroDelay";
} }

View File

@ -18,8 +18,10 @@ import net.i2p.util.I2PThread;
* treats everything as open (aka doesn't limit) * treats everything as open (aka doesn't limit)
* *
*/ */
public class TrivialBandwidthLimiter extends BandwidthLimiter { public class TrivialBandwidthLimiter implements BandwidthLimiter {
private Log _log; private Log _log;
private RouterContext _context;
/** how many bytes can we read from the network without blocking? */ /** how many bytes can we read from the network without blocking? */
private volatile long _inboundAvailable; private volatile long _inboundAvailable;
/** how many bytes can we write to the network without blocking? */ /** how many bytes can we write to the network without blocking? */
@ -54,19 +56,24 @@ public class TrivialBandwidthLimiter extends BandwidthLimiter {
*/ */
private Object _updateBwLock = new Object(); private Object _updateBwLock = new Object();
final static String PROP_INBOUND_BANDWIDTH = "i2np.bandwidth.inboundKBytesPerSecond"; /**
final static String PROP_OUTBOUND_BANDWIDTH = "i2np.bandwidth.outboundKBytesPerSecond"; * wait on this when we want outbound bytes, and notify
final static String PROP_INBOUND_BANDWIDTH_PEAK = "i2np.bandwidth.inboundBurstKBytes"; * it when more bytes are available
final static String PROP_OUTBOUND_BANDWIDTH_PEAK = "i2np.bandwidth.outboundBurstKBytes"; */
final static String PROP_REPLENISH_FREQUENCY = "i2np.bandwidth.replenishFrequency"; private Object _outboundWaitLock = new Object();
final static String PROP_MIN_NON_ZERO_DELAY = "i2np.bandwidth.minimumNonZeroDelay"; /**
final static long DEFAULT_REPLENISH_FREQUENCY = 1*1000; * wait on this when we want inbound bytes, and notify
final static long DEFAULT_MIN_NON_ZERO_DELAY = 1*1000; * it when more bytes are available
*/
private Object _inboundWaitLock = new Object();
final static long UPDATE_LIMIT_FREQUENCY = 60*1000; private static final long DEFAULT_REPLENISH_FREQUENCY = 1*1000;
private static final long DEFAULT_MIN_NON_ZERO_DELAY = 1*1000;
private static final long UPDATE_LIMIT_FREQUENCY = 60*1000;
public TrivialBandwidthLimiter(RouterContext ctx) { public TrivialBandwidthLimiter(RouterContext ctx) {
super(ctx); _context = ctx;
_log = ctx.logManager().getLog(TrivialBandwidthLimiter.class); _log = ctx.logManager().getLog(TrivialBandwidthLimiter.class);
_inboundAvailable = 0; _inboundAvailable = 0;
_outboundAvailable = 0; _outboundAvailable = 0;
@ -87,13 +94,49 @@ public class TrivialBandwidthLimiter extends BandwidthLimiter {
bwThread.start(); bwThread.start();
} }
/**
* Delay the required amount of time before returning so that receiving numBytes
* from the peer will not violate the bandwidth limits
*/
public void delayInbound(RouterIdentity peer, int numBytes) {
long delay = 0;
while ( (delay = calculateDelayInbound(peer, numBytes)) > 0) {
try {
synchronized (_inboundWaitLock) {
_inboundWaitLock.wait(delay);
}
} catch (InterruptedException ie) {}
}
synchronized (_inboundWaitLock) { _inboundWaitLock.notify(); }
consumeInbound(peer, numBytes);
}
/**
* Delay the required amount of time before returning so that sending numBytes
* to the peer will not violate the bandwidth limits
*/
public void delayOutbound(RouterIdentity peer, int numBytes) {
long delay = 0;
while ( (delay = calculateDelayOutbound(peer, numBytes)) > 0) {
try {
synchronized (_outboundWaitLock) {
_outboundWaitLock.wait(delay);
}
} catch (InterruptedException ie) {}
}
synchronized (_outboundWaitLock) { _outboundWaitLock.notify(); }
consumeOutbound(peer, numBytes);
}
public long getTotalSendBytes() { return _totalOutboundBytes; } public long getTotalSendBytes() { return _totalOutboundBytes; }
public long getTotalReceiveBytes() { return _totalInboundBytes; } public long getTotalReceiveBytes() { return _totalInboundBytes; }
/** /**
* Return how many milliseconds to wait before receiving/processing numBytes from the peer * Return how many milliseconds to wait before receiving/processing numBytes from the peer
*/ */
public long calculateDelayInbound(RouterIdentity peer, int numBytes) { private long calculateDelayInbound(RouterIdentity peer, int numBytes) {
if (_inboundKBytesPerSecond <= 0) return 0; if (_inboundKBytesPerSecond <= 0) return 0;
if (_inboundAvailable - numBytes > 0) { if (_inboundAvailable - numBytes > 0) {
// we have bytes available // we have bytes available
@ -118,7 +161,7 @@ public class TrivialBandwidthLimiter extends BandwidthLimiter {
/** /**
* Return how many milliseconds to wait before sending numBytes to the peer * Return how many milliseconds to wait before sending numBytes to the peer
*/ */
public long calculateDelayOutbound(RouterIdentity peer, int numBytes) { private long calculateDelayOutbound(RouterIdentity peer, int numBytes) {
if (_outboundKBytesPerSecond <= 0) return 0; if (_outboundKBytesPerSecond <= 0) return 0;
if (_outboundAvailable - numBytes > 0) { if (_outboundAvailable - numBytes > 0) {
// we have bytes available // we have bytes available
@ -145,7 +188,7 @@ public class TrivialBandwidthLimiter extends BandwidthLimiter {
/** /**
* Note that numBytes have been read from the peer * Note that numBytes have been read from the peer
*/ */
public void consumeInbound(RouterIdentity peer, int numBytes) { private void consumeInbound(RouterIdentity peer, int numBytes) {
if (numBytes > 0) if (numBytes > 0)
_totalInboundBytes += numBytes; _totalInboundBytes += numBytes;
if (_inboundKBytesPerSecond > 0) if (_inboundKBytesPerSecond > 0)
@ -155,7 +198,7 @@ public class TrivialBandwidthLimiter extends BandwidthLimiter {
/** /**
* Note that numBytes have been sent to the peer * Note that numBytes have been sent to the peer
*/ */
public void consumeOutbound(RouterIdentity peer, int numBytes) { private void consumeOutbound(RouterIdentity peer, int numBytes) {
if (numBytes > 0) if (numBytes > 0)
_totalOutboundBytes += numBytes; _totalOutboundBytes += numBytes;
if (_outboundKBytesPerSecond > 0) if (_outboundKBytesPerSecond > 0)