项目作者: Orangesoft-Development

项目描述 :
Load balancer between nodes
高级语言: PHP
项目地址: git://github.com/Orangesoft-Development/throttler.git
创建时间: 2021-01-09T14:14:06Z
项目社区:https://github.com/Orangesoft-Development/throttler

开源协议:MIT License

下载


Throttler

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:

  1. 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:

  1. <?php
  2. use Orangesoft\Throttler\Counter\InMemoryCounter;
  3. use Orangesoft\Throttler\Collection\NodeInterface;
  4. use Orangesoft\Throttler\Collection\Node;
  5. use Orangesoft\Throttler\WeightedRoundRobinThrottler;
  6. $throttler = new WeightedRoundRobinThrottler(
  7. new InMemoryCounter(),
  8. );
  9. $collection = new InMemoryCollection([
  10. new Node('192.168.0.1', 5),
  11. new Node('192.168.0.2', 1),
  12. new Node('192.168.0.3', 1),
  13. ]);
  14. while (true) {
  15. /** @var NodeInterface $node */
  16. $node = $throttler->pick($collection);
  17. // ...
  18. }

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

  1. +---------+-------------+
  2. | request | node |
  3. +---------+-------------+
  4. | 1 | 192.168.0.1 |
  5. | 2 | 192.168.0.1 |
  6. | 3 | 192.168.0.1 |
  7. | 4 | 192.168.0.1 |
  8. | 5 | 192.168.0.1 |
  9. | 6 | 192.168.0.2 |
  10. | 7 | 192.168.0.3 |
  11. | n | etc. |
  12. +---------+-------------+

The following throttlers are available:

Benchmarks

Run composer phpbench to check out benchmarks:

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

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.