From b56abcaad218e4295fe48b86eca51a068524ee8c Mon Sep 17 00:00:00 2001 From: Aleksandr Denisyuk Date: Thu, 23 Dec 2021 11:40:54 +0300 Subject: [PATCH] Update docs --- docs/index.md | 179 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 132 insertions(+), 47 deletions(-) diff --git a/docs/index.md b/docs/index.md index 96f11c9..7081ec2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -5,44 +5,45 @@ - [Sort nodes](#sort-nodes) - [Keep counter](#keep-counter) - [Serialize strategies](#serialize-strategies) +- [Dynamically change strategy](#dynamically-change-strategy) +- [Balance cluster](#balance-cluster) ## Configure Throttler -You need to collect a collection of nodes and choose a strategy. Set weight for Node as the second argument in constructor if you are using weighted-strategies: +You need to choose a strategy and configure it: ```php next(); - - $name = $node->getName(); + /** @var Node $node */ + $node = $throttler->pick($collection); // ... } @@ -75,6 +76,8 @@ Strategies are divided into two types: random and round-robin. The following str - [RoundRobinStrategy](../src/Strategy/RoundRobinStrategy.php) - [WeightedRoundRobinStrategy](../src/Strategy/WeightedRoundRobinStrategy.php) - [SmoothWeightedRoundRobinStrategy](../src/Strategy/SmoothWeightedRoundRobinStrategy.php) +- [MultipleDynamicStrategy](../src/Strategy/MultipleDynamicStrategy.php) +- [ClusterDetermineStrategy](../src/Strategy/ClusterDetermineStrategy.php) ## Sort nodes @@ -84,7 +87,6 @@ For some strategies, such as [FrequencyRandomStrategy](../src/Strategy/Frequency sort($collection, new Desc()); +$sorter->sort($collection, new Desc()); ``` The nodes at the top of the list will be used more often. You can manage sorting using [Asc](../src/Collection/Asc.php) and [Desc](../src/Collection/Desc.php) comparators. Example for the Desc direction: @@ -130,18 +131,15 @@ The nodes at the top of the list will be used more often. You can manage sorting +--------+--------+ ``` -FrequencyRandomStrategy has 2 not required options: frequency and depth. Frequency is probability to choose nodes from a first group in percent. Depth is length the first group from the list in percent. By default frequency is 80 and depth is 20. +FrequencyRandomStrategy has 2 not required options: frequency and depth. Frequency is probability to choose nodes from a first group in percent. Depth is length the first group from the list in percent. By default, frequency is 0.8 and depth is 0.2: ```php -$frequency = 80; -$depth = 20; - -$strategy = new FrequencyRandomStrategy($frequency, $depth); - -$throttler = new Throttler($sortedCollection, $strategy); +$throttler = new Throttler( + new FrequencyRandomStrategy(frequency: 0.8, depth: 0.2) +); -/** @var NodeInterface $node */ -$node = $throttler->next(); +/** @var Node $node */ +$node = $throttler->pick($collection); ``` The probability of choosing nodes for FrequencyRandomStrategy can be visualized as follows: @@ -168,7 +166,7 @@ If you need the reverse order of the nodes use Asc direction. ## Keep counter -For strategies are [RoundRobinStrategy](../src/Strategy/RoundRobinStrategy.php) and [WeightedRoundRobinStrategy](../src/Strategy/WeightedRoundRobinStrategy.php) you must use InMemoryCounter to remember order of nodes. A counter is not needed for round-robin strategies. +For strategies are [RoundRobinStrategy](../src/Strategy/RoundRobinStrategy.php) and [WeightedRoundRobinStrategy](../src/Strategy/WeightedRoundRobinStrategy.php) you must use InMemoryCounter to remember order of nodes. A counter is not needed for round-robin strategies: ```php client = $client; + public function __construct( + private Client $client, + ) { } - public function increment(string $key = 'default'): int + public function next(string $name = 'default'): int { - if (!$this->client->exists($key)) { - $this->client->set($key, -1); + if (!$this->client->exists($name)) { + $this->client->set($name, -1); } - return $this->client->incr($key); + return $this->client->incr($name); } } ``` @@ -212,16 +208,16 @@ In the example above, we wrote the counter with Redis. ```php /** @var Predis\Client $client */ -$counter = new RedisCounter($client); - -$strategy = new WeightedRoundRobinStrategy($counter); +$strategy = new WeightedRoundRobinStrategy( + new RedisCounter($client) +); ``` Now Throttler will resume work from the last node according to the chosen strategy. ## Serialize strategies -[SmoothWeightedRoundRobinStrategy](../src/Strategy/SmoothWeightedRoundRobinStrategy.php) does not supported counters. Instead it you can serialize and unserialize this strategy to keep last state: +[SmoothWeightedRoundRobinStrategy](../src/Strategy/SmoothWeightedRoundRobinStrategy.php) does not support counters. Instead it you can serialize and unserialize this strategy to keep last state: ```php pick($collection, [ + 'strategy_name' => RoundRobinStrategy::class, +]); +``` + +The advantage of this method is that you do not need to create many instances of the balancer. + +## Balance cluster + +You can divide the nodes into clusters and set a specific balancing strategy for each cluster. To do this, configure the [ClusterDetermineStrategy](../src/Strategy/ClusterDetermineStrategy.php) as shown below: + +```php +balance($throttler); +``` + +This method is well suited in cases where the nodes can be divided according to a specific criterion and each cluster needs its own balancing strategy.