Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

denisyukphp/throttler

Repository files navigation

Throttler

Load balancer between nodes.

Installation

You can install the latest version via Composer:

composer require orangesoft/throttler

This package requires PHP 8.1 or later.

Quick usage

Configure Orangesoft\Throttler\WeightedRoundRobinThrottler::class as below and set weight for each node if you are using weighted strategy:

pick($collection); // ... }">

use Orangesoft\Throttler\Counter\InMemoryCounter;
use Orangesoft\Throttler\Collection\NodeInterface;
use Orangesoft\Throttler\Collection\Node;
use Orangesoft\Throttler\WeightedRoundRobinThrottler;

$throttler = new WeightedRoundRobinThrottler(
new InMemoryCounter(),
);

$collection = new InMemoryCollection([
new Node('192.168.0.1', 5),
new Node('192.168.0.2', 1),
new Node('192.168.0.3', 1),
]);

while (true) {
/** @var NodeInterface $node */
$node = $throttler->pick($collection);

// ...
}

As a result, the throttler will go through all the nodes and return the appropriate one according to the chosen strategy as shown below:

+---------+-------------+
| request | node |
+---------+-------------+
| 1 | 192.168.0.1 |
| 2 | 192.168.0.1 |
| 3 | 192.168.0.1 |
| 4 | 192.168.0.1 |
| 5 | 192.168.0.1 |
| 6 | 192.168.0.2 |
| 7 | 192.168.0.3 |
| n | etc. |
+---------+-------------+

The following throttlers are available:

Benchmarks

Run composer phpbench to check out benchmarks:

+-------------------------------+------+-----+----------+----------+----------+---------+
| benchmark | revs | its | mean | best | worst | stdev |
+-------------------------------+------+-----+----------+----------+----------+---------+
| RandomBench | 1000 | 5 | 4.002ms | 3.880ms | 4.097ms | 0.073ms |
| WeightedRandomBench | 1000 | 5 | 11.660ms | 11.533ms | 11.797ms | 0.094ms |
| FrequencyRandomBench | 1000 | 5 | 6.074ms | 5.924ms | 6.242ms | 0.139ms |
| RoundRobinBench | 1000 | 5 | 4.060ms | 3.888ms | 4.363ms | 0.171ms |
| WeightedRoundRobinBench | 1000 | 5 | 10.778ms | 10.655ms | 10.919ms | 0.115ms |
| SmoothWeightedRoundRobinBench | 1000 | 5 | 6.888ms | 6.707ms | 7.102ms | 0.130ms |
+-------------------------------+------+-----+----------+----------+----------+---------+

The report is based on measuring the speed. Check best column to find out which strategy is the fastest.

Documentation

Read more about load balancing on Sam Rose's blog.

About

Load balancer between nodes.

Topics

Resources

Readme

License

MIT license

Stars

Watchers

Forks

Contributors

Languages