BandwidthManager

class BandwidthManager(channel: Int)

Schedules bandwidth fairly across competing consumers. This is a pure-Kotlin port of libtorrent's bandwidth_manager (aux_/bandwidth_manager.hpp, bandwidth_manager.cpp).

There is one manager per direction (channel is upload or download). Consumers call requestBandwidth; if their channels have quota banked the request is granted immediately, otherwise it joins queueSize and is serviced over subsequent updateQuotas rounds. Each round refills every involved BandwidthChannel's token bucket, splits the available quota in proportion to request priorities, and dispatches finished requests through BandwidthSocket.assignBandwidth.

This is the pure scheduling algorithm: the only outside interaction is the BandwidthSocket callback, so it ports without any I/O or timers. The driver decides when to call updateQuotas and with what elapsed time.

Parameters

channel

which direction this manager assigns (upload or download); passed straight back to BandwidthSocket.assignBandwidth.

Constructors

Link copied to clipboard
constructor(channel: Int)

Functions

Link copied to clipboard
fun close()

Shuts the manager down: clears the queue and immediately dispatches every pending request with whatever it had been BwRequest.assigned so far. Faithful to bandwidth_manager::close.

Link copied to clipboard

True if request is currently in the queue. (is_queued)

Link copied to clipboard

Total still-unassigned bytes across all queued requests. (queued_bytes)

Link copied to clipboard
fun queueSize(): Int

Number of requests currently queued. (queue_size)

Link copied to clipboard
fun requestBandwidth(peer: BandwidthSocket, blk: Int, priority: Int, chans: List<BandwidthChannel>): Int

Requests blk bytes for peer across the given chans, at priority (1 = normal). Faithful to bandwidth_manager::request_bandwidth:

Link copied to clipboard
fun updateQuotas(dtMilliseconds: Long)

Runs one scheduling round over an elapsed dtMilliseconds. Faithful to bandwidth_manager::update_quotas (which first converts its time_duration via total_milliseconds):