PHPackages                             bandwidth-throttle/bandwidth-throttle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bandwidth-throttle/bandwidth-throttle

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bandwidth-throttle/bandwidth-throttle
=====================================

Bandwidth throttle at application layer

0.1(10y ago)87139.3k↓21.1%6[2 issues](https://github.com/bandwidth-throttle/bandwidth-throttle/issues)WTFPLPHPPHP &gt;=5.5

Since Aug 8Pushed 8y ago4 watchersCompare

[ Source](https://github.com/bandwidth-throttle/bandwidth-throttle)[ Packagist](https://packagist.org/packages/bandwidth-throttle/bandwidth-throttle)[ Docs](https://github.com/bandwidth-throttle/bandwidth-throttle)[ RSS](/packages/bandwidth-throttle-bandwidth-throttle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

Bandwidth Throttle
==================

[](#bandwidth-throttle)

This library implements traffic shaping on streams (input and output streams).

Installation
============

[](#installation)

Use [Composer](https://getcomposer.org/):

```
composer require bandwidth-throttle/bandwidth-throttle
```

Usage
=====

[](#usage)

The package is in the namespace [`bandwidthThrottle`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/namespace-bandwidthThrottle.html).

[`BandwidthThrottle::setRate()`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/class-bandwidthThrottle.BandwidthThrottle.html#_setRate)sets the bandwidth limit. E.g. this would set it to 100KiB/s:

```
$throttle->setRate(100, bandwidthThrottle\BandwidthThrottle::KIBIBYTES);
```

[`BandwidthThrottle::throttle()`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/class-bandwidthThrottle.BandwidthThrottle.html#_throttle)throttles a stream. After that any stream operation (e.g. `fread()`) will be limited to the throttle rate.

Optional methods
----------------

[](#optional-methods)

[`BandwidthThrottle::setBurstCapacity()`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/class-bandwidthThrottle.BandwidthThrottle.html#_setBurstCapacity)sets the burst capacity. This is the capacity which can be accumulated while the stream is not in use. Accumulated capacity can be consumed instantly. Per default this the amount of bytes for one second from the rate.

[`BandwidthThrottle::setInitialBurst()`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/class-bandwidthThrottle.BandwidthThrottle.html#_setInitialBurst)sets the initial burst. Per default the throttle starts with 0 accumulated bytes. Setting an initial burst makes that amount of bytes instantly available.

[`BandwidthThrottle::setStorage()`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/class-bandwidthThrottle.BandwidthThrottle.html#_setStorage)sets the storage for the underlying [token bucket](https://github.com/bandwidth-throttle/token-bucket). The storage determines the scope of the bucket. The default storage is in the request scope. I.e. it will limit the rate per request. There are [storages](http://bandwidth-throttle.github.io/token-bucket/api/class-bandwidthThrottle.tokenBucket.storage.Storage.html)which can be shared amongst requests.

[`BandwidthThrottle::setThrottleBothStreams()`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/class-bandwidthThrottle.BandwidthThrottle.html#_setThrottleBothStreams)will apply the throttle for both input and output streams. This is the default mode.

[`BandwidthThrottle::setThrottleInputStream()`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/class-bandwidthThrottle.BandwidthThrottle.html#_setThrottleInputStream)will apply the throttle for the input stream only.

[`BandwidthThrottle::setThrottleOutputStream()`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/class-bandwidthThrottle.BandwidthThrottle.html#_setThrottleOutputStream)will apply the throttle for the output stream only.

[`BandwidthThrottle::unthrottle()`](http://bandwidth-throttle.github.io/bandwidth-throttle/api/class-bandwidthThrottle.BandwidthThrottle.html#_unthrottle)removes the throttle from the stream.

Example
-------

[](#example)

This example will stream a video with a rate of 100KiB/s to the browser:

```
use bandwidthThrottle\BandwidthThrottle;

$in  = fopen(__DIR__ . "/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);
```

A more sophisticated scenario would be applying multiple throttles on one resource. E.g. the overall bandwidth for the host should be throttled to 1MiB/s and 100KiB/s per request. This will require a shared storage for the 1MiB/s:

```
use bandwidthThrottle\BandwidthThrottle;
use bandwidthThrottle\tokenBucket\storage\FileStorage;

$in  = fopen(__DIR__ . "/video.mpg", "r");
$out = fopen("php://output", "w");

$hostThrottle = new BandwidthThrottle();
$hostThrottle->setRate(1, BandwidthThrottle::MEBIBYTES); // Set limit to 1MiB/s
$hostThrottle->setStorage(new FileStorage(__DIR__ . "/host.throttle"));
$hostThrottle->throttle($out);

$requestThrottle = new BandwidthThrottle();
$requestThrottle->setRate(100, BandwidthThrottle::KIBIBYTES); // Set limit to 100KiB/s
$requestThrottle->throttle($out);

stream_copy_to_stream($in, $out);
```

License and authors
===================

[](#license-and-authors)

This project is free and under the WTFPL. Responsible for this project is Markus Malkusch .

Donations
---------

[](#donations)

If you like this project and feel generous donate a few Bitcoins here: 1335STSwu9hST4vcMRppEPgENMHD2r1REK

[![Build Status](https://camo.githubusercontent.com/e3e20c66e42a4114871345c38ca14f0edc799cb8c253a77fcc91d48d78358f95/68747470733a2f2f7472617669732d63692e6f72672f62616e6477696474682d7468726f74746c652f62616e6477696474682d7468726f74746c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/bandwidth-throttle/bandwidth-throttle)

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community12

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3936d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/97ee86e5e22b92173b2787cad0ba7ac144109e126221c6d21adecdae32ef37fc?d=identicon)[malkusch](/maintainers/malkusch)

---

Top Contributors

[![malkusch](https://avatars.githubusercontent.com/u/1623984?v=4)](https://github.com/malkusch "malkusch (32 commits)")

---

Tags

limitRatedownloadthrottlebandwidth

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bandwidth-throttle-bandwidth-throttle/health.svg)

```
[![Health](https://phpackages.com/badges/bandwidth-throttle-bandwidth-throttle/health.svg)](https://phpackages.com/packages/bandwidth-throttle-bandwidth-throttle)
```

###  Alternatives

[bandwidth-throttle/token-bucket

Implementation of the Token Bucket algorithm.

5121.9M10](/packages/bandwidth-throttle-token-bucket)[florianv/swap

Exchange rates library for PHP

1.3k6.4M16](/packages/florianv-swap)[davedevelopment/stiphle

Simple rate limiting/throttling for php

2567.7M9](/packages/davedevelopment-stiphle)[florianv/laravel-swap

Currency exchange rates library for Laravel and Lumen

3342.0M2](/packages/florianv-laravel-swap)[muffin/throttle

(API) Rate limiting requests in CakePHP

62286.8k](/packages/muffin-throttle)[sunspikes/php-ratelimiter

A framework agnostic rate limiter for PHP

76674.1k1](/packages/sunspikes-php-ratelimiter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
