Skip to content
This repository has been archived by the owner on Aug 17, 2022. It is now read-only.

Commit

Permalink
Refactored System
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelandrieu committed May 20, 2019
1 parent 26f9cba commit 5449892
Show file tree
Hide file tree
Showing 20 changed files with 165 additions and 81 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ the circuit breaker:

* the **failures**: define how much times we try to access the service;
* the **timeout**: define how much time we wait before consider the service unreachable;
* the **striped timeout**: define how much time we wait before consider the service unreachable, once we're in half open state;
* the **threshold**: define how much time we wait before trying to access again the service;

We also need to define which (HTTP) client will be used to call the service.
Expand All @@ -37,9 +38,10 @@ use Resiliency\SimpleCircuitBreakerFactory;
$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
$circuitBreaker = $circuitBreakerFactory->create(
[
'closed' => [2, 0.1, 0.1],
'open' => [0, 0.0, 10.0],
'half_open' => [1, 0.2, 0.0],
'failures' => 2,
'timeout' => 0.1,
'stripped_timeout' => 0.2,
'threshold' => 10.0,
'client' => [
'proxy' => '192.168.16.1:10',
'method' => 'POST',
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/CircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function getState(): string;
/**
* The function that execute the service.
*
* @param string $service the service to call
* @param array $serviceParameters the service parameters
* @param callable $fallback if the service is unavailable, rely on the fallback
* @param string $service the service to call
* @param array $serviceParameters the service parameters
* @param callable $fallback if the service is unavailable, rely on the fallback
*
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Client

/**
* @param string $resource the URI of the service to be reached
* @param array $options the options if needed
* @param array $options the options if needed
*
* @return string
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ interface Storage
/**
* Save the CircuitBreaker transaction.
*
* @param string $service The service name
* @param Transaction $transaction the transaction
* @param string $service The service name
* @param Transaction $transaction The transaction
*
* @return bool
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/TransitionDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ interface TransitionDispatcher
* Dispatch a Circuit Breaker transition.
*
* @param CircuitBreaker $circuitBreaker the Circuit Breaker
* @param string $transition the Circuit Breaker transition name
* @param string $service the URI service called
* @param array $parameters the service parameters
* @param string $transition the Circuit Breaker transition name
* @param string $service the URI service called
* @param array $parameters the service parameters
*/
public function dispatch(CircuitBreaker $circuitBreaker, $transition, $service, array $parameters): void;
}
6 changes: 3 additions & 3 deletions src/Events/TransitionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class TransitionEvent extends Event
private $parameters;

/**
* @param string $eventName the transition name
* @param string $service the Service URI
* @param array $parameters the Service parameters
* @param string $eventName the transition name
* @param string $service the Service URI
* @param array $parameters the Service parameters
*/
public function __construct(CircuitBreaker $circuitBreaker, $eventName, $service, array $parameters)
{
Expand Down
7 changes: 3 additions & 4 deletions src/Exceptions/InvalidPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
final class InvalidPlace extends Exception implements ResiliencyException
{
/**
* @param mixed $failures the failures
* @param mixed $timeout the timeout
* @param mixed $failures the failures
* @param mixed $timeout the timeout
* @param mixed $threshold the threshold
*
* @return self
Expand All @@ -20,8 +20,7 @@ public static function invalidSettings($failures, $timeout, $threshold): self
$exceptionMessage = 'Invalid settings for Place' . PHP_EOL .
ErrorFormatter::format('failures', $failures, 'isPositiveInteger', 'a positive integer') .
ErrorFormatter::format('timeout', $timeout, 'isPositiveValue', 'a float') .
ErrorFormatter::format('threshold', $threshold, 'isPositiveInteger', 'a positive integer')
;
ErrorFormatter::format('threshold', $threshold, 'isPositiveInteger', 'a positive integer');

return new self($exceptionMessage);
}
Expand Down
34 changes: 34 additions & 0 deletions src/Exceptions/InvalidSystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Resiliency\Exceptions;

use Exception;
use Resiliency\Contracts\Exception as ResiliencyException;

final class InvalidSystem extends Exception implements ResiliencyException
{
const REQUIRED_SETTINGS = [
'timeout',
'stripped_timeout',
'failures',
'threshold',
];

/**
* @param array $settings the System settings
*
* @return self
*/
public static function missingSettings(array $settings): self
{
$exceptionMessage = 'Missing settings for System:' . PHP_EOL;

foreach (self::REQUIRED_SETTINGS as $setting) {
if (!array_key_exists($setting, $settings)) {
$exceptionMessage .= sprintf('The setting "%s" is missing from configuration', $setting);
}
}

return new self($exceptionMessage);
}
}
9 changes: 4 additions & 5 deletions src/Exceptions/InvalidTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
final class InvalidTransaction extends Exception implements ResiliencyException
{
/**
* @param mixed $service the service URI
* @param mixed $failures the failures
* @param mixed $state the Circuit Breaker
* @param mixed $service the service URI
* @param mixed $failures the failures
* @param mixed $state the Circuit Breaker
* @param mixed $threshold the threshold
*
* @return self
Expand All @@ -22,8 +22,7 @@ public static function invalidParameters($service, $failures, $state, $threshold
ErrorFormatter::format('service', $service, 'isURI', 'an URI') .
ErrorFormatter::format('failures', $failures, 'isPositiveInteger', 'a positive integer') .
ErrorFormatter::format('state', $state, 'isString', 'a string') .
ErrorFormatter::format('threshold', $threshold, 'isPositiveInteger', 'a positive integer')
;
ErrorFormatter::format('threshold', $threshold, 'isPositiveInteger', 'a positive integer');

return new self($exceptionMessage);
}
Expand Down
28 changes: 15 additions & 13 deletions src/MainCircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function isClosed(): bool
}

/**
* @param string $state the Place state
* @param string $state the Place state
* @param string $service the service URI
*
* @return Transaction
Expand All @@ -160,8 +160,8 @@ private function moveStateTo($state, $service): Transaction
}

/**
* @param string $service the service URI
* @param array $serviceParameters the service UI parameters
* @param string $service the service URI
* @param array $serviceParameters the service UI parameters
*
* @return Transaction
*/
Expand Down Expand Up @@ -207,8 +207,8 @@ private function canAccessService(Transaction $transaction): bool
/**
* Calls the client with the right information.
*
* @param string $service the service URI
* @param array $parameters the service URI parameters
* @param string $service the service URI
* @param array $parameters the service URI parameters
*
* @return string
*/
Expand All @@ -218,19 +218,22 @@ private function request(string $service, array $parameters = []): string

return $this->client->request(
$service,
array_merge($parameters, [
'connect_timeout' => $this->currentPlace->getTimeout(),
'timeout' => $this->currentPlace->getTimeout(),
])
array_merge(
$parameters,
[
'connect_timeout' => $this->currentPlace->getTimeout(),
'timeout' => $this->currentPlace->getTimeout(),
]
)
);
}

/**
* Helper to dispatch transition events.
*
* @param string $transition the transition name
* @param string $service the URI service called
* @param array $parameters the service parameters
* @param string $service the URI service called
* @param array $parameters the service parameters
*/
private function dispatch($transition, $service, array $parameters): void
{
Expand All @@ -240,7 +243,6 @@ private function dispatch($transition, $service, array $parameters): void
$transition,
$service,
$parameters
)
;
);
}
}
11 changes: 5 additions & 6 deletions src/Places/AbstractPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ abstract class AbstractPlace implements Place
private $threshold;

/**
* @param int $failures the Place failures
* @param float $timeout the Place timeout
* @param int $failures the Place failures
* @param float $timeout the Place timeout
* @param float $threshold the Place threshold
*/
public function __construct(int $failures, float $timeout, float $threshold)
Expand Down Expand Up @@ -81,8 +81,8 @@ public static function fromArray(array $settings): self
/**
* Ensure the place is valid
*
* @param int $failures the failures should be a positive value
* @param float $timeout the timeout should be a positive value
* @param int $failures the failures should be a positive value
* @param float $timeout the timeout should be a positive value
* @param float $threshold the threshold should be a positive value
*
* @throws InvalidPlace
Expand All @@ -93,8 +93,7 @@ private function validate(int $failures, float $timeout, float $threshold): bool
{
$assertionsAreValid = Assert::isPositiveInteger($failures)
&& Assert::isPositiveValue($timeout)
&& Assert::isPositiveValue($threshold)
;
&& Assert::isPositiveValue($threshold);

if ($assertionsAreValid) {
return true;
Expand Down
10 changes: 10 additions & 0 deletions src/Places/ClosedPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

final class ClosedPlace extends AbstractPlace
{
/**
* @param int $failures the Place failures
* @param float $timeout the Place timeout
* @param float $threshold the Place threshold
*/
public function __construct(int $failures, float $timeout, float $threshold)
{
parent::__construct($failures, $timeout, 0.0);
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Places/HalfOpenPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

final class HalfOpenPlace extends AbstractPlace
{
/**
* @param int $failures the Place failures
* @param float $timeout the Place timeout
* @param float $threshold the Place threshold
*/
public function __construct(int $failures, float $timeout, float $threshold)
{
parent::__construct(0, $timeout, 0.0);
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Places/OpenPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

final class OpenPlace extends AbstractPlace
{
/**
* @param int $failures the Place failures
* @param float $timeout the Place timeout
* @param float $threshold the Place threshold
*/
public function __construct(int $failures, float $timeout, float $threshold)
{
parent::__construct(0, 0, $threshold);
}

/**
* {@inheritdoc}
*/
Expand Down
40 changes: 28 additions & 12 deletions src/Systems/MainSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Resiliency\Systems;

use Resiliency\States;
use Resiliency\Contracts\Place;
use Resiliency\Contracts\System;
use Resiliency\Places\OpenPlace;
use Resiliency\Places\ClosedPlace;
use Resiliency\Places\HalfOpenPlace;
use Resiliency\Places\OpenPlace;
use Resiliency\States;
use Resiliency\Exceptions\InvalidSystem;

/**
* Implement the system described by the documentation.
Expand All @@ -19,15 +20,21 @@
final class MainSystem implements System
{
/**
* @var Place[]
* @param int $failures the number of allowed failures
* @param float $timeout the timeout in milliseconds
* @param float $strippedTimeout the timeout in milliseconds when trying again
* @param float $threshold the timeout in milliseconds before trying again
*/
private $places;

public function __construct(
Place $closedPlace,
Place $halfOpenPlace,
Place $openPlace
int $failures,
float $timeout,
float $strippedTimeout,
float $threshold
) {
$closedPlace = new ClosedPlace($failures, $timeout, 0);
$halfOpenPlace = new HalfOpenPlace(0, $strippedTimeout, 0);
$openPlace = new OpenPlace(0, 0, $threshold);

$this->places = [
$closedPlace->getState() => $closedPlace,
$halfOpenPlace->getState() => $halfOpenPlace,
Expand Down Expand Up @@ -58,10 +65,19 @@ public function getPlaces(): array
*/
public static function createFromArray(array $settings): self
{
$openPlace = OpenPlace::fromArray((array) $settings['open']);
$halfOpenPlace = HalfOpenPlace::fromArray((array) $settings['half_open']);
$closedPlace = ClosedPlace::fromArray((array) $settings['closed']);
if (array_key_exists('failures', $settings)
&& array_key_exists('timeout', $settings)
&& array_key_exists('stripped_timeout', $settings)
&& array_key_exists('threshold', $settings)
) {
return new self(
$settings['failures'],
$settings['timeout'],
$settings['stripped_timeout'],
$settings['threshold']
);
}

return new self($closedPlace, $halfOpenPlace, $openPlace);
throw InvalidSystem::missingSettings($settings);
}
}
Loading

0 comments on commit 5449892

Please sign in to comment.