orangesoft/throttler

Load balancer between nodes.

2.0.4 2023-03-30 18:19 UTC

This package is auto-updated.

Last update: 2024-03-27 22:19:35 UTC


README

Build Status Latest Stable Version Packagist PHP Version Support Total Downloads License

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 Throttler as below:

<?php

use Orangesoft\Throttler\Collection\Node;
use Orangesoft\Throttler\Collection\Collection;
use Orangesoft\Throttler\Strategy\WeightedRoundRobinStrategy;
use Orangesoft\Throttler\Strategy\InMemoryCounter;
use Orangesoft\Throttler\Throttler;

$throttler = new Throttler(
    new WeightedRoundRobinStrategy(
        new InMemoryCounter(start: 0),
    )
);

$collection = new Collection([
    new Node('node1', 5),
    new Node('node2', 1),
    new Node('node3', 1),
]);

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

Set weight for Node as the second argument in constructor if you are using weighted-strategies.

Benchmarks

Run composer phpbench to check out benchmarks:

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

The report is based on measuring the speed. Check best column to find out which strategy is the fastest. You can see that the fastest strategies are Random and RoundRobin.

Documentation

Read more about usage on Orangesoft Tech.