Overview

Namespaces

  • bandwidthThrottle

Classes

  • BandwidthThrottle
  • TokenBucketFilter

Exceptions

  • BandwidthThrottleException
  • Overview
  • Namespace
  • Class

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);
Namespace: bandwidthThrottle
License: WTFPL
Author: Markus Malkusch markus@malkusch.de
Link: Donations
Located at BandwidthThrottle.php
Methods summary
public
# __construct( )

Initialization.

Initialization.

public
# setStorage( bandwidthThrottle\tokenBucket\storage\Storage $storage )

Sets the storage.

Sets the storage.

The storage determines the scope of the throttle. Setting the storage is optional. The default storage is limited to the request scope. I.e. it will throttle the bandwidth per request.

Parameters

$storage
The storage.
public
# setRate( integer $rate, string $unit = bandwidthThrottle\BandwidthThrottle::BYTES )

Sets the rate per second.

Sets the rate per second.

Parameters

$rate
The rate per second.
$unit
The unit for the rate, default is bytes.

Throws

InvalidArgumentException
The unit was invalid.
public
# setBurstCapacity( integer $capacity, string $unit = bandwidthThrottle\BandwidthThrottle::BYTES )

Sets the burst capacity.

Sets the burst capacity.

Setting the burst capacity is optional. If no capacity was set, the capacity is set to the amount of bytes for one second.

Parameters

$capacity
The burst capacity.
$unit
The unit for the capacity, default is bytes.

Throws

InvalidArgumentException
The unit was invalid.
public
# setInitialBurst( integer $initialBurst, string $unit = bandwidthThrottle\BandwidthThrottle::BYTES )

Sets the initial burst size.

Sets the initial burst size.

This size determines how many bytes can be send instantly after the throttle was activated without limiting the rate.

Setting this size is optional. Default is 0.

Parameters

$initialBurst
The initial burst size.
$unit
The unit for the burst size, default is bytes.

Throws

InvalidArgumentException
The unit was invalid.
public
# setThrottleInputStream( )

Throttles only the input stream.

Throttles only the input stream.

Default is throtteling both streams.

public
# setThrottleOutputStream( )

Throttles only the output stream.

Throttles only the output stream.

Default is throtteling both streams.

public
# setThrottleBothStreams( )

Throttles the output and input stream.

Throttles the output and input stream.

This is the default mode.

public
# throttle( resource $stream )

Throttles a stream to the given rate.

Throttles a stream to the given rate.

This registers a filter to the given stream which does the traffic shaping. After that any stream operation is throttled.

The stream can be an input or an output stream.

This object can throttle only one stream at a time. If you want to call throttle() again, make either sure you called bandwidthThrottle\BandwidthThrottle::unthrottle() before or use a new instance.

Parameters

$stream
The stream.

Throws

bandwidthThrottle\BandwidthThrottleException
Error during throtteling the stream.
LengthException
The initial burst size was greater than the burst size.
public
# unthrottle( )

Unthrottles a previously throttled stream.

Unthrottles a previously throttled stream.

If the throttle was not applied to a stream, this method returns silenty.

Throws

bandwidthThrottle\BandwidthThrottleException
The throttle could not be removed.
Constants summary
string BYTES

Unit for bytes.

Unit for bytes.

# "bytes"
string KILOBYTES

Unit for kilobytes (1000 bytes).

Unit for kilobytes (1000 bytes).

# "kilobytes"
string KIBIBYTES

Unit for kibibytes (1024 bytes).

Unit for kibibytes (1024 bytes).

# "kibibytes"
string MEGABYTES

Unit for megabytes (1000 kilobytes).

Unit for megabytes (1000 kilobytes).

# "megabytes"
string MEBIBYTES

Unit for mebibytes (1024 kibibytes).

Unit for mebibytes (1024 kibibytes).

# "mebibytes"
API documentation generated by ApiGen