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

Commit

Permalink
Added a PHP timeout exception on System initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickaël Andrieu committed Mar 31, 2020
1 parent c468e65 commit 64e41ce
Show file tree
Hide file tree
Showing 37 changed files with 179 additions and 209 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
"psr/simple-cache": "^1.0"
},
"require-dev": {
"edgedesign/phpqa": "v1.22",
"friendsofphp/php-cs-fixer": "^2.12",
"edgedesign/phpqa": "v1.23",
"friendsofphp/php-cs-fixer": "^2.16",
"guzzlehttp/guzzle": "^6.3",
"jakub-onderka/php-parallel-lint": "^1.0",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-phpunit": "^0.11.0",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
"phpunit/phpunit": "^8.0",
"sensiolabs/security-checker": "^5.0",
"symfony/cache": "~3.4|~4.3",
"symfony/cache": "~3.4|~4.4",
"symfony/http-client": "^4.3",
"vimeo/psalm": "^3.4"
},
Expand Down
3 changes: 3 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon

parameters:
checkMissingIterableValueType: false
12 changes: 4 additions & 8 deletions src/Clients/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Resiliency\Clients;

use Exception;
use GuzzleHttp\Client as OriginalGuzzleClient;
use Resiliency\Exceptions\UnavailableService;
use Resiliency\Contracts\Service;
use Resiliency\Contracts\Place;
use Exception;
use Resiliency\Contracts\Service;
use Resiliency\Exceptions\UnavailableService;

/**
* Guzzle implementation of client.
Expand All @@ -31,11 +31,7 @@ public function request(Service $service, Place $place): string

return (string) $client->request($method, $service->getURI(), $clientParameters)->getBody();
} catch (Exception $exception) {
throw new UnavailableService(
$exception->getMessage(),
(int) $exception->getCode(),
$exception
);
throw new UnavailableService($exception->getMessage(), (int) $exception->getCode(), $exception);
}
}
}
12 changes: 4 additions & 8 deletions src/Clients/SymfonyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Resiliency\Clients;

use Resiliency\Contracts\Place;
use Resiliency\Contracts\Service;
use Resiliency\Exceptions\UnavailableService;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Resiliency\Exceptions\UnavailableService;
use Resiliency\Contracts\Service;
use Resiliency\Contracts\Place;

/**
* Symfony implementation of client.
Expand Down Expand Up @@ -43,11 +43,7 @@ public function request(Service $service, Place $place): string

return $this->httpClient->request($method, $service->getURI(), $clientParameters)->getContent();
} catch (TransportExceptionInterface $exception) {
throw new UnavailableService(
$exception->getMessage(),
(int) $exception->getCode(),
$exception
);
throw new UnavailableService($exception->getMessage(), (int) $exception->getCode(), $exception);
}
}
}
8 changes: 0 additions & 8 deletions src/Contracts/CircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public function getDispatcher(): EventDispatcherInterface;
* @param callable $fallback if the service is unavailable, rely on the fallback
*
* @throws Exception in case of failure, throws an exception
*
* @return string
*/
public function call(string $uri, callable $fallback, array $uriParameters = []): string;

Expand All @@ -44,17 +42,13 @@ public function call(string $uri, callable $fallback, array $uriParameters = [])
* This can be used for example to take it offline for maintenance.
*
* @param string $uri the service URI to call
*
* @return self
*/
public function isolate(string $uri): self;

/**
* Reset the breaker to closed state to start accepting actions again.
*
* @param string $uri the service URI to call
*
* @return self
*/
public function reset(string $uri): self;

Expand All @@ -63,8 +57,6 @@ public function reset(string $uri): self;
*
* @param string $state the Place state
* @param Service $service the service
*
* @return self
*/
public function moveStateTo($state, Service $service): self;
}
2 changes: 0 additions & 2 deletions src/Contracts/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ interface Client
* @param Place $place the place
*
* @throws UnavailableService
*
* @return string
*/
public function request(Service $service, Place $place): string;
}
2 changes: 0 additions & 2 deletions src/Contracts/Monitoring/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ interface Report
{
/**
* @var ReportEntry the report entry
*
* @return self
*/
public function add(ReportEntry $reportEntry): self;

Expand Down
6 changes: 0 additions & 6 deletions src/Contracts/Place.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ interface Place
{
/**
* Return the state.
*
* @return string
*/
public function getState(): string;

Expand All @@ -37,17 +35,13 @@ public function getTimeout(): float;
* @param callable $fallback if the service is unavailable, rely on the fallback
*
* @throws Exception in case of failure, throws an exception
*
* @return string
*/
public function call(Transaction $transaction, callable $fallback): string;

/**
* Set the Circuit Breaker to the place.
*
* @param CircuitBreaker $circuitBreaker the circuit breaker
*
* @return self
*/
public function setCircuitBreaker(CircuitBreaker $circuitBreaker): self;
}
8 changes: 0 additions & 8 deletions src/Contracts/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ interface Storage
*
* @param string $serviceUri The service name
* @param Transaction $transaction The transaction
*
* @return bool
*/
public function saveTransaction(string $serviceUri, Transaction $transaction): bool;

Expand All @@ -25,8 +23,6 @@ public function saveTransaction(string $serviceUri, Transaction $transaction): b
*
* @param string $serviceUri the service name
*
* @return Transaction
*
*@throws TransactionNotFound
*/
public function getTransaction(string $serviceUri): Transaction;
Expand All @@ -35,15 +31,11 @@ public function getTransaction(string $serviceUri): Transaction;
* Checks if the transaction exists.
*
* @param string $serviceUri the service name
*
* @return bool
*/
public function hasTransaction(string $serviceUri): bool;

/**
* Clear the Circuit Breaker storage.
*
* @return bool
*/
public function clear(): bool;
}
4 changes: 0 additions & 4 deletions src/Contracts/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,11 @@ public function getThresholdDateTime(): DateTime;

/**
* Every time the service call fails, increment the number of failures.
*
* @return bool
*/
public function incrementFailures(): bool;

/**
* If the service is up again, reset the number of failures to 0.
*
* @return bool
*/
public function clearFailures(): bool;
}
2 changes: 1 addition & 1 deletion src/Events/TransitionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Resiliency\Events;

use Resiliency\Contracts\CircuitBreaker;
use Resiliency\Contracts\Event;
use Resiliency\Contracts\Service;
use Resiliency\Contracts\CircuitBreaker;

abstract class TransitionEvent implements Event
{
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/InvalidPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
namespace Resiliency\Exceptions;

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

final class InvalidPlace extends Exception implements ResiliencyException
{
/**
* @param mixed $failures the failures
* @param mixed $timeout the timeout
* @param mixed $threshold the threshold
*
* @return self
*/
public static function invalidSettings($failures, $timeout, $threshold): self
{
Expand Down
13 changes: 11 additions & 2 deletions src/Exceptions/InvalidSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ final class InvalidSystem extends Exception implements ResiliencyException

/**
* @param array $settings the System settings
*
* @return self
*/
public static function missingSettings(array $settings): self
{
Expand All @@ -34,4 +32,15 @@ public static function missingSettings(array $settings): self

return new self($exceptionMessage);
}

public static function phpTimeoutExceeded(): self
{
$exceptionMessage = 'The configuration timeout exceeds the PHP timeout' . PHP_EOL;
$exceptionMessage .= sprintf(
'Configure `max_execution_time` to a higher value, got: "%ss".',
ini_get('max_execution_time')
);

return new self($exceptionMessage);
}
}
4 changes: 1 addition & 3 deletions src/Exceptions/InvalidTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Resiliency\Exceptions;

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

final class InvalidTransaction extends Exception implements ResiliencyException
{
Expand All @@ -13,8 +13,6 @@ final class InvalidTransaction extends Exception implements ResiliencyException
* @param mixed $failures the failures
* @param mixed $state the Circuit Breaker
* @param mixed $threshold the threshold
*
* @return self
*/
public static function invalidParameters($service, $failures, $state, $threshold): self
{
Expand Down
14 changes: 6 additions & 8 deletions src/MainCircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace Resiliency;

use Psr\EventDispatcher\EventDispatcherInterface;
use Resiliency\Events\Initiated;
use Resiliency\Events\Isolated;
use Resiliency\Events\Reseted;
use Resiliency\Transactions\SimpleTransaction;
use Resiliency\Contracts\CircuitBreaker;
use Resiliency\Contracts\Transaction;
use Resiliency\Contracts\Place;
use Resiliency\Contracts\Service;
use Resiliency\Contracts\Storage;
use Resiliency\Contracts\System;
use Resiliency\Contracts\Place;
use Resiliency\Contracts\Transaction;
use Resiliency\Events\Initiated;
use Resiliency\Events\Isolated;
use Resiliency\Events\Reseted;
use Resiliency\Transactions\SimpleTransaction;

/**
* Main implementation of the Circuit Breaker.
Expand Down Expand Up @@ -139,8 +139,6 @@ public function moveStateTo($state, Service $service): CircuitBreaker
* @todo: refactor to remove this function in favor of moveStateTo
*
* @param Service $service the service
*
* @return Transaction
*/
private function initTransaction(Service $service): Transaction
{
Expand Down
2 changes: 1 addition & 1 deletion src/Monitors/SimpleMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Resiliency\Monitors;

use Resiliency\Contracts\Event;
use Resiliency\Contracts\Monitoring\Monitor;
use Resiliency\Contracts\Monitoring\Report;
use Resiliency\Contracts\Event;

final class SimpleMonitor implements Monitor
{
Expand Down
2 changes: 1 addition & 1 deletion src/Places/Closed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Resiliency\Places;

use Resiliency\Contracts\Client;
use Resiliency\Contracts\Transaction;
use Resiliency\Events\Tried;
use Resiliency\Exceptions\UnavailableService;
use Resiliency\Contracts\Transaction;
use Resiliency\States;

/**
Expand Down
10 changes: 3 additions & 7 deletions src/Places/PlaceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Resiliency\Places;

use DateTime;
use Resiliency\Contracts\CircuitBreaker;
use Resiliency\Contracts\Transaction;
use Resiliency\Contracts\Place;
use Resiliency\Contracts\Event;
use Resiliency\Contracts\Place;
use Resiliency\Contracts\Transaction;
use Resiliency\Exceptions\InvalidPlace;
use Resiliency\Utils\Assert;
use DateTime;

abstract class PlaceHelper implements Place
{
Expand Down Expand Up @@ -97,8 +97,6 @@ public function call(Transaction $transaction, callable $fallback): string

/**
* @param Transaction $transaction the Transaction
*
* @return bool
*/
public function isAllowedToRetry(Transaction $transaction): bool
{
Expand All @@ -107,8 +105,6 @@ public function isAllowedToRetry(Transaction $transaction): bool

/**
* @param Transaction $transaction the Transaction
*
* @return bool
*/
public function haveWaitedLongEnough(Transaction $transaction): bool
{
Expand Down
2 changes: 1 addition & 1 deletion src/Storages/SymfonyCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Resiliency\Storages;

use Psr\SimpleCache\CacheInterface;
use Resiliency\Contracts\Storage;
use Resiliency\Contracts\Transaction;
use Resiliency\Exceptions\TransactionNotFound;
use Psr\SimpleCache\CacheInterface;

/**
* Implementation of Storage using the Symfony Cache Component.
Expand Down
Loading

0 comments on commit 64e41ce

Please sign in to comment.