java.lang.Object
org.klomp.snark.BandwidthManager
- All Implemented Interfaces:
BandwidthListener
Bandwidth and bandwidth limits.
Maintain three bandwidth estimators: Sent, received, and requested.
There are three layers of BandwidthListeners:
BandwidthManager (total)
PeerCoordinator (per-torrent)
Peer/WebPeer (per-connection)
Here at the top, we use SyntheticRedQueues for accurate and current moving averages of up, down, and requested bandwidth.At the lower layers, simple weighted moving averages of three buckets of time PeerCoordinator.CHECK_PERIOD each are used for up and down, and requested is delegated here.
The lower layers must report to the next-higher layer.
At the Peer layer, we report inbound piece data per-read, not per-piece, to get a smoother inbound estimate.
Only the following data are counted by the BandwidthListeners:
- Pieces (both Peer and WebPeer)
- ut_metadata
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final I2PAppContextThe context the limiter runs in.private SyntheticREDQueueGate for downloads.private final LogLogger for this class.private SyntheticREDQueueGate for metadata requests.private SyntheticREDQueueGate for uploads.Constructor Summary
ConstructorsConstructorDescriptionBandwidthManager(I2PAppContext ctx, int upLimit, int downLimit) Create the bandwidth manager.Method Summary
Public MethodsModifier and TypeMethodDescriptionvoiddownloaded(int size) We received this many bytes.longCurrent limit in BPS.longThe average rate in Bps.(package private) longThe average rate in Bps.longCurrent limit in BPS.longThe average rate in Bps.booleanAre we currently over the limit?booleanAre we currently over the limit?(package private) voidsetDownBWLimit(long downLimit) Current limit in Bps.(package private) voidsetUpBWLimit(long upLimit) Current limit in Bps.booleanshouldRequest(Peer peer, int size) Should we request this many bytes?booleanshouldSend(int size) Should we send this many bytes? Do NOT calluploaded(int)if this returns true.toString()In HTML for debug page.voiduploaded(int size) We unconditionally sent this many bytes.
Field Details
_context
The context the limiter runs in._log
Logger for this class._up
Gate for uploads._down
Gate for downloads._req
Gate for metadata requests.
Constructor Details
BandwidthManager
BandwidthManager(I2PAppContext ctx, int upLimit, int downLimit) Create the bandwidth manager.- Parameters:
ctx- the app contextupLimit- the upload limit in BpsdownLimit- the download limit in Bps
Method Details
setUpBWLimit
void setUpBWLimit(long upLimit) Current limit in Bps.- Parameters:
upLimit- the new upload limit in Bps, truncated to Integer.MAX_VALUE
setDownBWLimit
void setDownBWLimit(long downLimit) Current limit in Bps.- Parameters:
downLimit- the new download limit in Bps, truncated to Integer.MAX_VALUE
getRequestRate
long getRequestRate()The average rate in Bps.- Returns:
- the estimated rate at which peers request data from us, in Bps
getUploadRate
public long getUploadRate()The average rate in Bps.- Specified by:
getUploadRatein interfaceBandwidthListener- Returns:
- the average upload rate in Bps
getDownloadRate
public long getDownloadRate()The average rate in Bps.- Specified by:
getDownloadRatein interfaceBandwidthListener- Returns:
- the average download rate in Bps
uploaded
public void uploaded(int size) We unconditionally sent this many bytes.- Specified by:
uploadedin interfaceBandwidthListener- Parameters:
size- the number of bytes sent
downloaded
public void downloaded(int size) We received this many bytes.- Specified by:
downloadedin interfaceBandwidthListener- Parameters:
size- the number of bytes received
shouldSend
public boolean shouldSend(int size) Should we send this many bytes? Do NOT calluploaded(int)if this returns true.- Specified by:
shouldSendin interfaceBandwidthListener- Parameters:
size- the number of bytes to send- Returns:
- true if the bytes should be sent, false otherwise
shouldRequest
Should we request this many bytes?- Specified by:
shouldRequestin interfaceBandwidthListener- Parameters:
peer- ignored; both rate limiters are consulted regardlesssize- the number of bytes to request- Returns:
- true if both the request and download limiters accept it
getUpBWLimit
public long getUpBWLimit()Current limit in BPS.- Specified by:
getUpBWLimitin interfaceBandwidthListener- Returns:
- the upload limit in Bps
getDownBWLimit
public long getDownBWLimit()Current limit in BPS.- Specified by:
getDownBWLimitin interfaceBandwidthListener- Returns:
- the download limit in Bps
overUpBWLimit
public boolean overUpBWLimit()Are we currently over the limit?- Specified by:
overUpBWLimitin interfaceBandwidthListener- Returns:
- true if over the upload limit, false otherwise
overDownBWLimit
public boolean overDownBWLimit()Are we currently over the limit?- Specified by:
overDownBWLimitin interfaceBandwidthListener- Returns:
- true if over the download limit, false otherwise
toString