Class BandwidthThrottle
Stream based bandwidth throtteling.
This class is a facade for the throtteling stream filter
bandwidthThrottle\TokenBucketFilter
and PHP's stream_filter_* functions.
You have to set a rate with bandwidthThrottle\BandwidthThrottle::setRate()
. Then you can
throttle a stream by calling the bandwidthThrottle\BandwidthThrottle::throttle()
method.
After that all operations on that stream are throttled.
Per default the throttle applies for both, input and output streams. If you do use one stream bidirectional (which you probably don't) the effective rate might not be what you expect. both directions will share the same throttle, and therefore in total share the bandwidth. E.g. if you limit to 100KiB/s you could read and write each with 50KiB/s. If this is not what you want consider using dedicated throttles and streams.
The following example will stream a video with a rate of 100KiB/s to the client:
use bandwidthThrottle\BandwidthThrottle; $in = fopen(__DIR__ . "/resources/video.mpg", "r"); $out = fopen("php://output", "w"); $throttle = new BandwidthThrottle(); $throttle->setRate(100, BandwidthThrottle::KIBIBYTES); // Set limit to 100KiB/s $throttle->throttle($out); stream_copy_to_stream($in, $out);
License: WTFPL
Author: Markus Malkusch markus@malkusch.de
Link: Donations
Located at BandwidthThrottle.php
public
|
|
public
|
|
public
|
#
setRate( integer $rate, string $unit =
Sets the rate per second. |
public
|
#
setBurstCapacity( integer $capacity, string $unit =
Sets the burst capacity. |
public
|
#
setInitialBurst( integer $initialBurst, string $unit =
Sets the initial burst size. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
string |
BYTES
Unit for bytes. |
#
"bytes"
|
string |
KILOBYTES
Unit for kilobytes (1000 bytes). |
#
"kilobytes"
|
string |
KIBIBYTES
Unit for kibibytes (1024 bytes). |
#
"kibibytes"
|
string |
MEGABYTES
Unit for megabytes (1000 kilobytes). |
#
"megabytes"
|
string |
MEBIBYTES
Unit for mebibytes (1024 kibibytes). |
#
"mebibytes"
|